Add FORCE argument to `delete-file'.
[bpt/emacs.git] / 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 (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 (<= ,level tramp-verbose)
2275 (condition-case nil
2276 (setq pr (funcall 'make-progress-reporter ,message)
2277 tm (run-at-time 3 0.1 'progress-reporter-update pr))
2278 (error nil)))
2279 (unwind-protect
2280 ;; Execute the body.
2281 (progn ,@body)
2282 ;; Stop progress reporter.
2283 (if tm (cancel-timer tm))
2284 (tramp-message ,vec ,level "%s...done" ,message))))
2285
2286 (put 'with-progress-reporter 'lisp-indent-function 3)
2287 (put 'with-progress-reporter 'edebug-form-spec t)
2288 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-progress-reporter\\>"))
2289
2290 (eval-and-compile ;; Silence compiler.
2291 (if (memq system-type '(cygwin windows-nt))
2292 (defun tramp-drop-volume-letter (name)
2293 "Cut off unnecessary drive letter from file NAME.
2294 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2295 locally on a remote file name. When the local system is a W32 system
2296 but the remote system is Unix, this introduces a superfluous drive
2297 letter into the file name. This function removes it."
2298 (save-match-data
2299 (if (string-match tramp-root-regexp name)
2300 (replace-match "/" nil t name)
2301 name)))
2302
2303 (defalias 'tramp-drop-volume-letter 'identity)))
2304
2305 (defsubst tramp-make-tramp-temp-file (vec)
2306 "Create a temporary file on the remote host identified by VEC.
2307 Return the local name of the temporary file."
2308 (let ((prefix
2309 (tramp-make-tramp-file-name
2310 (tramp-file-name-method vec)
2311 (tramp-file-name-user vec)
2312 (tramp-file-name-host vec)
2313 (tramp-drop-volume-letter
2314 (expand-file-name
2315 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
2316 result)
2317 (while (not result)
2318 ;; `make-temp-file' would be the natural choice for
2319 ;; implementation. But it calls `write-region' internally,
2320 ;; which also needs a temporary file - we would end in an
2321 ;; infinite loop.
2322 (setq result (make-temp-name prefix))
2323 (if (file-exists-p result)
2324 (setq result nil)
2325 ;; This creates the file by side effect.
2326 (set-file-times result)
2327 (set-file-modes result (tramp-octal-to-decimal "0700"))))
2328
2329 ;; Return the local part.
2330 (with-parsed-tramp-file-name result nil localname)))
2331
2332
2333 ;;; Config Manipulation Functions:
2334
2335 (defun tramp-set-completion-function (method function-list)
2336 "Sets the list of completion functions for METHOD.
2337 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2338 The FUNCTION is intended to parse FILE according its syntax.
2339 It might be a predefined FUNCTION, or a user defined FUNCTION.
2340 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2341 `tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
2342 and `tramp-parse-netrc'.
2343
2344 Example:
2345
2346 (tramp-set-completion-function
2347 \"ssh\"
2348 '((tramp-parse-sconfig \"/etc/ssh_config\")
2349 (tramp-parse-sconfig \"~/.ssh/config\")))"
2350
2351 (let ((r function-list)
2352 (v function-list))
2353 (setq tramp-completion-function-alist
2354 (delete (assoc method tramp-completion-function-alist)
2355 tramp-completion-function-alist))
2356
2357 (while v
2358 ;; Remove double entries.
2359 (when (member (car v) (cdr v))
2360 (setcdr v (delete (car v) (cdr v))))
2361 ;; Check for function and file or registry key.
2362 (unless (and (functionp (nth 0 (car v)))
2363 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
2364 ;; Windows registry.
2365 (and (memq system-type '(cygwin windows-nt))
2366 (zerop
2367 (tramp-local-call-process
2368 "reg" nil nil nil "query" (nth 1 (car v)))))
2369 ;; Configuration file.
2370 (file-exists-p (nth 1 (car v)))))
2371 (setq r (delete (car v) r)))
2372 (setq v (cdr v)))
2373
2374 (when r
2375 (add-to-list 'tramp-completion-function-alist
2376 (cons method r)))))
2377
2378 (defun tramp-get-completion-function (method)
2379 "Returns a list of completion functions for METHOD.
2380 For definition of that list see `tramp-set-completion-function'."
2381 (cons
2382 ;; Hosts visited once shall be remembered.
2383 `(tramp-parse-connection-properties ,method)
2384 ;; The method related defaults.
2385 (cdr (assoc method tramp-completion-function-alist))))
2386
2387
2388 ;;; Fontification of `read-file-name':
2389
2390 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
2391 (defvar tramp-rfn-eshadow-overlay)
2392 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
2393
2394 (defun tramp-rfn-eshadow-setup-minibuffer ()
2395 "Set up a minibuffer for `file-name-shadow-mode'.
2396 Adds another overlay hiding filename parts according to Tramp's
2397 special handling of `substitute-in-file-name'."
2398 (when (symbol-value 'minibuffer-completing-file-name)
2399 (setq tramp-rfn-eshadow-overlay
2400 (funcall (symbol-function 'make-overlay)
2401 (funcall (symbol-function 'minibuffer-prompt-end))
2402 (funcall (symbol-function 'minibuffer-prompt-end))))
2403 ;; Copy rfn-eshadow-overlay properties.
2404 (let ((props (funcall (symbol-function 'overlay-properties)
2405 (symbol-value 'rfn-eshadow-overlay))))
2406 (while props
2407 (funcall (symbol-function 'overlay-put)
2408 tramp-rfn-eshadow-overlay (pop props) (pop props))))))
2409
2410 (when (boundp 'rfn-eshadow-setup-minibuffer-hook)
2411 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2412 'tramp-rfn-eshadow-setup-minibuffer)
2413 (add-hook 'tramp-unload-hook
2414 (lambda ()
2415 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2416 'tramp-rfn-eshadow-setup-minibuffer))))
2417
2418 (defconst tramp-rfn-eshadow-update-overlay-regexp
2419 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
2420
2421 (defun tramp-rfn-eshadow-update-overlay ()
2422 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2423 This is intended to be used as a minibuffer `post-command-hook' for
2424 `file-name-shadow-mode'; the minibuffer should have already
2425 been set up by `rfn-eshadow-setup-minibuffer'."
2426 ;; In remote files name, there is a shadowing just for the local part.
2427 (let ((end (or (funcall (symbol-function 'overlay-end)
2428 (symbol-value 'rfn-eshadow-overlay))
2429 (funcall (symbol-function 'minibuffer-prompt-end)))))
2430 (when (file-remote-p (buffer-substring-no-properties end (point-max)))
2431 (save-excursion
2432 (save-restriction
2433 (narrow-to-region
2434 (1+ (or (string-match
2435 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
2436 end))
2437 (point-max))
2438 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
2439 (rfn-eshadow-update-overlay-hook nil))
2440 (move-overlay rfn-eshadow-overlay (point-max) (point-max))
2441 (funcall (symbol-function 'rfn-eshadow-update-overlay))))))))
2442
2443 (when (boundp 'rfn-eshadow-update-overlay-hook)
2444 (add-hook 'rfn-eshadow-update-overlay-hook
2445 'tramp-rfn-eshadow-update-overlay)
2446 (add-hook 'tramp-unload-hook
2447 (lambda ()
2448 (remove-hook 'rfn-eshadow-update-overlay-hook
2449 'tramp-rfn-eshadow-update-overlay))))
2450
2451
2452 ;;; Integration of eshell.el:
2453
2454 (eval-when-compile
2455 (defvar eshell-path-env))
2456
2457 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
2458 ;; when `default-directory' points to another host.
2459 (defun tramp-eshell-directory-change ()
2460 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2461 (setq eshell-path-env
2462 (if (file-remote-p default-directory)
2463 (with-parsed-tramp-file-name default-directory nil
2464 (mapconcat
2465 'identity
2466 (tramp-get-remote-path v)
2467 ":"))
2468 (getenv "PATH"))))
2469
2470 (eval-after-load "esh-util"
2471 '(progn
2472 (tramp-eshell-directory-change)
2473 (add-hook 'eshell-directory-change-hook
2474 'tramp-eshell-directory-change)
2475 (add-hook 'tramp-unload-hook
2476 (lambda ()
2477 (remove-hook 'eshell-directory-change-hook
2478 'tramp-eshell-directory-change)))))
2479
2480
2481 ;;; File Name Handler Functions:
2482
2483 (defun tramp-handle-make-symbolic-link
2484 (filename linkname &optional ok-if-already-exists)
2485 "Like `make-symbolic-link' for Tramp files.
2486 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2487 the symlink. If LINKNAME is a Tramp file, only the localname component is
2488 used as the target of the symlink.
2489
2490 If LINKNAME is a Tramp file and the localname component is relative, then
2491 it is expanded first, before the localname component is taken. Note that
2492 this can give surprising results if the user/host for the source and
2493 target of the symlink differ."
2494 (with-parsed-tramp-file-name linkname l
2495 (let ((ln (tramp-get-remote-ln l))
2496 (cwd (tramp-run-real-handler
2497 'file-name-directory (list l-localname))))
2498 (unless ln
2499 (tramp-error
2500 l 'file-error
2501 "Making a symbolic link. ln(1) does not exist on the remote host."))
2502
2503 ;; Do the 'confirm if exists' thing.
2504 (when (file-exists-p linkname)
2505 ;; What to do?
2506 (if (or (null ok-if-already-exists) ; not allowed to exist
2507 (and (numberp ok-if-already-exists)
2508 (not (yes-or-no-p
2509 (format
2510 "File %s already exists; make it a link anyway? "
2511 l-localname)))))
2512 (tramp-error
2513 l 'file-already-exists "File %s already exists" l-localname)
2514 (tramp-compat-delete-file linkname 'force)))
2515
2516 ;; If FILENAME is a Tramp name, use just the localname component.
2517 (when (tramp-tramp-file-p filename)
2518 (setq filename
2519 (tramp-file-name-localname
2520 (tramp-dissect-file-name (expand-file-name filename)))))
2521
2522 ;; Right, they are on the same host, regardless of user, method, etc.
2523 ;; We now make the link on the remote machine. This will occur as the user
2524 ;; that FILENAME belongs to.
2525 (zerop
2526 (tramp-send-command-and-check
2527 l
2528 (format
2529 "cd %s && %s -sf %s %s"
2530 (tramp-shell-quote-argument cwd)
2531 ln
2532 (tramp-shell-quote-argument filename)
2533 (tramp-shell-quote-argument l-localname))
2534 t)))))
2535
2536 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2537 "Like `load' for Tramp files."
2538 (with-parsed-tramp-file-name (expand-file-name file) nil
2539 (unless nosuffix
2540 (cond ((file-exists-p (concat file ".elc"))
2541 (setq file (concat file ".elc")))
2542 ((file-exists-p (concat file ".el"))
2543 (setq file (concat file ".el")))))
2544 (when must-suffix
2545 ;; The first condition is always true for absolute file names.
2546 ;; Included for safety's sake.
2547 (unless (or (file-name-directory file)
2548 (string-match "\\.elc?\\'" file))
2549 (tramp-error
2550 v 'file-error
2551 "File `%s' does not include a `.el' or `.elc' suffix" file)))
2552 (unless noerror
2553 (when (not (file-exists-p file))
2554 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
2555 (if (not (file-exists-p file))
2556 nil
2557 (unless nomessage (tramp-message v 0 "Loading %s..." file))
2558 (let ((local-copy (file-local-copy file)))
2559 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2560 (unwind-protect
2561 (load local-copy noerror t t)
2562 (tramp-compat-delete-file local-copy 'force)))
2563 (unless nomessage (tramp-message v 0 "Loading %s...done" file))
2564 t)))
2565
2566 ;; Localname manipulation functions that grok Tramp localnames...
2567 (defun tramp-handle-file-name-as-directory (file)
2568 "Like `file-name-as-directory' but aware of Tramp files."
2569 ;; `file-name-as-directory' would be sufficient except localname is
2570 ;; the empty string.
2571 (let ((v (tramp-dissect-file-name file t)))
2572 ;; Run the command on the localname portion only.
2573 (tramp-make-tramp-file-name
2574 (tramp-file-name-method v)
2575 (tramp-file-name-user v)
2576 (tramp-file-name-host v)
2577 (tramp-run-real-handler
2578 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2579
2580 (defun tramp-handle-file-name-directory (file)
2581 "Like `file-name-directory' but aware of Tramp files."
2582 ;; Everything except the last filename thing is the directory. We
2583 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2584 ;; the remote file name parts. This is a problem when we are in
2585 ;; file name completion.
2586 (let ((v (tramp-dissect-file-name file t)))
2587 ;; Run the command on the localname portion only.
2588 (tramp-make-tramp-file-name
2589 (tramp-file-name-method v)
2590 (tramp-file-name-user v)
2591 (tramp-file-name-host v)
2592 (tramp-run-real-handler
2593 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
2594
2595 (defun tramp-handle-file-name-nondirectory (file)
2596 "Like `file-name-nondirectory' but aware of Tramp files."
2597 (with-parsed-tramp-file-name file nil
2598 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2599
2600 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2601 "Like `file-truename' for Tramp files."
2602 (with-parsed-tramp-file-name (expand-file-name filename) nil
2603 (with-file-property v localname "file-truename"
2604 (let ((result nil)) ; result steps in reverse order
2605 (tramp-message v 4 "Finding true name for `%s'" filename)
2606 (cond
2607 ;; Use GNU readlink --canonicalize-missing where available.
2608 ((tramp-get-remote-readlink v)
2609 (setq result
2610 (tramp-send-command-and-read
2611 v
2612 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2613 (tramp-get-remote-readlink v)
2614 (tramp-shell-quote-argument localname)))))
2615
2616 ;; Use Perl implementation.
2617 ((and (tramp-get-remote-perl v)
2618 (tramp-get-connection-property v "perl-file-spec" nil)
2619 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2620 (tramp-maybe-send-script
2621 v tramp-perl-file-truename "tramp_perl_file_truename")
2622 (setq result
2623 (tramp-send-command-and-read
2624 v
2625 (format "tramp_perl_file_truename %s"
2626 (tramp-shell-quote-argument localname)))))
2627
2628 ;; Do it yourself. We bind `directory-sep-char' here for
2629 ;; XEmacs on Windows, which would otherwise use backslash.
2630 (t (let* ((directory-sep-char ?/)
2631 (steps (tramp-compat-split-string localname "/"))
2632 (localnamedir (tramp-run-real-handler
2633 'file-name-as-directory (list localname)))
2634 (is-dir (string= localname localnamedir))
2635 (thisstep nil)
2636 (numchase 0)
2637 ;; Don't make the following value larger than
2638 ;; necessary. People expect an error message in a
2639 ;; timely fashion when something is wrong;
2640 ;; otherwise they might think that Emacs is hung.
2641 ;; Of course, correctness has to come first.
2642 (numchase-limit 20)
2643 symlink-target)
2644 (while (and steps (< numchase numchase-limit))
2645 (setq thisstep (pop steps))
2646 (tramp-message
2647 v 5 "Check %s"
2648 (mapconcat 'identity
2649 (append '("") (reverse result) (list thisstep))
2650 "/"))
2651 (setq symlink-target
2652 (nth 0 (file-attributes
2653 (tramp-make-tramp-file-name
2654 method user host
2655 (mapconcat 'identity
2656 (append '("")
2657 (reverse result)
2658 (list thisstep))
2659 "/")))))
2660 (cond ((string= "." thisstep)
2661 (tramp-message v 5 "Ignoring step `.'"))
2662 ((string= ".." thisstep)
2663 (tramp-message v 5 "Processing step `..'")
2664 (pop result))
2665 ((stringp symlink-target)
2666 ;; It's a symlink, follow it.
2667 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2668 (setq numchase (1+ numchase))
2669 (when (file-name-absolute-p symlink-target)
2670 (setq result nil))
2671 ;; If the symlink was absolute, we'll get a string like
2672 ;; "/user@host:/some/target"; extract the
2673 ;; "/some/target" part from it.
2674 (when (tramp-tramp-file-p symlink-target)
2675 (unless (tramp-equal-remote filename symlink-target)
2676 (tramp-error
2677 v 'file-error
2678 "Symlink target `%s' on wrong host" symlink-target))
2679 (setq symlink-target localname))
2680 (setq steps
2681 (append (tramp-compat-split-string
2682 symlink-target "/")
2683 steps)))
2684 (t
2685 ;; It's a file.
2686 (setq result (cons thisstep result)))))
2687 (when (>= numchase numchase-limit)
2688 (tramp-error
2689 v 'file-error
2690 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2691 (setq result (reverse result))
2692 ;; Combine list to form string.
2693 (setq result
2694 (if result
2695 (mapconcat 'identity (cons "" result) "/")
2696 "/"))
2697 (when (and is-dir (or (string= "" result)
2698 (not (string= (substring result -1) "/"))))
2699 (setq result (concat result "/"))))))
2700
2701 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2702 (tramp-make-tramp-file-name method user host result)))))
2703
2704 ;; Basic functions.
2705
2706 (defun tramp-handle-file-exists-p (filename)
2707 "Like `file-exists-p' for Tramp files."
2708 (with-parsed-tramp-file-name filename nil
2709 (with-file-property v localname "file-exists-p"
2710 (or (not (null (tramp-get-file-property
2711 v localname "file-attributes-integer" nil)))
2712 (not (null (tramp-get-file-property
2713 v localname "file-attributes-string" nil)))
2714 (zerop (tramp-send-command-and-check
2715 v
2716 (format
2717 "%s %s"
2718 (tramp-get-file-exists-command v)
2719 (tramp-shell-quote-argument localname))))))))
2720
2721 ;; Inodes don't exist for some file systems. Therefore we must
2722 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2723 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2724 ;; performance isn't the major issue given that file transfer will
2725 ;; take time.
2726 (defvar tramp-inodes nil
2727 "Keeps virtual inodes numbers.")
2728
2729 ;; Devices must distinguish physical file systems. The device numbers
2730 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2731 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2732 ;; EFS use device number "-1". In order to be different, we use device number
2733 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2734 (defvar tramp-devices nil
2735 "Keeps virtual device numbers.")
2736
2737 ;; CCC: This should check for an error condition and signal failure
2738 ;; when something goes wrong.
2739 ;; Daniel Pittman <daniel@danann.net>
2740 (defun tramp-handle-file-attributes (filename &optional id-format)
2741 "Like `file-attributes' for Tramp files."
2742 (unless id-format (setq id-format 'integer))
2743 ;; Don't modify `last-coding-system-used' by accident.
2744 (let ((last-coding-system-used last-coding-system-used))
2745 (with-parsed-tramp-file-name (expand-file-name filename) nil
2746 (with-file-property v localname (format "file-attributes-%s" id-format)
2747 (save-excursion
2748 (tramp-convert-file-attributes
2749 v
2750 (cond
2751 ((tramp-get-remote-stat v)
2752 (tramp-do-file-attributes-with-stat v localname id-format))
2753 ((tramp-get-remote-perl v)
2754 (tramp-do-file-attributes-with-perl v localname id-format))
2755 (t
2756 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2757
2758 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
2759 "Implement `file-attributes' for Tramp files using the ls(1) command."
2760 (let (symlinkp dirp
2761 res-inode res-filemodes res-numlinks
2762 res-uid res-gid res-size res-symlink-target)
2763 (tramp-message vec 5 "file attributes with ls: %s" localname)
2764 (tramp-send-command
2765 vec
2766 (format "(%s %s || %s -h %s) && %s %s %s"
2767 (tramp-get-file-exists-command vec)
2768 (tramp-shell-quote-argument localname)
2769 (tramp-get-test-command vec)
2770 (tramp-shell-quote-argument localname)
2771 (tramp-get-ls-command vec)
2772 (if (eq id-format 'integer) "-ildn" "-ild")
2773 (tramp-shell-quote-argument localname)))
2774 ;; parse `ls -l' output ...
2775 (with-current-buffer (tramp-get-buffer vec)
2776 (when (> (buffer-size) 0)
2777 (goto-char (point-min))
2778 ;; ... inode
2779 (setq res-inode
2780 (condition-case err
2781 (read (current-buffer))
2782 (invalid-read-syntax
2783 (when (and (equal (cadr err)
2784 "Integer constant overflow in reader")
2785 (string-match
2786 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2787 (car (cddr err))))
2788 (let* ((big (read (substring (car (cddr err)) 0
2789 (match-beginning 1))))
2790 (small (read (match-string 1 (car (cddr err)))))
2791 (twiddle (/ small 65536)))
2792 (cons (+ big twiddle)
2793 (- small (* twiddle 65536))))))))
2794 ;; ... file mode flags
2795 (setq res-filemodes (symbol-name (read (current-buffer))))
2796 ;; ... number links
2797 (setq res-numlinks (read (current-buffer)))
2798 ;; ... uid and gid
2799 (setq res-uid (read (current-buffer)))
2800 (setq res-gid (read (current-buffer)))
2801 (if (eq id-format 'integer)
2802 (progn
2803 (unless (numberp res-uid) (setq res-uid -1))
2804 (unless (numberp res-gid) (setq res-gid -1)))
2805 (progn
2806 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
2807 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
2808 ;; ... size
2809 (setq res-size (read (current-buffer)))
2810 ;; From the file modes, figure out other stuff.
2811 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2812 (setq dirp (eq ?d (aref res-filemodes 0)))
2813 ;; if symlink, find out file name pointed to
2814 (when symlinkp
2815 (search-forward "-> ")
2816 (setq res-symlink-target
2817 (buffer-substring (point) (tramp-compat-line-end-position))))
2818 ;; return data gathered
2819 (list
2820 ;; 0. t for directory, string (name linked to) for symbolic
2821 ;; link, or nil.
2822 (or dirp res-symlink-target)
2823 ;; 1. Number of links to file.
2824 res-numlinks
2825 ;; 2. File uid.
2826 res-uid
2827 ;; 3. File gid.
2828 res-gid
2829 ;; 4. Last access time, as a list of two integers. First
2830 ;; integer has high-order 16 bits of time, second has low 16
2831 ;; bits.
2832 ;; 5. Last modification time, likewise.
2833 ;; 6. Last status change time, likewise.
2834 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2835 ;; 7. Size in bytes (-1, if number is out of range).
2836 res-size
2837 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2838 res-filemodes
2839 ;; 9. t if file's gid would change if file were deleted and
2840 ;; recreated. Will be set in `tramp-convert-file-attributes'
2841 t
2842 ;; 10. inode number.
2843 res-inode
2844 ;; 11. Device number. Will be replaced by a virtual device number.
2845 -1
2846 )))))
2847
2848 (defun tramp-do-file-attributes-with-perl
2849 (vec localname &optional id-format)
2850 "Implement `file-attributes' for Tramp files using a Perl script."
2851 (tramp-message vec 5 "file attributes with perl: %s" localname)
2852 (tramp-maybe-send-script
2853 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
2854 (tramp-send-command-and-read
2855 vec
2856 (format "tramp_perl_file_attributes %s %s"
2857 (tramp-shell-quote-argument localname) id-format)))
2858
2859 (defun tramp-do-file-attributes-with-stat
2860 (vec localname &optional id-format)
2861 "Implement `file-attributes' for Tramp files using stat(1) command."
2862 (tramp-message vec 5 "file attributes with stat: %s" localname)
2863 (tramp-send-command-and-read
2864 vec
2865 (format
2866 "((%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)"
2867 (tramp-get-file-exists-command vec)
2868 (tramp-shell-quote-argument localname)
2869 (tramp-get-test-command vec)
2870 (tramp-shell-quote-argument localname)
2871 (tramp-get-remote-stat vec)
2872 (if (eq id-format 'integer) "%u" "\"%U\"")
2873 (if (eq id-format 'integer) "%g" "\"%G\"")
2874 (tramp-shell-quote-argument localname))))
2875
2876 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2877 "Like `set-visited-file-modtime' for Tramp files."
2878 (unless (buffer-file-name)
2879 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2880 (buffer-name)))
2881 (if time-list
2882 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2883 (let ((f (buffer-file-name))
2884 coding-system-used)
2885 (with-parsed-tramp-file-name f nil
2886 (let* ((attr (file-attributes f))
2887 ;; '(-1 65535) means file doesn't exists yet.
2888 (modtime (or (nth 5 attr) '(-1 65535))))
2889 (when (boundp 'last-coding-system-used)
2890 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2891 ;; We use '(0 0) as a don't-know value. See also
2892 ;; `tramp-do-file-attributes-with-ls'.
2893 (if (not (equal modtime '(0 0)))
2894 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2895 (progn
2896 (tramp-send-command
2897 v
2898 (format "%s -ild %s"
2899 (tramp-get-ls-command v)
2900 (tramp-shell-quote-argument localname)))
2901 (setq attr (buffer-substring (point)
2902 (progn (end-of-line) (point)))))
2903 (tramp-set-file-property
2904 v localname "visited-file-modtime-ild" attr))
2905 (when (boundp 'last-coding-system-used)
2906 (set 'last-coding-system-used coding-system-used))
2907 nil)))))
2908
2909 ;; This function makes the same assumption as
2910 ;; `tramp-handle-set-visited-file-modtime'.
2911 (defun tramp-handle-verify-visited-file-modtime (buf)
2912 "Like `verify-visited-file-modtime' for Tramp files.
2913 At the time `verify-visited-file-modtime' calls this function, we
2914 already know that the buffer is visiting a file and that
2915 `visited-file-modtime' does not return 0. Do not call this
2916 function directly, unless those two cases are already taken care
2917 of."
2918 (with-current-buffer buf
2919 ;; There is no file visiting the buffer, or the buffer has no
2920 ;; recorded last modification time.
2921 (if (or (not (buffer-file-name))
2922 (eq (visited-file-modtime) 0))
2923 t
2924 (let ((f (buffer-file-name)))
2925 (with-parsed-tramp-file-name f nil
2926 (tramp-flush-file-property v localname)
2927 (let* ((attr (file-attributes f))
2928 (modtime (nth 5 attr))
2929 (mt (visited-file-modtime)))
2930
2931 (cond
2932 ;; File exists, and has a known modtime.
2933 ((and attr (not (equal modtime '(0 0))))
2934 (< (abs (tramp-time-diff
2935 modtime
2936 ;; For compatibility, deal with both the old
2937 ;; (HIGH . LOW) and the new (HIGH LOW) return
2938 ;; values of `visited-file-modtime'.
2939 (if (atom (cdr mt))
2940 (list (car mt) (cdr mt))
2941 mt)))
2942 2))
2943 ;; Modtime has the don't know value.
2944 (attr
2945 (tramp-send-command
2946 v
2947 (format "%s -ild %s"
2948 (tramp-get-ls-command v)
2949 (tramp-shell-quote-argument localname)))
2950 (with-current-buffer (tramp-get-buffer v)
2951 (setq attr (buffer-substring
2952 (point) (progn (end-of-line) (point)))))
2953 (equal
2954 attr
2955 (tramp-get-file-property
2956 v localname "visited-file-modtime-ild" "")))
2957 ;; If file does not exist, say it is not modified if and
2958 ;; only if that agrees with the buffer's record.
2959 (t (equal mt '(-1 65535))))))))))
2960
2961 (defun tramp-handle-set-file-modes (filename mode)
2962 "Like `set-file-modes' for Tramp files."
2963 (with-parsed-tramp-file-name filename nil
2964 (tramp-flush-file-property v localname)
2965 (unless (zerop (tramp-send-command-and-check
2966 v
2967 (format "chmod %s %s"
2968 (tramp-decimal-to-octal mode)
2969 (tramp-shell-quote-argument localname))))
2970 ;; FIXME: extract the proper text from chmod's stderr.
2971 (tramp-error
2972 v 'file-error "Error while changing file's mode %s" filename))))
2973
2974 (defun tramp-handle-set-file-times (filename &optional time)
2975 "Like `set-file-times' for Tramp files."
2976 (zerop
2977 (if (file-remote-p filename)
2978 (with-parsed-tramp-file-name filename nil
2979 (tramp-flush-file-property v localname)
2980 (let ((time (if (or (null time) (equal time '(0 0)))
2981 (current-time)
2982 time))
2983 (utc
2984 ;; With GNU Emacs, `format-time-string' has an
2985 ;; optional parameter UNIVERSAL. This is preferred,
2986 ;; because we could handle the case when the remote
2987 ;; host is located in a different time zone as the
2988 ;; local host.
2989 (and (functionp 'subr-arity)
2990 (subrp (symbol-function 'format-time-string))
2991 (= 3 (cdr (funcall (symbol-function 'subr-arity)
2992 (symbol-function
2993 'format-time-string)))))))
2994 (tramp-send-command-and-check
2995 v (format "%s touch -t %s %s"
2996 (if utc "TZ=UTC; export TZ;" "")
2997 (if utc
2998 (format-time-string "%Y%m%d%H%M.%S" time t)
2999 (format-time-string "%Y%m%d%H%M.%S" time))
3000 (tramp-shell-quote-argument localname)))))
3001
3002 ;; We handle also the local part, because in older Emacsen,
3003 ;; without `set-file-times', this function is an alias for this.
3004 ;; We are local, so we don't need the UTC settings.
3005 (tramp-local-call-process
3006 "touch" nil nil nil "-t"
3007 (format-time-string "%Y%m%d%H%M.%S" time)
3008 (tramp-shell-quote-argument filename)))))
3009
3010 (defun tramp-set-file-uid-gid (filename &optional uid gid)
3011 "Set the ownership for FILENAME.
3012 If UID and GID are provided, these values are used; otherwise uid
3013 and gid of the corresponding user is taken. Both parameters must be integers."
3014 ;; Modern Unices allow chown only for root. So we might need
3015 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3016 ;; working with su(do)? when it is needed, so it shall succeed in
3017 ;; the majority of cases.
3018 ;; Don't modify `last-coding-system-used' by accident.
3019 (let ((last-coding-system-used last-coding-system-used))
3020 (if (file-remote-p filename)
3021 (with-parsed-tramp-file-name filename nil
3022 (if (and (zerop (user-uid)) (tramp-local-host-p v))
3023 ;; If we are root on the local host, we can do it directly.
3024 (tramp-set-file-uid-gid localname uid gid)
3025 (let ((uid (or (and (integerp uid) uid)
3026 (tramp-get-remote-uid v 'integer)))
3027 (gid (or (and (integerp gid) gid)
3028 (tramp-get-remote-gid v 'integer))))
3029 (tramp-send-command
3030 v (format
3031 "chown %d:%d %s" uid gid
3032 (tramp-shell-quote-argument localname))))))
3033
3034 ;; We handle also the local part, because there doesn't exist
3035 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3036 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
3037 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
3038 (tramp-local-call-process
3039 "chown" nil nil nil
3040 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
3041
3042 (defun tramp-remote-selinux-p (vec)
3043 "Check, whether SELINUX is enabled on the remote host."
3044 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
3045 (let ((result (tramp-find-executable
3046 vec "getenforce" (tramp-get-remote-path vec) t t)))
3047 (and result
3048 (string-equal
3049 (tramp-send-command-and-read
3050 vec (format "echo \\\"`%S`\\\"" result))
3051 "Enforcing")))))
3052
3053 (defun tramp-handle-file-selinux-context (filename)
3054 "Like `file-selinux-context' for Tramp files."
3055 (with-parsed-tramp-file-name filename nil
3056 (with-file-property v localname "file-selinux-context"
3057 (let ((context '(nil nil nil nil))
3058 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3059 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
3060 (when (and (tramp-remote-selinux-p v)
3061 (zerop (tramp-send-command-and-check
3062 v (format
3063 "%s -d -Z %s"
3064 (tramp-get-ls-command v)
3065 (tramp-shell-quote-argument localname)))))
3066 (with-current-buffer (tramp-get-connection-buffer v)
3067 (goto-char (point-min))
3068 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
3069 (setq context (list (match-string 1) (match-string 2)
3070 (match-string 3) (match-string 4))))))
3071 ;; Return the context.
3072 context))))
3073
3074 (defun tramp-handle-set-file-selinux-context (filename context)
3075 "Like `set-file-selinux-context' for Tramp files."
3076 (with-parsed-tramp-file-name filename nil
3077 (if (and (consp context)
3078 (tramp-remote-selinux-p v)
3079 (zerop (tramp-send-command-and-check
3080 v (format "chcon %s %s %s %s %s"
3081 (if (stringp (nth 0 context))
3082 (format "--user=%s" (nth 0 context)) "")
3083 (if (stringp (nth 1 context))
3084 (format "--role=%s" (nth 1 context)) "")
3085 (if (stringp (nth 2 context))
3086 (format "--type=%s" (nth 2 context)) "")
3087 (if (stringp (nth 3 context))
3088 (format "--range=%s" (nth 3 context)) "")
3089 (tramp-shell-quote-argument localname)))))
3090 (tramp-set-file-property v localname "file-selinux-context" context)
3091 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
3092 ;; We always return nil.
3093 nil)
3094
3095 ;; Simple functions using the `test' command.
3096
3097 (defun tramp-handle-file-executable-p (filename)
3098 "Like `file-executable-p' for Tramp files."
3099 (with-parsed-tramp-file-name filename nil
3100 (with-file-property v localname "file-executable-p"
3101 ;; Examine `file-attributes' cache to see if request can be
3102 ;; satisfied without remote operation.
3103 (or (tramp-check-cached-permissions v ?x)
3104 (zerop (tramp-run-test "-x" filename))))))
3105
3106 (defun tramp-handle-file-readable-p (filename)
3107 "Like `file-readable-p' for Tramp files."
3108 (with-parsed-tramp-file-name filename nil
3109 (with-file-property v localname "file-readable-p"
3110 ;; Examine `file-attributes' cache to see if request can be
3111 ;; satisfied without remote operation.
3112 (or (tramp-check-cached-permissions v ?r)
3113 (zerop (tramp-run-test "-r" filename))))))
3114
3115 ;; When the remote shell is started, it looks for a shell which groks
3116 ;; tilde expansion. Here, we assume that all shells which grok tilde
3117 ;; expansion will also provide a `test' command which groks `-nt' (for
3118 ;; newer than). If this breaks, tell me about it and I'll try to do
3119 ;; something smarter about it.
3120 (defun tramp-handle-file-newer-than-file-p (file1 file2)
3121 "Like `file-newer-than-file-p' for Tramp files."
3122 (cond ((not (file-exists-p file1))
3123 nil)
3124 ((not (file-exists-p file2))
3125 t)
3126 ;; We are sure both files exist at this point.
3127 (t
3128 (save-excursion
3129 ;; We try to get the mtime of both files. If they are not
3130 ;; equal to the "dont-know" value, then we subtract the times
3131 ;; and obtain the result.
3132 (let ((fa1 (file-attributes file1))
3133 (fa2 (file-attributes file2)))
3134 (if (and (not (equal (nth 5 fa1) '(0 0)))
3135 (not (equal (nth 5 fa2) '(0 0))))
3136 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
3137 ;; If one of them is the dont-know value, then we can
3138 ;; still try to run a shell command on the remote host.
3139 ;; However, this only works if both files are Tramp
3140 ;; files and both have the same method, same user, same
3141 ;; host.
3142 (unless (tramp-equal-remote file1 file2)
3143 (with-parsed-tramp-file-name
3144 (if (tramp-tramp-file-p file1) file1 file2) nil
3145 (tramp-error
3146 v 'file-error
3147 "Files %s and %s must have same method, user, host"
3148 file1 file2)))
3149 (with-parsed-tramp-file-name file1 nil
3150 (zerop (tramp-run-test2
3151 (tramp-get-test-nt-command v) file1 file2)))))))))
3152
3153 ;; Functions implemented using the basic functions above.
3154
3155 (defun tramp-handle-file-modes (filename)
3156 "Like `file-modes' for Tramp files."
3157 (let ((truename (or (file-truename filename) filename)))
3158 (when (file-exists-p truename)
3159 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
3160
3161 (defun tramp-default-file-modes (filename)
3162 "Return file modes of FILENAME as integer.
3163 If the file modes of FILENAME cannot be determined, return the
3164 value of `default-file-modes', without execute permissions."
3165 (or (file-modes filename)
3166 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
3167
3168 (defun tramp-handle-file-directory-p (filename)
3169 "Like `file-directory-p' for Tramp files."
3170 ;; Care must be taken that this function returns `t' for symlinks
3171 ;; pointing to directories. Surely the most obvious implementation
3172 ;; would be `test -d', but that returns false for such symlinks.
3173 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3174 ;; I now think he's right. So we could be using `test -d', couldn't
3175 ;; we?
3176 ;;
3177 ;; Alternatives: `cd %s', `test -d %s'
3178 (with-parsed-tramp-file-name filename nil
3179 (with-file-property v localname "file-directory-p"
3180 (zerop (tramp-run-test "-d" filename)))))
3181
3182 (defun tramp-handle-file-regular-p (filename)
3183 "Like `file-regular-p' for Tramp files."
3184 (and (file-exists-p filename)
3185 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
3186
3187 (defun tramp-handle-file-symlink-p (filename)
3188 "Like `file-symlink-p' for Tramp files."
3189 (with-parsed-tramp-file-name filename nil
3190 (let ((x (car (file-attributes filename))))
3191 (when (stringp x)
3192 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3193 ;; might do weird things.
3194 (if (file-name-absolute-p x)
3195 (tramp-make-tramp-file-name method user host x)
3196 x)))))
3197
3198 (defun tramp-handle-file-writable-p (filename)
3199 "Like `file-writable-p' for Tramp files."
3200 (with-parsed-tramp-file-name filename nil
3201 (with-file-property v localname "file-writable-p"
3202 (if (file-exists-p filename)
3203 ;; Examine `file-attributes' cache to see if request can be
3204 ;; satisfied without remote operation.
3205 (or (tramp-check-cached-permissions v ?w)
3206 (zerop (tramp-run-test "-w" filename)))
3207 ;; If file doesn't exist, check if directory is writable.
3208 (and (zerop (tramp-run-test
3209 "-d" (file-name-directory filename)))
3210 (zerop (tramp-run-test
3211 "-w" (file-name-directory filename))))))))
3212
3213 (defun tramp-handle-file-ownership-preserved-p (filename)
3214 "Like `file-ownership-preserved-p' for Tramp files."
3215 (with-parsed-tramp-file-name filename nil
3216 (with-file-property v localname "file-ownership-preserved-p"
3217 (let ((attributes (file-attributes filename)))
3218 ;; Return t if the file doesn't exist, since it's true that no
3219 ;; information would be lost by an (attempted) delete and create.
3220 (or (null attributes)
3221 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
3222
3223 ;; Other file name ops.
3224
3225 (defun tramp-handle-directory-file-name (directory)
3226 "Like `directory-file-name' for Tramp files."
3227 ;; If localname component of filename is "/", leave it unchanged.
3228 ;; Otherwise, remove any trailing slash from localname component.
3229 ;; Method, host, etc, are unchanged. Does it make sense to try
3230 ;; to avoid parsing the filename?
3231 (with-parsed-tramp-file-name directory nil
3232 (if (and (not (zerop (length localname)))
3233 (eq (aref localname (1- (length localname))) ?/)
3234 (not (string= localname "/")))
3235 (substring directory 0 -1)
3236 directory)))
3237
3238 ;; Directory listings.
3239
3240 (defun tramp-handle-directory-files
3241 (directory &optional full match nosort files-only)
3242 "Like `directory-files' for Tramp files."
3243 ;; FILES-ONLY is valid for XEmacs only.
3244 (when (file-directory-p directory)
3245 (setq directory (file-name-as-directory (expand-file-name directory)))
3246 (let ((temp (nreverse (file-name-all-completions "" directory)))
3247 result item)
3248
3249 (while temp
3250 (setq item (directory-file-name (pop temp)))
3251 (when (and (or (null match) (string-match match item))
3252 (or (null files-only)
3253 ;; Files only.
3254 (and (equal files-only t) (file-regular-p item))
3255 ;; Directories only.
3256 (file-directory-p item)))
3257 (push (if full (concat directory item) item)
3258 result)))
3259 (if nosort result (sort result 'string<)))))
3260
3261 (defun tramp-handle-directory-files-and-attributes
3262 (directory &optional full match nosort id-format)
3263 "Like `directory-files-and-attributes' for Tramp files."
3264 (unless id-format (setq id-format 'integer))
3265 (when (file-directory-p directory)
3266 (setq directory (expand-file-name directory))
3267 (let* ((temp
3268 (copy-tree
3269 (with-parsed-tramp-file-name directory nil
3270 (with-file-property
3271 v localname
3272 (format "directory-files-and-attributes-%s" id-format)
3273 (save-excursion
3274 (mapcar
3275 (lambda (x)
3276 (cons (car x)
3277 (tramp-convert-file-attributes v (cdr x))))
3278 (cond
3279 ((tramp-get-remote-stat v)
3280 (tramp-do-directory-files-and-attributes-with-stat
3281 v localname id-format))
3282 ((tramp-get-remote-perl v)
3283 (tramp-do-directory-files-and-attributes-with-perl
3284 v localname id-format)))))))))
3285 result item)
3286
3287 (while temp
3288 (setq item (pop temp))
3289 (when (or (null match) (string-match match (car item)))
3290 (when full
3291 (setcar item (expand-file-name (car item) directory)))
3292 (push item result)))
3293
3294 (if nosort
3295 result
3296 (sort result (lambda (x y) (string< (car x) (car y))))))))
3297
3298 (defun tramp-do-directory-files-and-attributes-with-perl
3299 (vec localname &optional id-format)
3300 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3301 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3302 (tramp-maybe-send-script
3303 vec tramp-perl-directory-files-and-attributes
3304 "tramp_perl_directory_files_and_attributes")
3305 (let ((object
3306 (tramp-send-command-and-read
3307 vec
3308 (format "tramp_perl_directory_files_and_attributes %s %s"
3309 (tramp-shell-quote-argument localname) id-format))))
3310 (when (stringp object) (tramp-error vec 'file-error object))
3311 object))
3312
3313 (defun tramp-do-directory-files-and-attributes-with-stat
3314 (vec localname &optional id-format)
3315 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3316 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3317 (tramp-send-command-and-read
3318 vec
3319 (format
3320 (concat
3321 ;; We must care about filenames with spaces, or starting with
3322 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3323 ;; but it does not work on all remote systems. Therefore, we
3324 ;; quote the filenames via sed.
3325 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
3326 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
3327 "echo \")\"")
3328 (tramp-shell-quote-argument localname)
3329 (tramp-get-ls-command vec)
3330 (tramp-get-remote-stat vec)
3331 (if (eq id-format 'integer) "%u" "\"%U\"")
3332 (if (eq id-format 'integer) "%g" "\"%G\""))))
3333
3334 ;; This function should return "foo/" for directories and "bar" for
3335 ;; files.
3336 (defun tramp-handle-file-name-all-completions (filename directory)
3337 "Like `file-name-all-completions' for Tramp files."
3338 (unless (save-match-data (string-match "/" filename))
3339 (with-parsed-tramp-file-name (expand-file-name directory) nil
3340
3341 (all-completions
3342 filename
3343 (mapcar
3344 'list
3345 (or
3346 ;; Try cache first
3347 (and
3348 ;; Ignore if expired
3349 (or (not (integerp tramp-completion-reread-directory-timeout))
3350 (<= (tramp-time-diff
3351 (current-time)
3352 (tramp-get-file-property
3353 v localname "last-completion" '(0 0 0)))
3354 tramp-completion-reread-directory-timeout))
3355
3356 ;; Try cache entries for filename, filename with last
3357 ;; character removed, filename with last two characters
3358 ;; removed, ..., and finally the empty string - all
3359 ;; concatenated to the local directory name
3360
3361 ;; This is inefficient for very long filenames, pity
3362 ;; `reduce' is not available...
3363 (car
3364 (apply
3365 'append
3366 (mapcar
3367 (lambda (x)
3368 (let ((cache-hit
3369 (tramp-get-file-property
3370 v
3371 (concat localname (substring filename 0 x))
3372 "file-name-all-completions"
3373 nil)))
3374 (when cache-hit (list cache-hit))))
3375 (tramp-compat-number-sequence (length filename) 0 -1)))))
3376
3377 ;; Cache expired or no matching cache entry found so we need
3378 ;; to perform a remote operation
3379 (let (result)
3380 ;; Get a list of directories and files, including reliably
3381 ;; tagging the directories with a trailing '/'. Because I
3382 ;; rock. --daniel@danann.net
3383
3384 ;; Changed to perform `cd' in the same remote op and only
3385 ;; get entries starting with `filename'. Capture any `cd'
3386 ;; error messages. Ensure any `cd' and `echo' aliases are
3387 ;; ignored.
3388 (tramp-send-command
3389 v
3390 (if (tramp-get-remote-perl v)
3391 (progn
3392 (tramp-maybe-send-script
3393 v tramp-perl-file-name-all-completions
3394 "tramp_perl_file_name_all_completions")
3395 (format "tramp_perl_file_name_all_completions %s %s %d"
3396 (tramp-shell-quote-argument localname)
3397 (tramp-shell-quote-argument filename)
3398 (if (symbol-value
3399 ;; `read-file-name-completion-ignore-case'
3400 ;; is introduced with Emacs 22.1.
3401 (if (boundp
3402 'read-file-name-completion-ignore-case)
3403 'read-file-name-completion-ignore-case
3404 'completion-ignore-case))
3405 1 0)))
3406
3407 (format (concat
3408 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3409 ;; `ls' with wildcard might fail with `Argument
3410 ;; list too long' error in some corner cases; if
3411 ;; `ls' fails after `cd' succeeded, chances are
3412 ;; that's the case, so let's retry without
3413 ;; wildcard. This will return "too many" entries
3414 ;; but that isn't harmful.
3415 " || %s -a 2>/dev/null)"
3416 " | while read f; do"
3417 " if %s -d \"$f\" 2>/dev/null;"
3418 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3419 " && \\echo ok) || \\echo fail")
3420 (tramp-shell-quote-argument localname)
3421 (tramp-get-ls-command v)
3422 ;; When `filename' is empty, just `ls' without
3423 ;; filename argument is more efficient than `ls *'
3424 ;; for very large directories and might avoid the
3425 ;; `Argument list too long' error.
3426 ;;
3427 ;; With and only with wildcard, we need to add
3428 ;; `-d' to prevent `ls' from descending into
3429 ;; sub-directories.
3430 (if (zerop (length filename))
3431 "."
3432 (concat (tramp-shell-quote-argument filename) "* -d"))
3433 (tramp-get-ls-command v)
3434 (tramp-get-test-command v))))
3435
3436 ;; Now grab the output.
3437 (with-current-buffer (tramp-get-buffer v)
3438 (goto-char (point-max))
3439
3440 ;; Check result code, found in last line of output
3441 (forward-line -1)
3442 (if (looking-at "^fail$")
3443 (progn
3444 ;; Grab error message from line before last line
3445 ;; (it was put there by `cd 2>&1')
3446 (forward-line -1)
3447 (tramp-error
3448 v 'file-error
3449 "tramp-handle-file-name-all-completions: %s"
3450 (buffer-substring
3451 (point) (tramp-compat-line-end-position))))
3452 ;; For peace of mind, if buffer doesn't end in `fail'
3453 ;; then it should end in `ok'. If neither are in the
3454 ;; buffer something went seriously wrong on the remote
3455 ;; side.
3456 (unless (looking-at "^ok$")
3457 (tramp-error
3458 v 'file-error
3459 "\
3460 tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3461 (tramp-shell-quote-argument localname) (buffer-string))))
3462
3463 (while (zerop (forward-line -1))
3464 (push (buffer-substring
3465 (point) (tramp-compat-line-end-position))
3466 result)))
3467
3468 ;; Because the remote op went through OK we know the
3469 ;; directory we `cd'-ed to exists
3470 (tramp-set-file-property
3471 v localname "file-exists-p" t)
3472
3473 ;; Because the remote op went through OK we know every
3474 ;; file listed by `ls' exists.
3475 (mapc (lambda (entry)
3476 (tramp-set-file-property
3477 v (concat localname entry) "file-exists-p" t))
3478 result)
3479
3480 (tramp-set-file-property
3481 v localname "last-completion" (current-time))
3482
3483 ;; Store result in the cache
3484 (tramp-set-file-property
3485 v (concat localname filename)
3486 "file-name-all-completions"
3487 result))))))))
3488
3489 (defun tramp-handle-file-name-completion
3490 (filename directory &optional predicate)
3491 "Like `file-name-completion' for Tramp files."
3492 (unless (tramp-tramp-file-p directory)
3493 (error
3494 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3495 directory))
3496 (try-completion
3497 filename
3498 (mapcar 'list (file-name-all-completions filename directory))
3499 (when predicate
3500 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
3501
3502 ;; cp, mv and ln
3503
3504 (defun tramp-handle-add-name-to-file
3505 (filename newname &optional ok-if-already-exists)
3506 "Like `add-name-to-file' for Tramp files."
3507 (unless (tramp-equal-remote filename newname)
3508 (with-parsed-tramp-file-name
3509 (if (tramp-tramp-file-p filename) filename newname) nil
3510 (tramp-error
3511 v 'file-error
3512 "add-name-to-file: %s"
3513 "only implemented for same method, same user, same host")))
3514 (with-parsed-tramp-file-name filename v1
3515 (with-parsed-tramp-file-name newname v2
3516 (let ((ln (when v1 (tramp-get-remote-ln v1))))
3517 (when (and (not ok-if-already-exists)
3518 (file-exists-p newname)
3519 (not (numberp ok-if-already-exists))
3520 (y-or-n-p
3521 (format
3522 "File %s already exists; make it a new name anyway? "
3523 newname)))
3524 (tramp-error
3525 v2 'file-error
3526 "add-name-to-file: file %s already exists" newname))
3527 (tramp-flush-file-property v2 (file-name-directory v2-localname))
3528 (tramp-flush-file-property v2 v2-localname)
3529 (tramp-barf-unless-okay
3530 v1
3531 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3532 (tramp-shell-quote-argument v2-localname))
3533 "error with add-name-to-file, see buffer `%s' for details"
3534 (buffer-name))))))
3535
3536 (defun tramp-handle-copy-file
3537 (filename newname &optional ok-if-already-exists keep-date
3538 preserve-uid-gid preserve-selinux-context)
3539 "Like `copy-file' for Tramp files."
3540 (setq filename (expand-file-name filename))
3541 (setq newname (expand-file-name newname))
3542 (cond
3543 ;; At least one file a Tramp file?
3544 ((or (tramp-tramp-file-p filename)
3545 (tramp-tramp-file-p newname))
3546 (tramp-do-copy-or-rename-file
3547 'copy filename newname ok-if-already-exists keep-date
3548 preserve-uid-gid preserve-selinux-context))
3549 ;; Compat section.
3550 (preserve-selinux-context
3551 (tramp-run-real-handler
3552 'copy-file
3553 (list filename newname ok-if-already-exists keep-date
3554 preserve-uid-gid preserve-selinux-context)))
3555 (preserve-uid-gid
3556 (tramp-run-real-handler
3557 'copy-file
3558 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3559 (t
3560 (tramp-run-real-handler
3561 'copy-file (list filename newname ok-if-already-exists keep-date)))))
3562
3563 (defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3564 "Like `copy-directory' for Tramp files."
3565 (let ((t1 (tramp-tramp-file-p dirname))
3566 (t2 (tramp-tramp-file-p newname)))
3567 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3568 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3569 ;; When DIRNAME and NEWNAME are remote, they must have
3570 ;; the same method.
3571 (or (null t1) (null t2)
3572 (string-equal
3573 (tramp-file-name-method (tramp-dissect-file-name dirname))
3574 (tramp-file-name-method (tramp-dissect-file-name newname)))))
3575 ;; scp or rsync DTRT.
3576 (progn
3577 (setq dirname (directory-file-name (expand-file-name dirname))
3578 newname (directory-file-name (expand-file-name newname)))
3579 (if (and (file-directory-p newname)
3580 (not (string-equal (file-name-nondirectory dirname)
3581 (file-name-nondirectory newname))))
3582 (setq newname
3583 (expand-file-name
3584 (file-name-nondirectory dirname) newname)))
3585 (if (not (file-directory-p (file-name-directory newname)))
3586 (make-directory (file-name-directory newname) parents))
3587 (tramp-do-copy-or-rename-file-out-of-band
3588 'copy dirname newname keep-date))
3589 ;; We must do it file-wise.
3590 (tramp-run-real-handler
3591 'copy-directory (list dirname newname keep-date parents)))
3592
3593 ;; When newname did exist, we have wrong cached values.
3594 (when t2
3595 (with-parsed-tramp-file-name newname nil
3596 (tramp-flush-file-property v (file-name-directory localname))
3597 (tramp-flush-file-property v localname))))))
3598
3599 (defun tramp-handle-rename-file
3600 (filename newname &optional ok-if-already-exists)
3601 "Like `rename-file' for Tramp files."
3602 ;; Check if both files are local -- invoke normal rename-file.
3603 ;; Otherwise, use Tramp from local system.
3604 (setq filename (expand-file-name filename))
3605 (setq newname (expand-file-name newname))
3606 ;; At least one file a Tramp file?
3607 (if (or (tramp-tramp-file-p filename)
3608 (tramp-tramp-file-p newname))
3609 (tramp-do-copy-or-rename-file
3610 'rename filename newname ok-if-already-exists t t)
3611 (tramp-run-real-handler
3612 'rename-file (list filename newname ok-if-already-exists))))
3613
3614 (defun tramp-do-copy-or-rename-file
3615 (op filename newname &optional ok-if-already-exists keep-date
3616 preserve-uid-gid preserve-selinux-context)
3617 "Copy or rename a remote file.
3618 OP must be `copy' or `rename' and indicates the operation to perform.
3619 FILENAME specifies the file to copy or rename, NEWNAME is the name of
3620 the new file (for copy) or the new name of the file (for rename).
3621 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3622 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3623 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3624 the uid and gid if both files are on the same host.
3625 PRESERVE-SELINUX-CONTEXT activates selinux commands.
3626
3627 This function is invoked by `tramp-handle-copy-file' and
3628 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3629 and `rename'. FILENAME and NEWNAME must be absolute file names."
3630 (unless (memq op '(copy rename))
3631 (error "Unknown operation `%s', must be `copy' or `rename'" op))
3632 (let ((t1 (tramp-tramp-file-p filename))
3633 (t2 (tramp-tramp-file-p newname))
3634 (context (and preserve-selinux-context
3635 (apply 'file-selinux-context (list filename))))
3636 pr tm)
3637
3638 (when (and (not ok-if-already-exists) (file-exists-p newname))
3639 (with-parsed-tramp-file-name (if t1 filename newname) nil
3640 (tramp-error
3641 v 'file-already-exists "File %s already exists" newname)))
3642
3643 (with-parsed-tramp-file-name (if t1 filename newname) nil
3644 (with-progress-reporter
3645 v 0 (format "Transferring %s to %s" filename newname)
3646
3647 (cond
3648 ;; Both are Tramp files.
3649 ((and t1 t2)
3650 (with-parsed-tramp-file-name filename v1
3651 (with-parsed-tramp-file-name newname v2
3652 (cond
3653 ;; Shortcut: if method, host, user are the same for both
3654 ;; files, we invoke `cp' or `mv' on the remote host
3655 ;; directly.
3656 ((tramp-equal-remote filename newname)
3657 (tramp-do-copy-or-rename-file-directly
3658 op filename newname
3659 ok-if-already-exists keep-date preserve-uid-gid))
3660
3661 ;; Try out-of-band operation.
3662 ((tramp-method-out-of-band-p
3663 v1 (nth 7 (file-attributes filename)))
3664 (tramp-do-copy-or-rename-file-out-of-band
3665 op filename newname keep-date))
3666
3667 ;; No shortcut was possible. So we copy the
3668 ;; file first. If the operation was `rename', we go
3669 ;; back and delete the original file (if the copy was
3670 ;; successful). The approach is simple-minded: we
3671 ;; create a new buffer, insert the contents of the
3672 ;; source file into it, then write out the buffer to
3673 ;; the target file. The advantage is that it doesn't
3674 ;; matter which filename handlers are used for the
3675 ;; source and target file.
3676 (t
3677 (tramp-do-copy-or-rename-file-via-buffer
3678 op filename newname keep-date))))))
3679
3680 ;; One file is a Tramp file, the other one is local.
3681 ((or t1 t2)
3682 (cond
3683 ;; Fast track on local machine.
3684 ((tramp-local-host-p v)
3685 (tramp-do-copy-or-rename-file-directly
3686 op filename newname
3687 ok-if-already-exists keep-date preserve-uid-gid))
3688
3689 ;; If the Tramp file has an out-of-band method, the corresponding
3690 ;; copy-program can be invoked.
3691 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3692 (tramp-do-copy-or-rename-file-out-of-band
3693 op filename newname keep-date))
3694
3695 ;; Use the inline method via a Tramp buffer.
3696 (t (tramp-do-copy-or-rename-file-via-buffer
3697 op filename newname keep-date))))
3698
3699 (t
3700 ;; One of them must be a Tramp file.
3701 (error "Tramp implementation says this cannot happen")))
3702
3703 ;; Handle `preserve-selinux-context'.
3704 (when context (apply 'set-file-selinux-context (list newname context)))
3705
3706 ;; In case of `rename', we must flush the cache of the source file.
3707 (when (and t1 (eq op 'rename))
3708 (with-parsed-tramp-file-name filename v1
3709 (tramp-flush-file-property v1 (file-name-directory localname))
3710 (tramp-flush-file-property v1 localname)))
3711
3712 ;; When newname did exist, we have wrong cached values.
3713 (when t2
3714 (with-parsed-tramp-file-name newname v2
3715 (tramp-flush-file-property v2 (file-name-directory localname))
3716 (tramp-flush-file-property v2 localname)))))))
3717
3718 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3719 "Use an Emacs buffer to copy or rename a file.
3720 First arg OP is either `copy' or `rename' and indicates the operation.
3721 FILENAME is the source file, NEWNAME the target file.
3722 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3723 (with-temp-buffer
3724 ;; We must disable multibyte, because binary data shall not be
3725 ;; converted.
3726 (set-buffer-multibyte nil)
3727 (let ((coding-system-for-read 'binary)
3728 (jka-compr-inhibit t))
3729 (insert-file-contents-literally filename))
3730 ;; We don't want the target file to be compressed, so we let-bind
3731 ;; `jka-compr-inhibit' to t.
3732 (let ((coding-system-for-write 'binary)
3733 (jka-compr-inhibit t))
3734 (write-region (point-min) (point-max) newname)))
3735 ;; KEEP-DATE handling.
3736 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3737 ;; Set the mode.
3738 (set-file-modes newname (tramp-default-file-modes filename))
3739 ;; If the operation was `rename', delete the original file.
3740 (unless (eq op 'copy) (tramp-compat-delete-file filename 'force)))
3741
3742 (defun tramp-do-copy-or-rename-file-directly
3743 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
3744 "Invokes `cp' or `mv' on the remote system.
3745 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3746 respectively. FILENAME specifies the file to copy or rename,
3747 NEWNAME is the name of the new file (for copy) or the new name of
3748 the file (for rename). Both files must reside on the same host.
3749 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3750 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3751 the uid and gid from FILENAME."
3752 (let ((t1 (tramp-tramp-file-p filename))
3753 (t2 (tramp-tramp-file-p newname))
3754 (file-times (nth 5 (file-attributes filename)))
3755 (file-modes (tramp-default-file-modes filename)))
3756 (with-parsed-tramp-file-name (if t1 filename newname) nil
3757 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3758 ((eq op 'copy) "cp -f")
3759 ((eq op 'rename) "mv -f")
3760 (t (tramp-error
3761 v 'file-error
3762 "Unknown operation `%s', must be `copy' or `rename'"
3763 op))))
3764 (localname1
3765 (if t1
3766 (tramp-file-name-handler 'file-remote-p filename 'localname)
3767 filename))
3768 (localname2
3769 (if t2
3770 (tramp-file-name-handler 'file-remote-p newname 'localname)
3771 newname))
3772 (prefix (file-remote-p (if t1 filename newname)))
3773 cmd-result)
3774
3775 (cond
3776 ;; Both files are on a remote host, with same user.
3777 ((and t1 t2)
3778 (setq cmd-result
3779 (tramp-send-command-and-check
3780 v
3781 (format "%s %s %s" cmd
3782 (tramp-shell-quote-argument localname1)
3783 (tramp-shell-quote-argument localname2))))
3784 (with-current-buffer (tramp-get-buffer v)
3785 (goto-char (point-min))
3786 (unless
3787 (or
3788 (and keep-date
3789 ;; Mask cp -f error.
3790 (re-search-forward
3791 tramp-operation-not-permitted-regexp nil t))
3792 (zerop cmd-result))
3793 (tramp-error-with-buffer
3794 nil v 'file-error
3795 "Copying directly failed, see buffer `%s' for details."
3796 (buffer-name)))))
3797
3798 ;; We are on the local host.
3799 ((or t1 t2)
3800 (cond
3801 ;; We can do it directly.
3802 ((let (file-name-handler-alist)
3803 (and (file-readable-p localname1)
3804 (file-writable-p (file-name-directory localname2))
3805 (or (file-directory-p localname2)
3806 (file-writable-p localname2))))
3807 (if (eq op 'copy)
3808 (tramp-compat-copy-file
3809 localname1 localname2 ok-if-already-exists
3810 keep-date preserve-uid-gid)
3811 (tramp-run-real-handler
3812 'rename-file (list localname1 localname2 ok-if-already-exists))))
3813
3814 ;; We can do it directly with `tramp-send-command'
3815 ((and (file-readable-p (concat prefix localname1))
3816 (file-writable-p
3817 (file-name-directory (concat prefix localname2)))
3818 (or (file-directory-p (concat prefix localname2))
3819 (file-writable-p (concat prefix localname2))))
3820 (tramp-do-copy-or-rename-file-directly
3821 op (concat prefix localname1) (concat prefix localname2)
3822 ok-if-already-exists keep-date t)
3823 ;; We must change the ownership to the local user.
3824 (tramp-set-file-uid-gid
3825 (concat prefix localname2)
3826 (tramp-get-local-uid 'integer)
3827 (tramp-get-local-gid 'integer)))
3828
3829 ;; We need a temporary file in between.
3830 (t
3831 ;; Create the temporary file.
3832 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
3833 (unwind-protect
3834 (progn
3835 (cond
3836 (t1
3837 (or
3838 (zerop
3839 (tramp-send-command-and-check
3840 v (format
3841 "%s %s %s" cmd
3842 (tramp-shell-quote-argument localname1)
3843 (tramp-shell-quote-argument tmpfile))))
3844 (tramp-error-with-buffer
3845 nil v 'file-error
3846 "Copying directly failed, see buffer `%s' for details."
3847 (tramp-get-buffer v)))
3848 ;; We must change the ownership as remote user.
3849 ;; Since this does not work reliable, we also
3850 ;; give read permissions.
3851 (set-file-modes
3852 (concat prefix tmpfile) (tramp-octal-to-decimal "0777"))
3853 (tramp-set-file-uid-gid
3854 (concat prefix tmpfile)
3855 (tramp-get-local-uid 'integer)
3856 (tramp-get-local-gid 'integer)))
3857 (t2
3858 (if (eq op 'copy)
3859 (tramp-compat-copy-file
3860 localname1 tmpfile t
3861 keep-date preserve-uid-gid)
3862 (tramp-run-real-handler
3863 'rename-file
3864 (list localname1 tmpfile t)))
3865 ;; We must change the ownership as local user.
3866 ;; Since this does not work reliable, we also
3867 ;; give read permissions.
3868 (set-file-modes tmpfile (tramp-octal-to-decimal "0777"))
3869 (tramp-set-file-uid-gid
3870 tmpfile
3871 (tramp-get-remote-uid v 'integer)
3872 (tramp-get-remote-gid v 'integer))))
3873
3874 ;; Move the temporary file to its destination.
3875 (cond
3876 (t2
3877 (or
3878 (zerop
3879 (tramp-send-command-and-check
3880 v (format
3881 "cp -f -p %s %s"
3882 (tramp-shell-quote-argument tmpfile)
3883 (tramp-shell-quote-argument localname2))))
3884 (tramp-error-with-buffer
3885 nil v 'file-error
3886 "Copying directly failed, see buffer `%s' for details."
3887 (tramp-get-buffer v))))
3888 (t1
3889 (tramp-run-real-handler
3890 'rename-file
3891 (list tmpfile localname2 ok-if-already-exists)))))
3892
3893 ;; Save exit.
3894 (condition-case nil
3895 (tramp-compat-delete-file tmpfile 'force)
3896 (error)))))))))
3897
3898 ;; Set the time and mode. Mask possible errors.
3899 (condition-case nil
3900 (when keep-date
3901 (set-file-times newname file-times)
3902 (set-file-modes newname file-modes))
3903 (error)))))
3904
3905 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3906 "Invoke rcp program to copy.
3907 The method used must be an out-of-band method."
3908 (let ((t1 (tramp-tramp-file-p filename))
3909 (t2 (tramp-tramp-file-p newname))
3910 copy-program copy-args copy-env copy-keep-date port spec
3911 source target)
3912
3913 (with-parsed-tramp-file-name (if t1 filename newname) nil
3914 (if (and t1 t2)
3915
3916 ;; Both are Tramp files. We shall optimize it, when the
3917 ;; methods for filename and newname are the same.
3918 (let* ((dir-flag (file-directory-p filename))
3919 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
3920 (if dir-flag
3921 (setq tmpfile
3922 (expand-file-name
3923 (file-name-nondirectory newname) tmpfile)))
3924 (unwind-protect
3925 (progn
3926 (tramp-do-copy-or-rename-file-out-of-band
3927 op filename tmpfile keep-date)
3928 (tramp-do-copy-or-rename-file-out-of-band
3929 'rename tmpfile newname keep-date))
3930 ;; Save exit.
3931 (condition-case nil
3932 (if dir-flag
3933 (tramp-compat-delete-directory
3934 (expand-file-name ".." tmpfile) 'recursive)
3935 (tramp-compat-delete-file tmpfile 'force))
3936 (error))))
3937
3938 ;; Expand hops. Might be necessary for gateway methods.
3939 (setq v (car (tramp-compute-multi-hops v)))
3940 (aset v 3 localname)
3941
3942 ;; Check which ones of source and target are Tramp files.
3943 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
3944 target (funcall
3945 (if (and (file-directory-p filename)
3946 (string-equal
3947 (file-name-nondirectory filename)
3948 (file-name-nondirectory newname)))
3949 'file-name-directory
3950 'identity)
3951 (if t2 (tramp-make-copy-program-file-name v) newname)))
3952
3953 ;; Check for port number. Until now, there's no need for handling
3954 ;; like method, user, host.
3955 (setq host (tramp-file-name-real-host v)
3956 port (tramp-file-name-port v)
3957 port (or (and port (number-to-string port)) ""))
3958
3959 ;; Compose copy command.
3960 (setq spec (format-spec-make
3961 ?h host ?u user ?p port
3962 ?t (tramp-get-connection-property
3963 (tramp-get-connection-process v) "temp-file" "")
3964 ?k (if keep-date " " ""))
3965 copy-program (tramp-get-method-parameter
3966 method 'tramp-copy-program)
3967 copy-keep-date (tramp-get-method-parameter
3968 method 'tramp-copy-keep-date)
3969 copy-args
3970 (delq
3971 nil
3972 (mapcar
3973 (lambda (x)
3974 (setq
3975 x
3976 ;; " " is indication for keep-date argument.
3977 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
3978 (unless (member "" x) (mapconcat 'identity x " ")))
3979 (tramp-get-method-parameter method 'tramp-copy-args)))
3980 copy-env
3981 (delq
3982 nil
3983 (mapcar
3984 (lambda (x)
3985 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
3986 (unless (member "" x) (mapconcat 'identity x " ")))
3987 (tramp-get-method-parameter method 'tramp-copy-env))))
3988
3989 ;; Check for program.
3990 (when (and (fboundp 'executable-find)
3991 (not (let ((default-directory
3992 (tramp-compat-temporary-file-directory)))
3993 (executable-find copy-program))))
3994 (tramp-error
3995 v 'file-error "Cannot find copy program: %s" copy-program))
3996
3997 ;; Set variables for computing the prompt for reading
3998 ;; password.
3999 (setq tramp-current-method (tramp-file-name-method v)
4000 tramp-current-user (tramp-file-name-user v)
4001 tramp-current-host (tramp-file-name-host v))
4002
4003 (unwind-protect
4004 (with-temp-buffer
4005 ;; The default directory must be remote.
4006 (let ((default-directory
4007 (file-name-directory (if t1 filename newname)))
4008 (process-environment (copy-sequence process-environment)))
4009 ;; Set the transfer process properties.
4010 (tramp-set-connection-property
4011 v "process-name" (buffer-name (current-buffer)))
4012 (tramp-set-connection-property
4013 v "process-buffer" (current-buffer))
4014 (while copy-env
4015 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
4016 (setenv (pop copy-env) (pop copy-env)))
4017
4018 ;; Use an asynchronous process. By this, password can
4019 ;; be handled. The default directory must be local, in
4020 ;; order to apply the correct `copy-program'. We don't
4021 ;; set a timeout, because the copying of large files can
4022 ;; last longer than 60 secs.
4023 (let ((p (let ((default-directory
4024 (tramp-compat-temporary-file-directory)))
4025 (apply 'start-process
4026 (tramp-get-connection-property
4027 v "process-name" nil)
4028 (tramp-get-connection-property
4029 v "process-buffer" nil)
4030 copy-program
4031 (append copy-args (list source target))))))
4032 (tramp-message
4033 v 6 "%s" (mapconcat 'identity (process-command p) " "))
4034 (tramp-set-process-query-on-exit-flag p nil)
4035 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
4036
4037 ;; Reset the transfer process properties.
4038 (tramp-set-connection-property v "process-name" nil)
4039 (tramp-set-connection-property v "process-buffer" nil))
4040
4041 ;; Handle KEEP-DATE argument.
4042 (when (and keep-date (not copy-keep-date))
4043 (set-file-times newname (nth 5 (file-attributes filename))))
4044
4045 ;; Set the mode.
4046 (unless (and keep-date copy-keep-date)
4047 (ignore-errors
4048 (set-file-modes newname (tramp-default-file-modes filename)))))
4049
4050 ;; If the operation was `rename', delete the original file.
4051 (unless (eq op 'copy)
4052 (if (file-regular-p filename)
4053 (tramp-compat-delete-file filename 'force)
4054 (tramp-compat-delete-directory filename 'recursive))))))
4055
4056 (defun tramp-handle-make-directory (dir &optional parents)
4057 "Like `make-directory' for Tramp files."
4058 (setq dir (expand-file-name dir))
4059 (with-parsed-tramp-file-name dir nil
4060 (tramp-flush-directory-property v (file-name-directory localname))
4061 (save-excursion
4062 (tramp-barf-unless-okay
4063 v
4064 (format "%s %s"
4065 (if parents "mkdir -p" "mkdir")
4066 (tramp-shell-quote-argument localname))
4067 "Couldn't make directory %s" dir))))
4068
4069 (defun tramp-handle-delete-directory (directory &optional recursive)
4070 "Like `delete-directory' for Tramp files."
4071 (setq directory (expand-file-name directory))
4072 (with-parsed-tramp-file-name directory nil
4073 (tramp-flush-file-property v (file-name-directory localname))
4074 (tramp-flush-directory-property v localname)
4075 (unless (zerop (tramp-send-command-and-check
4076 v
4077 (format
4078 "%s %s"
4079 (if recursive "rm -rf" "rmdir")
4080 (tramp-shell-quote-argument localname))))
4081 (tramp-error v 'file-error "Couldn't delete %s" directory))))
4082
4083 (defun tramp-handle-delete-file (filename &optional force)
4084 "Like `delete-file' for Tramp files."
4085 (setq filename (expand-file-name filename))
4086 (with-parsed-tramp-file-name filename nil
4087 (tramp-flush-file-property v (file-name-directory localname))
4088 (tramp-flush-file-property v localname)
4089 (unless (zerop (tramp-send-command-and-check
4090 v
4091 (format "rm -f %s"
4092 (tramp-shell-quote-argument localname))))
4093 (tramp-error v 'file-error "Couldn't delete %s" filename))))
4094
4095 ;; Dired.
4096
4097 ;; CCC: This does not seem to be enough. Something dies when
4098 ;; we try and delete two directories under Tramp :/
4099 (defun tramp-handle-dired-recursive-delete-directory (filename)
4100 "Recursively delete the directory given.
4101 This is like `dired-recursive-delete-directory' for Tramp files."
4102 (with-parsed-tramp-file-name filename nil
4103 ;; Run a shell command 'rm -r <localname>'
4104 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
4105 (unless (file-exists-p filename)
4106 (tramp-error v 'file-error "No such directory: %s" filename))
4107 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
4108 (tramp-send-command
4109 v
4110 (format "rm -rf %s" (tramp-shell-quote-argument localname))
4111 ;; Don't read the output, do it explicitely.
4112 nil t)
4113 ;; Wait for the remote system to return to us...
4114 ;; This might take a while, allow it plenty of time.
4115 (tramp-wait-for-output (tramp-get-connection-process v) 120)
4116 ;; Make sure that it worked...
4117 (tramp-flush-file-property v (file-name-directory localname))
4118 (tramp-flush-directory-property v localname)
4119 (and (file-exists-p filename)
4120 (tramp-error
4121 v 'file-error "Failed to recursively delete %s" filename))))
4122
4123 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
4124 "Like `dired-compress-file' for Tramp files."
4125 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4126 ;; Code stolen mainly from dired-aux.el.
4127 (with-parsed-tramp-file-name file nil
4128 (tramp-flush-file-property v localname)
4129 (save-excursion
4130 (let ((suffixes
4131 (if (not (featurep 'xemacs))
4132 ;; Emacs case
4133 (symbol-value 'dired-compress-file-suffixes)
4134 ;; XEmacs has `dired-compression-method-alist', which is
4135 ;; transformed into `dired-compress-file-suffixes' structure.
4136 (mapcar
4137 (lambda (x)
4138 (list (concat (regexp-quote (nth 1 x)) "\\'")
4139 nil
4140 (mapconcat 'identity (nth 3 x) " ")))
4141 (symbol-value 'dired-compression-method-alist))))
4142 suffix)
4143 ;; See if any suffix rule matches this file name.
4144 (while suffixes
4145 (let (case-fold-search)
4146 (if (string-match (car (car suffixes)) localname)
4147 (setq suffix (car suffixes) suffixes nil))
4148 (setq suffixes (cdr suffixes))))
4149
4150 (cond ((file-symlink-p file)
4151 nil)
4152 ((and suffix (nth 2 suffix))
4153 ;; We found an uncompression rule.
4154 (with-progress-reporter v 0 (format "Uncompressing %s..." file)
4155 (when (zerop
4156 (tramp-send-command-and-check
4157 v (concat (nth 2 suffix) " "
4158 (tramp-shell-quote-argument localname))))
4159 ;; `dired-remove-file' is not defined in XEmacs
4160 (funcall (symbol-function 'dired-remove-file) file)
4161 (string-match (car suffix) file)
4162 (concat (substring file 0 (match-beginning 0))))))
4163 (t
4164 ;; We don't recognize the file as compressed, so compress it.
4165 ;; Try gzip.
4166 (with-progress-reporter v 0 (format "Compressing %s..." file)
4167 (when (zerop
4168 (tramp-send-command-and-check
4169 v (concat "gzip -f "
4170 (tramp-shell-quote-argument localname))))
4171 ;; `dired-remove-file' is not defined in XEmacs
4172 (funcall (symbol-function 'dired-remove-file) file)
4173 (cond ((file-exists-p (concat file ".gz"))
4174 (concat file ".gz"))
4175 ((file-exists-p (concat file ".z"))
4176 (concat file ".z"))
4177 (t nil))))))))))
4178
4179 (defun tramp-handle-dired-uncache (dir &optional dir-p)
4180 "Like `dired-uncache' for Tramp files."
4181 ;; DIR-P is valid for XEmacs only.
4182 (with-parsed-tramp-file-name
4183 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
4184 (tramp-flush-file-property v localname)))
4185
4186 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4187 ;; not sure at all that this is the right way to do it, but let's hope
4188 ;; it works for now, and wait for a guru to point out the Right Way to
4189 ;; achieve this.
4190 ;;(eval-when-compile
4191 ;; (unless (fboundp 'dired-insert-set-properties)
4192 ;; (fset 'dired-insert-set-properties 'ignore)))
4193 ;; Gerd suggests this:
4194 (eval-when-compile (require 'dired))
4195 ;; Note that dired is required at run-time, too, when it is needed.
4196 ;; It is only needed on XEmacs for the function
4197 ;; `dired-insert-set-properties'.
4198
4199 (defun tramp-handle-insert-directory
4200 (filename switches &optional wildcard full-directory-p)
4201 "Like `insert-directory' for Tramp files."
4202 (setq filename (expand-file-name filename))
4203 (with-parsed-tramp-file-name filename nil
4204 (if (and (featurep 'ls-lisp)
4205 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
4206 (tramp-run-real-handler
4207 'insert-directory (list filename switches wildcard full-directory-p))
4208 (when (stringp switches)
4209 (setq switches (split-string switches)))
4210 (when (and (member "--dired" switches)
4211 (not (tramp-get-ls-command-with-dired v)))
4212 (setq switches (delete "--dired" switches)))
4213 (when wildcard
4214 (setq wildcard (tramp-run-real-handler
4215 'file-name-nondirectory (list localname)))
4216 (setq localname (tramp-run-real-handler
4217 'file-name-directory (list localname))))
4218 (unless full-directory-p
4219 (setq switches (add-to-list 'switches "-d" 'append)))
4220 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
4221 (when wildcard
4222 (setq switches (concat switches " " wildcard)))
4223 (tramp-message
4224 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4225 switches filename (if wildcard "yes" "no")
4226 (if full-directory-p "yes" "no"))
4227 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4228 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4229 (if full-directory-p
4230 (tramp-send-command
4231 v
4232 (format "%s %s %s 2>/dev/null"
4233 (tramp-get-ls-command v)
4234 switches
4235 (if wildcard
4236 localname
4237 (tramp-shell-quote-argument (concat localname ".")))))
4238 (tramp-barf-unless-okay
4239 v
4240 (format "cd %s" (tramp-shell-quote-argument
4241 (tramp-run-real-handler
4242 'file-name-directory (list localname))))
4243 "Couldn't `cd %s'"
4244 (tramp-shell-quote-argument
4245 (tramp-run-real-handler 'file-name-directory (list localname))))
4246 (tramp-send-command
4247 v
4248 (format "%s %s %s"
4249 (tramp-get-ls-command v)
4250 switches
4251 (if (or wildcard
4252 (zerop (length
4253 (tramp-run-real-handler
4254 'file-name-nondirectory (list localname)))))
4255 ""
4256 (tramp-shell-quote-argument
4257 (tramp-run-real-handler
4258 'file-name-nondirectory (list localname)))))))
4259 (let ((beg (point)))
4260 ;; We cannot use `insert-buffer-substring' because the Tramp
4261 ;; buffer changes its contents before insertion due to calling
4262 ;; `expand-file' and alike.
4263 (insert
4264 (with-current-buffer (tramp-get-buffer v)
4265 (buffer-string)))
4266
4267 ;; Check for "--dired" output.
4268 (forward-line -2)
4269 (when (looking-at "//SUBDIRED//")
4270 (forward-line -1))
4271 (when (looking-at "//DIRED//")
4272 (let ((end (tramp-compat-line-end-position))
4273 (linebeg (point)))
4274 ;; Now read the numeric positions of file names.
4275 (goto-char linebeg)
4276 (forward-word 1)
4277 (forward-char 3)
4278 (while (< (point) end)
4279 (let ((start (+ beg (read (current-buffer))))
4280 (end (+ beg (read (current-buffer)))))
4281 (if (memq (char-after end) '(?\n ?\ ))
4282 ;; End is followed by \n or by " -> ".
4283 (put-text-property start end 'dired-filename t))))))
4284 ;; Remove trailing lines.
4285 (goto-char (tramp-compat-line-beginning-position))
4286 (while (looking-at "//")
4287 (forward-line 1)
4288 (delete-region (match-beginning 0) (point)))
4289
4290 ;; The inserted file could be from somewhere else.
4291 (when (and (not wildcard) (not full-directory-p))
4292 (goto-char (point-max))
4293 (when (file-symlink-p filename)
4294 (goto-char (search-backward "->" beg 'noerror)))
4295 (search-backward
4296 (if (zerop (length (file-name-nondirectory filename)))
4297 "."
4298 (file-name-nondirectory filename))
4299 beg 'noerror)
4300 (replace-match (file-relative-name filename) t))
4301
4302 (goto-char (point-max))))))
4303
4304 (defun tramp-handle-unhandled-file-name-directory (filename)
4305 "Like `unhandled-file-name-directory' for Tramp files."
4306 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4307 ;; for backward compatibility.
4308 (expand-file-name "~/"))
4309
4310 ;; Canonicalization of file names.
4311
4312 (defun tramp-handle-expand-file-name (name &optional dir)
4313 "Like `expand-file-name' for Tramp files.
4314 If the localname part of the given filename starts with \"/../\" then
4315 the result will be a local, non-Tramp, filename."
4316 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4317 (setq dir (or dir default-directory "/"))
4318 ;; Unless NAME is absolute, concat DIR and NAME.
4319 (unless (file-name-absolute-p name)
4320 (setq name (concat (file-name-as-directory dir) name)))
4321 ;; If NAME is not a Tramp file, run the real handler.
4322 (if (not (tramp-connectable-p name))
4323 (tramp-run-real-handler 'expand-file-name (list name nil))
4324 ;; Dissect NAME.
4325 (with-parsed-tramp-file-name name nil
4326 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
4327 (setq localname (concat "~/" localname)))
4328 ;; Tilde expansion if necessary. This needs a shell which
4329 ;; groks tilde expansion! The function `tramp-find-shell' is
4330 ;; supposed to find such a shell on the remote host. Please
4331 ;; tell me about it when this doesn't work on your system.
4332 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4333 (let ((uname (match-string 1 localname))
4334 (fname (match-string 2 localname)))
4335 ;; We cannot simply apply "~/", because under sudo "~/" is
4336 ;; expanded to the local user home directory but to the
4337 ;; root home directory. On the other hand, using always
4338 ;; the default user name for tilde expansion is not
4339 ;; appropriate either, because ssh and companions might
4340 ;; use a user name from the config file.
4341 (when (and (string-equal uname "~")
4342 (string-match "\\`su\\(do\\)?\\'" method))
4343 (setq uname (concat uname user)))
4344 (setq uname
4345 (with-connection-property v uname
4346 (tramp-send-command
4347 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
4348 (with-current-buffer (tramp-get-buffer v)
4349 (goto-char (point-min))
4350 (buffer-substring
4351 (point) (tramp-compat-line-end-position)))))
4352 (setq localname (concat uname fname))))
4353 ;; There might be a double slash, for example when "~/"
4354 ;; expands to "/". Remove this.
4355 (while (string-match "//" localname)
4356 (setq localname (replace-match "/" t t localname)))
4357 ;; No tilde characters in file name, do normal
4358 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4359 ;; `directory-sep-char' here for XEmacs on Windows, which would
4360 ;; otherwise use backslash. `default-directory' is bound,
4361 ;; because on Windows there would be problems with UNC shares or
4362 ;; Cygwin mounts.
4363 (let ((directory-sep-char ?/)
4364 (default-directory (tramp-compat-temporary-file-directory)))
4365 (tramp-make-tramp-file-name
4366 method user host
4367 (tramp-drop-volume-letter
4368 (tramp-run-real-handler
4369 'expand-file-name (list localname))))))))
4370
4371 (defun tramp-replace-environment-variables (filename)
4372 "Replace environment variables in FILENAME.
4373 Return the string with the replaced variables."
4374 (save-match-data
4375 (let ((idx (string-match "$\\(\\w+\\)" filename)))
4376 ;; `$' is coded as `$$'.
4377 (when (and idx
4378 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
4379 (getenv (match-string 1 filename)))
4380 (setq filename
4381 (replace-match
4382 (substitute-in-file-name (match-string 0 filename))
4383 t nil filename)))
4384 filename)))
4385
4386 (defun tramp-handle-substitute-in-file-name (filename)
4387 "Like `substitute-in-file-name' for Tramp files.
4388 \"//\" and \"/~\" substitute only in the local filename part.
4389 If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4390 beginning of local filename are not substituted."
4391 ;; First, we must replace environment variables.
4392 (setq filename (tramp-replace-environment-variables filename))
4393 (with-parsed-tramp-file-name filename nil
4394 (if (equal tramp-syntax 'url)
4395 ;; We need to check localname only. The other parts cannot contain
4396 ;; "//" or "/~".
4397 (if (and (> (length localname) 1)
4398 (or (string-match "//" localname)
4399 (string-match "/~" localname 1)))
4400 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4401 (tramp-make-tramp-file-name
4402 (when method (substitute-in-file-name method))
4403 (when user (substitute-in-file-name user))
4404 (when host (substitute-in-file-name host))
4405 (when localname
4406 (tramp-run-real-handler
4407 'substitute-in-file-name (list localname)))))
4408 ;; Ignore in LOCALNAME everything before "//" or "/~".
4409 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4410 (setq filename
4411 (concat (file-remote-p filename)
4412 (replace-match "\\1" nil nil localname)))
4413 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4414 (when (string-match "~$" filename)
4415 (setq filename (concat filename "/"))))
4416 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4417
4418 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4419 ;; which calls corresponding functions (see minibuf.el).
4420 (when (fboundp 'minibuffer-electric-separator)
4421 (mapc
4422 (lambda (x)
4423 (eval
4424 `(defadvice ,x
4425 (around ,(intern (format "tramp-advice-%s" x)) activate)
4426 "Invoke `substitute-in-file-name' for Tramp files."
4427 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
4428 (tramp-tramp-file-p (buffer-substring)))
4429 ;; We don't need to handle `last-input-event', because
4430 ;; due to the key map we know it must be ?/ or ?~.
4431 (let ((s (concat (buffer-substring (point-min) (point))
4432 (string last-command-char))))
4433 (delete-region (point-min) (point))
4434 (insert (substitute-in-file-name s))
4435 (setq ad-return-value last-command-char))
4436 ad-do-it)))
4437 (eval
4438 `(add-hook
4439 'tramp-unload-hook
4440 (lambda ()
4441 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
4442 (ad-activate ',x)))))
4443
4444 '(minibuffer-electric-separator
4445 minibuffer-electric-tilde)))
4446
4447
4448 ;;; Remote commands:
4449
4450 (defun tramp-handle-executable-find (command)
4451 "Like `executable-find' for Tramp files."
4452 (with-parsed-tramp-file-name default-directory nil
4453 (tramp-find-executable v command (tramp-get-remote-path v) t)))
4454
4455 ;; We use BUFFER also as connection buffer during setup. Because of
4456 ;; this, its original contents must be saved, and restored once
4457 ;; connection has been setup.
4458 (defun tramp-handle-start-file-process (name buffer program &rest args)
4459 "Like `start-file-process' for Tramp files."
4460 (with-parsed-tramp-file-name default-directory nil
4461 (unless (stringp program)
4462 (tramp-error
4463 v 'file-error "pty association is not supported for `%s'" name))
4464 (unwind-protect
4465 (let ((command (format "cd %s; exec %s"
4466 (tramp-shell-quote-argument localname)
4467 (mapconcat 'tramp-shell-quote-argument
4468 (cons program args) " ")))
4469 (name1 name)
4470 (i 0))
4471 (unless buffer
4472 ;; BUFFER can be nil. We use a temporary buffer.
4473 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
4474 (while (get-process name1)
4475 ;; NAME must be unique as process name.
4476 (setq i (1+ i)
4477 name1 (format "%s<%d>" name i)))
4478 (setq name name1)
4479 ;; Set the new process properties.
4480 (tramp-set-connection-property v "process-name" name)
4481 (tramp-set-connection-property v "process-buffer" buffer)
4482 ;; Activate narrowing in order to save BUFFER contents.
4483 ;; Clear also the modification time; otherwise we might be
4484 ;; interrupted by `verify-visited-file-modtime'.
4485 (with-current-buffer (tramp-get-connection-buffer v)
4486 (clear-visited-file-modtime)
4487 (narrow-to-region (point-max) (point-max)))
4488 ;; Send the command. `tramp-send-command' opens a new
4489 ;; connection.
4490 (tramp-send-command v command nil t) ; nooutput
4491 ;; Set query flag for this process.
4492 (tramp-set-process-query-on-exit-flag
4493 (tramp-get-connection-process v) t)
4494 ;; Return process.
4495 (tramp-get-connection-process v))
4496 ;; Save exit.
4497 (with-current-buffer (tramp-get-connection-buffer v)
4498 (if (string-match tramp-temp-buffer-name (buffer-name))
4499 (progn
4500 (set-process-buffer (tramp-get-connection-process v) nil)
4501 (kill-buffer (current-buffer)))
4502 (widen)
4503 (goto-char (point-max))))
4504 (tramp-set-connection-property v "process-name" nil)
4505 (tramp-set-connection-property v "process-buffer" nil))))
4506
4507 (defun tramp-handle-process-file
4508 (program &optional infile destination display &rest args)
4509 "Like `process-file' for Tramp files."
4510 ;; The implementation is not complete yet.
4511 (when (and (numberp destination) (zerop destination))
4512 (error "Implementation does not handle immediate return"))
4513
4514 (with-parsed-tramp-file-name default-directory nil
4515 (let (command input tmpinput stderr tmpstderr outbuf ret)
4516 ;; Compute command.
4517 (setq command (mapconcat 'tramp-shell-quote-argument
4518 (cons program args) " "))
4519 ;; Determine input.
4520 (if (null infile)
4521 (setq input "/dev/null")
4522 (setq infile (expand-file-name infile))
4523 (if (tramp-equal-remote default-directory infile)
4524 ;; INFILE is on the same remote host.
4525 (setq input (with-parsed-tramp-file-name infile nil localname))
4526 ;; INFILE must be copied to remote host.
4527 (setq input (tramp-make-tramp-temp-file v)
4528 tmpinput (tramp-make-tramp-file-name method user host input))
4529 (copy-file infile tmpinput t)))
4530 (when input (setq command (format "%s <%s" command input)))
4531
4532 ;; Determine output.
4533 (cond
4534 ;; Just a buffer.
4535 ((bufferp destination)
4536 (setq outbuf destination))
4537 ;; A buffer name.
4538 ((stringp destination)
4539 (setq outbuf (get-buffer-create destination)))
4540 ;; (REAL-DESTINATION ERROR-DESTINATION)
4541 ((consp destination)
4542 ;; output.
4543 (cond
4544 ((bufferp (car destination))
4545 (setq outbuf (car destination)))
4546 ((stringp (car destination))
4547 (setq outbuf (get-buffer-create (car destination))))
4548 ((car destination)
4549 (setq outbuf (current-buffer))))
4550 ;; stderr.
4551 (cond
4552 ((stringp (cadr destination))
4553 (setcar (cdr destination) (expand-file-name (cadr destination)))
4554 (if (tramp-equal-remote default-directory (cadr destination))
4555 ;; stderr is on the same remote host.
4556 (setq stderr (with-parsed-tramp-file-name
4557 (cadr destination) nil localname))
4558 ;; stderr must be copied to remote host. The temporary
4559 ;; file must be deleted after execution.
4560 (setq stderr (tramp-make-tramp-temp-file v)
4561 tmpstderr (tramp-make-tramp-file-name
4562 method user host stderr))))
4563 ;; stderr to be discarded.
4564 ((null (cadr destination))
4565 (setq stderr "/dev/null"))))
4566 ;; 't
4567 (destination
4568 (setq outbuf (current-buffer))))
4569 (when stderr (setq command (format "%s 2>%s" command stderr)))
4570
4571 ;; Send the command. It might not return in time, so we protect it.
4572 (condition-case nil
4573 (unwind-protect
4574 (setq ret
4575 (tramp-send-command-and-check
4576 v (format "\\cd %s; %s"
4577 (tramp-shell-quote-argument localname)
4578 command)
4579 nil t))
4580 ;; We should show the output anyway.
4581 (when outbuf
4582 (with-current-buffer outbuf
4583 (insert
4584 (with-current-buffer (tramp-get-connection-buffer v)
4585 (buffer-string))))
4586 (when display (display-buffer outbuf))))
4587 ;; When the user did interrupt, we should do it also. We use
4588 ;; return code -1 as marker.
4589 (quit
4590 (kill-buffer (tramp-get-connection-buffer v))
4591 (setq ret -1))
4592 ;; Handle errors.
4593 (error
4594 (kill-buffer (tramp-get-connection-buffer v))
4595 (setq ret 1)))
4596
4597 ;; Provide error file.
4598 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
4599
4600 ;; Cleanup. We remove all file cache values for the connection,
4601 ;; because the remote process could have changed them.
4602 (when tmpinput (tramp-compat-delete-file tmpinput 'force))
4603
4604 ;; `process-file-side-effects' has been introduced with GNU
4605 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4606 ;; by `program'. If it doesn't exist, we assume its default
4607 ;; value 't'.
4608 (unless (and (boundp 'process-file-side-effects)
4609 (not (symbol-value 'process-file-side-effects)))
4610 (tramp-flush-directory-property v ""))
4611
4612 ;; Return exit status.
4613 (if (equal ret -1)
4614 (keyboard-quit)
4615 ret))))
4616
4617 (defun tramp-local-call-process
4618 (program &optional infile destination display &rest args)
4619 "Calls `call-process' on the local host.
4620 This is needed because for some Emacs flavors Tramp has
4621 defadviced `call-process' to behave like `process-file'. The
4622 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4623 (let ((default-directory
4624 (if (file-remote-p default-directory)
4625 (tramp-compat-temporary-file-directory)
4626 default-directory)))
4627 (if (executable-find program)
4628 (apply 'call-process program infile destination display args)
4629 1)))
4630
4631 (defun tramp-handle-call-process-region
4632 (start end program &optional delete buffer display &rest args)
4633 "Like `call-process-region' for Tramp files."
4634 (let ((tmpfile (tramp-compat-make-temp-file "")))
4635 (write-region start end tmpfile)
4636 (when delete (delete-region start end))
4637 (unwind-protect
4638 (apply 'call-process program tmpfile buffer display args)
4639 (tramp-compat-delete-file tmpfile 'force))))
4640
4641 (defun tramp-handle-shell-command
4642 (command &optional output-buffer error-buffer)
4643 "Like `shell-command' for Tramp files."
4644 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
4645 ;; We cannot use `shell-file-name' and `shell-command-switch',
4646 ;; they are variables of the local host.
4647 (args (list "/bin/sh" "-c" (substring command 0 asynchronous)))
4648 current-buffer-p
4649 (output-buffer
4650 (cond
4651 ((bufferp output-buffer) output-buffer)
4652 ((stringp output-buffer) (get-buffer-create output-buffer))
4653 (output-buffer
4654 (setq current-buffer-p t)
4655 (current-buffer))
4656 (t (get-buffer-create
4657 (if asynchronous
4658 "*Async Shell Command*"
4659 "*Shell Command Output*")))))
4660 (error-buffer
4661 (cond
4662 ((bufferp error-buffer) error-buffer)
4663 ((stringp error-buffer) (get-buffer-create error-buffer))))
4664 (buffer
4665 (if (and (not asynchronous) error-buffer)
4666 (with-parsed-tramp-file-name default-directory nil
4667 (list output-buffer (tramp-make-tramp-temp-file v)))
4668 output-buffer))
4669 (p (get-buffer-process output-buffer)))
4670
4671 ;; Check whether there is another process running. Tramp does not
4672 ;; support 2 (asynchronous) processes in parallel.
4673 (when p
4674 (if (yes-or-no-p "A command is running. Kill it? ")
4675 (condition-case nil
4676 (kill-process p)
4677 (error nil))
4678 (error "Shell command in progress")))
4679
4680 (if current-buffer-p
4681 (progn
4682 (barf-if-buffer-read-only)
4683 (push-mark nil t))
4684 (with-current-buffer output-buffer
4685 (setq buffer-read-only nil)
4686 (erase-buffer)))
4687
4688 (if (and (not current-buffer-p) (integerp asynchronous))
4689 (prog1
4690 ;; Run the process.
4691 (apply 'start-file-process "*Async Shell*" buffer args)
4692 ;; Display output.
4693 (pop-to-buffer output-buffer)
4694 (setq mode-line-process '(":%s"))
4695 (require 'shell) (shell-mode))
4696
4697 (prog1
4698 ;; Run the process.
4699 (apply 'process-file (car args) nil buffer nil (cdr args))
4700 ;; Insert error messages if they were separated.
4701 (when (listp buffer)
4702 (with-current-buffer error-buffer
4703 (insert-file-contents (cadr buffer)))
4704 (tramp-compat-delete-file (cadr buffer) 'force))
4705 (if current-buffer-p
4706 ;; This is like exchange-point-and-mark, but doesn't
4707 ;; activate the mark. It is cleaner to avoid activation,
4708 ;; even though the command loop would deactivate the mark
4709 ;; because we inserted text.
4710 (goto-char (prog1 (mark t)
4711 (set-marker (mark-marker) (point)
4712 (current-buffer))))
4713 ;; There's some output, display it.
4714 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4715 (if (functionp 'display-message-or-buffer)
4716 (funcall (symbol-function 'display-message-or-buffer)
4717 output-buffer)
4718 (pop-to-buffer output-buffer))))))))
4719
4720 ;; File Editing.
4721
4722 (defvar tramp-handle-file-local-copy-hook nil
4723 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4724
4725 (defun tramp-handle-file-local-copy (filename)
4726 "Like `file-local-copy' for Tramp files."
4727
4728 (with-parsed-tramp-file-name filename nil
4729 (unless (file-exists-p filename)
4730 (tramp-error
4731 v 'file-error
4732 "Cannot make local copy of non-existing file `%s'" filename))
4733
4734 (let* ((size (nth 7 (file-attributes filename)))
4735 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
4736 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
4737 (tmpfile (tramp-compat-make-temp-file filename)))
4738
4739 (condition-case err
4740 (cond
4741 ;; `copy-file' handles direct copy and out-of-band methods.
4742 ((or (tramp-local-host-p v)
4743 (tramp-method-out-of-band-p v size))
4744 (copy-file filename tmpfile t t))
4745
4746 ;; Use inline encoding for file transfer.
4747 (rem-enc
4748 (save-excursion
4749 (tramp-message v 5 "Encoding remote file %s..." filename)
4750 (tramp-barf-unless-okay
4751 v (format rem-enc (tramp-shell-quote-argument localname))
4752 "Encoding remote file failed")
4753 (tramp-message v 5 "Encoding remote file %s...done" filename)
4754
4755 (if (functionp loc-dec)
4756 ;; If local decoding is a function, we call it. We
4757 ;; must disable multibyte, because
4758 ;; `uudecode-decode-region' doesn't handle it
4759 ;; correctly.
4760 (with-temp-buffer
4761 (set-buffer-multibyte nil)
4762 (insert-buffer-substring (tramp-get-buffer v))
4763 (tramp-message
4764 v 5 "Decoding remote file %s with function %s..."
4765 filename loc-dec)
4766 (funcall loc-dec (point-min) (point-max))
4767 ;; Unset `file-name-handler-alist'. Otherwise,
4768 ;; epa-file gets confused.
4769 (let (file-name-handler-alist
4770 (coding-system-for-write 'binary))
4771 (write-region (point-min) (point-max) tmpfile)))
4772
4773 ;; If tramp-decoding-function is not defined for this
4774 ;; method, we invoke tramp-decoding-command instead.
4775 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
4776 ;; Unset `file-name-handler-alist'. Otherwise,
4777 ;; epa-file gets confused.
4778 (let (file-name-handler-alist
4779 (coding-system-for-write 'binary))
4780 (write-region (point-min) (point-max) tmpfile2))
4781 (tramp-message
4782 v 5 "Decoding remote file %s with command %s..."
4783 filename loc-dec)
4784 (unwind-protect
4785 (tramp-call-local-coding-command loc-dec tmpfile2 tmpfile)
4786 (tramp-compat-delete-file tmpfile2 'force))))
4787
4788 (tramp-message v 5 "Decoding remote file %s...done" filename)
4789 ;; Set proper permissions.
4790 (set-file-modes tmpfile (tramp-default-file-modes filename))
4791 ;; Set local user ownership.
4792 (tramp-set-file-uid-gid tmpfile)))
4793
4794 ;; Oops, I don't know what to do.
4795 (t (tramp-error
4796 v 'file-error "Wrong method specification for `%s'" method)))
4797
4798 ;; Error handling.
4799 ((error quit)
4800 (tramp-compat-delete-file tmpfile 'force)
4801 (signal (car err) (cdr err))))
4802
4803 (run-hooks 'tramp-handle-file-local-copy-hook)
4804 tmpfile)))
4805
4806 (defun tramp-handle-file-remote-p (filename &optional identification connected)
4807 "Like `file-remote-p' for Tramp files."
4808 (let ((tramp-verbose 3))
4809 (when (tramp-tramp-file-p filename)
4810 (let* ((v (tramp-dissect-file-name filename))
4811 (p (tramp-get-connection-process v))
4812 (c (and p (processp p) (memq (process-status p) '(run open)))))
4813 ;; We expand the file name only, if there is already a connection.
4814 (with-parsed-tramp-file-name
4815 (if c (expand-file-name filename) filename) nil
4816 (and (or (not connected) c)
4817 (cond
4818 ((eq identification 'method) method)
4819 ((eq identification 'user) user)
4820 ((eq identification 'host) host)
4821 ((eq identification 'localname) localname)
4822 (t (tramp-make-tramp-file-name method user host "")))))))))
4823
4824 (defun tramp-find-file-name-coding-system-alist (filename tmpname)
4825 "Like `find-operation-coding-system' for Tramp filenames.
4826 Tramp's `insert-file-contents' and `write-region' work over
4827 temporary file names. If `file-coding-system-alist' contains an
4828 expression, which matches more than the file name suffix, the
4829 coding system might not be determined. This function repairs it."
4830 (let (result)
4831 (dolist (elt file-coding-system-alist result)
4832 (when (and (consp elt) (string-match (car elt) filename))
4833 ;; We found a matching entry in `file-coding-system-alist'.
4834 ;; So we add a similar entry, but with the temporary file name
4835 ;; as regexp.
4836 (add-to-list
4837 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
4838
4839 (defun tramp-handle-insert-file-contents
4840 (filename &optional visit beg end replace)
4841 "Like `insert-file-contents' for Tramp files."
4842 (barf-if-buffer-read-only)
4843 (setq filename (expand-file-name filename))
4844 (let (coding-system-used result local-copy remote-copy)
4845 (with-parsed-tramp-file-name filename nil
4846 (unwind-protect
4847 (if (not (file-exists-p filename))
4848 ;; We don't raise a Tramp error, because it might be
4849 ;; suppressed, like in `find-file-noselect-1'.
4850 (signal 'file-error
4851 (list "File not found on remote host" filename))
4852
4853 (if (and (tramp-local-host-p v)
4854 (let (file-name-handler-alist)
4855 (file-readable-p localname)))
4856 ;; Short track: if we are on the local host, we can
4857 ;; run directly.
4858 (setq result
4859 (tramp-run-real-handler
4860 'insert-file-contents
4861 (list localname visit beg end replace)))
4862
4863 ;; When we shall insert only a part of the file, we copy
4864 ;; this part.
4865 (when (or beg end)
4866 (setq remote-copy (tramp-make-tramp-temp-file v))
4867 (tramp-send-command
4868 v
4869 (cond
4870 ((and beg end)
4871 (format "tail -c +%d %s | head -c +%d >%s"
4872 (1+ beg) (tramp-shell-quote-argument localname)
4873 (- end beg) remote-copy))
4874 (beg
4875 (format "tail -c +%d %s >%s"
4876 (1+ beg) (tramp-shell-quote-argument localname)
4877 remote-copy))
4878 (end
4879 (format "head -c +%d %s >%s"
4880 (1+ end) (tramp-shell-quote-argument localname)
4881 remote-copy)))))
4882
4883 ;; `insert-file-contents-literally' takes care to avoid
4884 ;; calling jka-compr. By let-binding
4885 ;; `inhibit-file-name-operation', we propagate that care
4886 ;; to the `file-local-copy' operation.
4887 (setq local-copy
4888 (let ((inhibit-file-name-operation
4889 (when (eq inhibit-file-name-operation
4890 'insert-file-contents)
4891 'file-local-copy)))
4892 (cond
4893 ((stringp remote-copy)
4894 (file-local-copy
4895 (tramp-make-tramp-file-name
4896 method user host remote-copy)))
4897 ((stringp tramp-temp-buffer-file-name)
4898 (copy-file filename tramp-temp-buffer-file-name 'ok)
4899 tramp-temp-buffer-file-name)
4900 (t (file-local-copy filename)))))
4901
4902 ;; When the file is not readable for the owner, it
4903 ;; cannot be inserted, even it is redable for the group
4904 ;; or for everybody.
4905 (set-file-modes local-copy (tramp-octal-to-decimal "0600"))
4906
4907 (when (and (null remote-copy)
4908 (tramp-get-method-parameter
4909 method 'tramp-copy-keep-tmpfile))
4910 ;; We keep the local file for performance reasons,
4911 ;; useful for "rsync".
4912 (setq tramp-temp-buffer-file-name local-copy)
4913 (put 'tramp-temp-buffer-file-name 'permanent-local t))
4914
4915 (tramp-message
4916 v 4 "Inserting local temp file `%s'..." local-copy)
4917
4918 ;; We must ensure that `file-coding-system-alist'
4919 ;; matches `local-copy'.
4920 (let ((file-coding-system-alist
4921 (tramp-find-file-name-coding-system-alist
4922 filename local-copy)))
4923 (setq result
4924 (insert-file-contents
4925 local-copy nil nil nil replace))
4926 ;; Now `last-coding-system-used' has right value.
4927 ;; Remember it.
4928 (when (boundp 'last-coding-system-used)
4929 (setq coding-system-used
4930 (symbol-value 'last-coding-system-used))))
4931
4932 (tramp-message
4933 v 4 "Inserting local temp file `%s'...done" local-copy)
4934 (when (boundp 'last-coding-system-used)
4935 (set 'last-coding-system-used coding-system-used))))
4936
4937 ;; Save exit.
4938 (progn
4939 (when visit
4940 (setq buffer-file-name filename)
4941 (setq buffer-read-only (not (file-writable-p filename)))
4942 (set-visited-file-modtime)
4943 (set-buffer-modified-p nil))
4944 (when (and (stringp local-copy)
4945 (or remote-copy (null tramp-temp-buffer-file-name)))
4946 (tramp-compat-delete-file local-copy 'force))
4947 (when (stringp remote-copy)
4948 (tramp-compat-delete-file
4949 (tramp-make-tramp-file-name method user host remote-copy)
4950 'force)))))
4951
4952 ;; Result.
4953 (list (expand-file-name filename)
4954 (cadr result))))
4955
4956 ;; This is needed for XEmacs only. Code stolen from files.el.
4957 (defun tramp-handle-insert-file-contents-literally
4958 (filename &optional visit beg end replace)
4959 "Like `insert-file-contents-literally' for Tramp files."
4960 (let ((format-alist nil)
4961 (after-insert-file-functions nil)
4962 (coding-system-for-read 'no-conversion)
4963 (coding-system-for-write 'no-conversion)
4964 (find-buffer-file-type-function
4965 (if (fboundp 'find-buffer-file-type)
4966 (symbol-function 'find-buffer-file-type)
4967 nil))
4968 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
4969 (inhibit-file-name-operation 'insert-file-contents))
4970 (unwind-protect
4971 (progn
4972 (fset 'find-buffer-file-type (lambda (filename) t))
4973 (insert-file-contents filename visit beg end replace))
4974 ;; Save exit.
4975 (if find-buffer-file-type-function
4976 (fset 'find-buffer-file-type find-buffer-file-type-function)
4977 (fmakunbound 'find-buffer-file-type)))))
4978
4979 (defun tramp-handle-find-backup-file-name (filename)
4980 "Like `find-backup-file-name' for Tramp files."
4981 (with-parsed-tramp-file-name filename nil
4982 ;; We set both variables. It doesn't matter whether it is
4983 ;; Emacs or XEmacs.
4984 (let ((backup-directory-alist
4985 ;; Emacs case.
4986 (when (boundp 'backup-directory-alist)
4987 (if (symbol-value 'tramp-backup-directory-alist)
4988 (mapcar
4989 (lambda (x)
4990 (cons
4991 (car x)
4992 (if (and (stringp (cdr x))
4993 (file-name-absolute-p (cdr x))
4994 (not (tramp-file-name-p (cdr x))))
4995 (tramp-make-tramp-file-name method user host (cdr x))
4996 (cdr x))))
4997 (symbol-value 'tramp-backup-directory-alist))
4998 (symbol-value 'backup-directory-alist))))
4999
5000 (bkup-backup-directory-info
5001 ;; XEmacs case.
5002 (when (boundp 'bkup-backup-directory-info)
5003 (if (symbol-value 'tramp-bkup-backup-directory-info)
5004 (mapcar
5005 (lambda (x)
5006 (nconc
5007 (list (car x))
5008 (list
5009 (if (and (stringp (car (cdr x)))
5010 (file-name-absolute-p (car (cdr x)))
5011 (not (tramp-file-name-p (car (cdr x)))))
5012 (tramp-make-tramp-file-name
5013 method user host (car (cdr x)))
5014 (car (cdr x))))
5015 (cdr (cdr x))))
5016 (symbol-value 'tramp-bkup-backup-directory-info))
5017 (symbol-value 'bkup-backup-directory-info)))))
5018
5019 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
5020
5021 (defun tramp-handle-make-auto-save-file-name ()
5022 "Like `make-auto-save-file-name' for Tramp files.
5023 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
5024 (let ((tramp-auto-save-directory tramp-auto-save-directory)
5025 (buffer-file-name
5026 (tramp-subst-strs-in-string
5027 '(("_" . "|")
5028 ("/" . "_a")
5029 (":" . "_b")
5030 ("|" . "__")
5031 ("[" . "_l")
5032 ("]" . "_r"))
5033 (buffer-file-name))))
5034 ;; File name must be unique. This is ensured with Emacs 22 (see
5035 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5036 ;; all other cases we must do it ourselves.
5037 (when (boundp 'auto-save-file-name-transforms)
5038 (mapc
5039 (lambda (x)
5040 (when (and (string-match (car x) buffer-file-name)
5041 (not (car (cddr x))))
5042 (setq tramp-auto-save-directory
5043 (or tramp-auto-save-directory
5044 (tramp-compat-temporary-file-directory)))))
5045 (symbol-value 'auto-save-file-name-transforms)))
5046 ;; Create directory.
5047 (when tramp-auto-save-directory
5048 (setq buffer-file-name
5049 (expand-file-name buffer-file-name tramp-auto-save-directory))
5050 (unless (file-exists-p tramp-auto-save-directory)
5051 (make-directory tramp-auto-save-directory t)))
5052 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5053 ;; it is not a magic file name operation (since Emacs 22).
5054 ;; We must deactivate it temporarily.
5055 (if (not (ad-is-active 'make-auto-save-file-name))
5056 (tramp-run-real-handler 'make-auto-save-file-name nil)
5057 ;; else
5058 (ad-deactivate 'make-auto-save-file-name)
5059 (prog1
5060 (tramp-run-real-handler 'make-auto-save-file-name nil)
5061 (ad-activate 'make-auto-save-file-name)))))
5062
5063 (defvar tramp-handle-write-region-hook nil
5064 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5065
5066 ;; CCC grok LOCKNAME
5067 (defun tramp-handle-write-region
5068 (start end filename &optional append visit lockname confirm)
5069 "Like `write-region' for Tramp files."
5070 (setq filename (expand-file-name filename))
5071 (with-parsed-tramp-file-name filename nil
5072 ;; Following part commented out because we don't know what to do about
5073 ;; file locking, and it does not appear to be a problem to ignore it.
5074 ;; Ange-ftp ignores it, too.
5075 ;; (when (and lockname (stringp lockname))
5076 ;; (setq lockname (expand-file-name lockname)))
5077 ;; (unless (or (eq lockname nil)
5078 ;; (string= lockname filename))
5079 ;; (error
5080 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
5081
5082 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
5083 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
5084 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
5085 (tramp-error v 'file-error "File not overwritten")))
5086
5087 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
5088 (tramp-get-remote-uid v 'integer)))
5089 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
5090 (tramp-get-remote-gid v 'integer))))
5091
5092 (if (and (tramp-local-host-p v)
5093 ;; `file-writable-p' calls `file-expand-file-name'. We
5094 ;; cannot use `tramp-run-real-handler' therefore.
5095 (let (file-name-handler-alist)
5096 (and
5097 (file-writable-p (file-name-directory localname))
5098 (or (file-directory-p localname)
5099 (file-writable-p localname)))))
5100 ;; Short track: if we are on the local host, we can run directly.
5101 (tramp-run-real-handler
5102 'write-region
5103 (list start end localname append 'no-message lockname confirm))
5104
5105 (let ((modes (save-excursion (tramp-default-file-modes filename)))
5106 ;; We use this to save the value of
5107 ;; `last-coding-system-used' after writing the tmp
5108 ;; file. At the end of the function, we set
5109 ;; `last-coding-system-used' to this saved value. This
5110 ;; way, any intermediary coding systems used while
5111 ;; talking to the remote shell or suchlike won't hose
5112 ;; this variable. This approach was snarfed from
5113 ;; ange-ftp.el.
5114 coding-system-used
5115 ;; Write region into a tmp file. This isn't really
5116 ;; needed if we use an encoding function, but currently
5117 ;; we use it always because this makes the logic
5118 ;; simpler.
5119 (tmpfile (or tramp-temp-buffer-file-name
5120 (tramp-compat-make-temp-file filename))))
5121
5122 ;; If `append' is non-nil, we copy the file locally, and let
5123 ;; the native `write-region' implementation do the job.
5124 (when append (copy-file filename tmpfile 'ok))
5125
5126 ;; We say `no-message' here because we don't want the
5127 ;; visited file modtime data to be clobbered from the temp
5128 ;; file. We call `set-visited-file-modtime' ourselves later
5129 ;; on. We must ensure that `file-coding-system-alist'
5130 ;; matches `tmpfile'.
5131 (let (file-name-handler-alist
5132 (file-coding-system-alist
5133 (tramp-find-file-name-coding-system-alist filename tmpfile)))
5134 (condition-case err
5135 (tramp-run-real-handler
5136 'write-region
5137 (list start end tmpfile append 'no-message lockname confirm))
5138 ((error quit)
5139 (setq tramp-temp-buffer-file-name nil)
5140 (tramp-compat-delete-file tmpfile 'force)
5141 (signal (car err) (cdr err))))
5142
5143 ;; Now, `last-coding-system-used' has the right value. Remember it.
5144 (when (boundp 'last-coding-system-used)
5145 (setq coding-system-used
5146 (symbol-value 'last-coding-system-used))))
5147
5148 ;; The permissions of the temporary file should be set. If
5149 ;; filename does not exist (eq modes nil) it has been
5150 ;; renamed to the backup file. This case `save-buffer'
5151 ;; handles permissions.
5152 ;; Ensure, that it is still readable.
5153 (when modes
5154 (set-file-modes
5155 tmpfile (logior (or modes 0) (tramp-octal-to-decimal "0400"))))
5156
5157 ;; This is a bit lengthy due to the different methods
5158 ;; possible for file transfer. First, we check whether the
5159 ;; method uses an rcp program. If so, we call it.
5160 ;; Otherwise, both encoding and decoding command must be
5161 ;; specified. However, if the method _also_ specifies an
5162 ;; encoding function, then that is used for encoding the
5163 ;; contents of the tmp file.
5164 (let* ((size (nth 7 (file-attributes tmpfile)))
5165 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
5166 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
5167 (cond
5168 ;; `copy-file' handles direct copy and out-of-band methods.
5169 ((or (tramp-local-host-p v)
5170 (tramp-method-out-of-band-p v size))
5171 (if (and (not (stringp start))
5172 (= (or end (point-max)) (point-max))
5173 (= (or start (point-min)) (point-min))
5174 (tramp-get-method-parameter
5175 method 'tramp-copy-keep-tmpfile))
5176 (progn
5177 (setq tramp-temp-buffer-file-name tmpfile)
5178 (condition-case err
5179 ;; We keep the local file for performance
5180 ;; reasons, useful for "rsync".
5181 (copy-file tmpfile filename t)
5182 ((error quit)
5183 (setq tramp-temp-buffer-file-name nil)
5184 (tramp-compat-delete-file tmpfile 'force)
5185 (signal (car err) (cdr err)))))
5186 (setq tramp-temp-buffer-file-name nil)
5187 ;; Don't rename, in order to keep context in SELinux.
5188 (unwind-protect
5189 (copy-file tmpfile filename t)
5190 (tramp-compat-delete-file tmpfile 'force))))
5191
5192 ;; Use inline file transfer.
5193 (rem-dec
5194 ;; Encode tmpfile.
5195 (tramp-message v 5 "Encoding region...")
5196 (unwind-protect
5197 (with-temp-buffer
5198 (set-buffer-multibyte nil)
5199 ;; Use encoding function or command.
5200 (if (functionp loc-enc)
5201 (progn
5202 (tramp-message
5203 v 5 "Encoding region using function `%s'..." loc-enc)
5204 (let ((coding-system-for-read 'binary))
5205 (insert-file-contents-literally tmpfile))
5206 ;; The following `let' is a workaround for the
5207 ;; base64.el that comes with pgnus-0.84. If
5208 ;; both of the following conditions are
5209 ;; satisfied, it tries to write to a local
5210 ;; file in default-directory, but at this
5211 ;; point, default-directory is remote.
5212 ;; (`call-process-region' can't write to
5213 ;; remote files, it seems.) The file in
5214 ;; question is a tmp file anyway.
5215 (let ((default-directory
5216 (tramp-compat-temporary-file-directory)))
5217 (funcall loc-enc (point-min) (point-max))))
5218
5219 (tramp-message
5220 v 5 "Encoding region using command `%s'..." loc-enc)
5221 (unless (zerop (tramp-call-local-coding-command
5222 loc-enc tmpfile t))
5223 (tramp-error
5224 v 'file-error
5225 "Cannot write to `%s', local encoding command `%s' failed"
5226 filename loc-enc)))
5227
5228 ;; Send buffer into remote decoding command which
5229 ;; writes to remote file. Because this happens on
5230 ;; the remote host, we cannot use the function.
5231 (goto-char (point-max))
5232 (unless (bolp) (newline))
5233 (tramp-message
5234 v 5 "Decoding region into remote file %s..." filename)
5235 (tramp-send-command
5236 v
5237 (format
5238 (concat rem-dec " <<'EOF'\n%sEOF")
5239 (tramp-shell-quote-argument localname)
5240 (buffer-string)))
5241 (tramp-barf-unless-okay
5242 v nil
5243 "Couldn't write region to `%s', decode using `%s' failed"
5244 filename rem-dec)
5245 ;; When `file-precious-flag' is set, the region is
5246 ;; written to a temporary file. Check that the
5247 ;; checksum is equal to that from the local tmpfile.
5248 (when file-precious-flag
5249 (erase-buffer)
5250 (and
5251 ;; cksum runs locally, if possible.
5252 (zerop (tramp-local-call-process "cksum" tmpfile t))
5253 ;; cksum runs remotely.
5254 (zerop
5255 (tramp-send-command-and-check
5256 v
5257 (format
5258 "cksum <%s" (tramp-shell-quote-argument localname))))
5259 ;; ... they are different.
5260 (not
5261 (string-equal
5262 (buffer-string)
5263 (with-current-buffer (tramp-get-buffer v)
5264 (buffer-string))))
5265 (tramp-error
5266 v 'file-error
5267 (concat "Couldn't write region to `%s',"
5268 " decode using `%s' failed")
5269 filename rem-dec)))
5270 (tramp-message
5271 v 5 "Decoding region into remote file %s...done" filename))
5272
5273 ;; Save exit.
5274 (tramp-compat-delete-file tmpfile 'force)))
5275
5276 ;; That's not expected.
5277 (t
5278 (tramp-error
5279 v 'file-error
5280 (concat "Method `%s' should specify both encoding and "
5281 "decoding command or an rcp program")
5282 method))))
5283
5284 ;; Make `last-coding-system-used' have the right value.
5285 (when coding-system-used
5286 (set 'last-coding-system-used coding-system-used))))
5287
5288 (tramp-flush-file-property v (file-name-directory localname))
5289 (tramp-flush-file-property v localname)
5290
5291 ;; We must protect `last-coding-system-used', now we have set it
5292 ;; to its correct value.
5293 (let (last-coding-system-used (need-chown t))
5294 ;; Set file modification time.
5295 (when (or (eq visit t) (stringp visit))
5296 (let ((file-attr (file-attributes filename)))
5297 (set-visited-file-modtime
5298 ;; We must pass modtime explicitely, because filename can
5299 ;; be different from (buffer-file-name), f.e. if
5300 ;; `file-precious-flag' is set.
5301 (nth 5 file-attr))
5302 (when (and (eq (nth 2 file-attr) uid)
5303 (eq (nth 3 file-attr) gid))
5304 (setq need-chown nil))))
5305
5306 ;; Set the ownership.
5307 (when need-chown
5308 (tramp-set-file-uid-gid filename uid gid))
5309 (when (or (eq visit t) (null visit) (stringp visit))
5310 (tramp-message v 0 "Wrote %s" filename))
5311 (run-hooks 'tramp-handle-write-region-hook)))))
5312
5313 (defvar tramp-vc-registered-file-names nil
5314 "List used to collect file names, which are checked during `vc-registered'.")
5315
5316 ;; VC backends check for the existence of various different special
5317 ;; files. This is very time consuming, because every single check
5318 ;; requires a remote command (the file cache must be invalidated).
5319 ;; Therefore, we apply a kind of optimization. We install the file
5320 ;; name handler `tramp-vc-file-name-handler', which does nothing but
5321 ;; remembers all file names for which `file-exists-p' or
5322 ;; `file-readable-p' has been applied. A first run of `vc-registered'
5323 ;; is performed. Afterwards, a script is applied for all collected
5324 ;; file names, using just one remote command. The result of this
5325 ;; script is used to fill the file cache with actual values. Now we
5326 ;; can reset the file name handlers, and we make a second run of
5327 ;; `vc-registered', which returns the expected result without sending
5328 ;; any other remote command.
5329 (defun tramp-handle-vc-registered (file)
5330 "Like `vc-registered' for Tramp files."
5331 (with-parsed-tramp-file-name file nil
5332
5333 ;; There could be new files, created by the vc backend. We cannot
5334 ;; reuse the old cache entries, therefore.
5335 (let (tramp-vc-registered-file-names
5336 (tramp-cache-inhibit-cache (current-time))
5337 (file-name-handler-alist
5338 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5339
5340 ;; Here we collect only file names, which need an operation.
5341 (tramp-run-real-handler 'vc-registered (list file))
5342 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5343
5344 ;; Send just one command, in order to fill the cache.
5345 (when tramp-vc-registered-file-names
5346 (tramp-maybe-send-script
5347 v
5348 (format tramp-vc-registered-read-file-names
5349 (tramp-get-file-exists-command v)
5350 (format "%s -r" (tramp-get-test-command v)))
5351 "tramp_vc_registered_read_file_names")
5352
5353 (dolist
5354 (elt
5355 (tramp-send-command-and-read
5356 v
5357 (format
5358 "tramp_vc_registered_read_file_names %s"
5359 (mapconcat 'tramp-shell-quote-argument
5360 tramp-vc-registered-file-names
5361 " "))))
5362
5363 (tramp-set-file-property v (car elt) (cadr elt) (cadr (cdr elt))))))
5364
5365 ;; Second run. Now all `file-exists-p' or `file-readable-p' calls
5366 ;; shall be answered from the file cache.
5367 ;; We unset `process-file-side-effects' in order to keep the cache
5368 ;; when `process-file' calls appear.
5369 (let (process-file-side-effects)
5370 (tramp-run-real-handler 'vc-registered (list file)))))
5371
5372 ;;;###autoload
5373 (progn (defun tramp-run-real-handler (operation args)
5374 "Invoke normal file name handler for OPERATION.
5375 First arg specifies the OPERATION, second arg is a list of arguments to
5376 pass to the OPERATION."
5377 (let* ((inhibit-file-name-handlers
5378 `(tramp-file-name-handler
5379 tramp-vc-file-name-handler
5380 tramp-completion-file-name-handler
5381 cygwin-mount-name-hook-function
5382 cygwin-mount-map-drive-hook-function
5383 .
5384 ,(and (eq inhibit-file-name-operation operation)
5385 inhibit-file-name-handlers)))
5386 (inhibit-file-name-operation operation))
5387 (apply operation args))))
5388
5389 ;;;###autoload
5390 (progn (defun tramp-completion-run-real-handler (operation args)
5391 "Invoke `tramp-file-name-handler' for OPERATION.
5392 First arg specifies the OPERATION, second arg is a list of arguments to
5393 pass to the OPERATION."
5394 (let* ((inhibit-file-name-handlers
5395 `(tramp-completion-file-name-handler
5396 cygwin-mount-name-hook-function
5397 cygwin-mount-map-drive-hook-function
5398 .
5399 ,(and (eq inhibit-file-name-operation operation)
5400 inhibit-file-name-handlers)))
5401 (inhibit-file-name-operation operation))
5402 (apply operation args))))
5403
5404 ;; We handle here all file primitives. Most of them have the file
5405 ;; name as first parameter; nevertheless we check for them explicitly
5406 ;; in order to be signaled if a new primitive appears. This
5407 ;; scenario is needed because there isn't a way to decide by
5408 ;; syntactical means whether a foreign method must be called. It would
5409 ;; ease the life if `file-name-handler-alist' would support a decision
5410 ;; function as well but regexp only.
5411 (defun tramp-file-name-for-operation (operation &rest args)
5412 "Return file name related to OPERATION file primitive.
5413 ARGS are the arguments OPERATION has been called with."
5414 (cond
5415 ;; FILE resp DIRECTORY.
5416 ((member operation
5417 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
5418 'delete-file 'diff-latest-backup-file 'directory-file-name
5419 'directory-files 'directory-files-and-attributes
5420 'dired-compress-file 'dired-uncache
5421 'file-accessible-directory-p 'file-attributes
5422 'file-directory-p 'file-executable-p 'file-exists-p
5423 'file-local-copy 'file-remote-p 'file-modes
5424 'file-name-as-directory 'file-name-directory
5425 'file-name-nondirectory 'file-name-sans-versions
5426 'file-ownership-preserved-p 'file-readable-p
5427 'file-regular-p 'file-symlink-p 'file-truename
5428 'file-writable-p 'find-backup-file-name 'find-file-noselect
5429 'get-file-buffer 'insert-directory 'insert-file-contents
5430 'load 'make-directory 'make-directory-internal
5431 'set-file-modes 'substitute-in-file-name
5432 'unhandled-file-name-directory 'vc-registered
5433 ;; Emacs 22+ only.
5434 'set-file-times
5435 ;; Emacs 24+ only.
5436 'file-selinux-context 'set-file-selinux-context
5437 ;; XEmacs only.
5438 'abbreviate-file-name 'create-file-buffer
5439 'dired-file-modtime 'dired-make-compressed-filename
5440 'dired-recursive-delete-directory 'dired-set-file-modtime
5441 'dired-shell-unhandle-file-name 'dired-uucode-file
5442 'insert-file-contents-literally 'make-temp-name 'recover-file
5443 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
5444 (if (file-name-absolute-p (nth 0 args))
5445 (nth 0 args)
5446 (expand-file-name (nth 0 args))))
5447 ;; FILE DIRECTORY resp FILE1 FILE2.
5448 ((member operation
5449 (list 'add-name-to-file 'copy-file 'expand-file-name
5450 'file-name-all-completions 'file-name-completion
5451 'file-newer-than-file-p 'make-symbolic-link 'rename-file
5452 ;; Emacs 23+ only.
5453 'copy-directory
5454 ;; XEmacs only.
5455 'dired-make-relative-symlink
5456 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
5457 (save-match-data
5458 (cond
5459 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
5460 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
5461 (t (buffer-file-name (current-buffer))))))
5462 ;; START END FILE.
5463 ((eq operation 'write-region)
5464 (nth 2 args))
5465 ;; BUFFER.
5466 ((member operation
5467 (list 'set-visited-file-modtime 'verify-visited-file-modtime
5468 ;; Emacs 22+ only.
5469 'make-auto-save-file-name
5470 ;; XEmacs only.
5471 'backup-buffer))
5472 (buffer-file-name
5473 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
5474 ;; COMMAND.
5475 ((member operation
5476 (list ;; not in Emacs 23+.
5477 'dired-call-process
5478 ;; Emacs only.
5479 'shell-command
5480 ;; Emacs 22+ only.
5481 'process-file
5482 ;; Emacs 23+ only.
5483 'start-file-process
5484 ;; XEmacs only.
5485 'dired-print-file 'dired-shell-call-process
5486 ;; nowhere yet.
5487 'executable-find 'start-process 'call-process))
5488 default-directory)
5489 ;; Unknown file primitive.
5490 (t (error "unknown file I/O primitive: %s" operation))))
5491
5492 (defun tramp-find-foreign-file-name-handler (filename)
5493 "Return foreign file name handler if exists."
5494 (when (tramp-tramp-file-p filename)
5495 (let ((v (tramp-dissect-file-name filename t))
5496 (handler tramp-foreign-file-name-handler-alist)
5497 elt res)
5498 ;; When we are not fully sure that filename completion is safe,
5499 ;; we should not return a handler.
5500 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
5501 (and (tramp-file-name-host v)
5502 (not (member (tramp-file-name-host v)
5503 (mapcar 'car tramp-methods))))
5504 (not (tramp-completion-mode-p)))
5505 (while handler
5506 (setq elt (car handler)
5507 handler (cdr handler))
5508 (when (funcall (car elt) filename)
5509 (setq handler nil
5510 res (cdr elt))))
5511 res))))
5512
5513 ;; Main function.
5514 ;;;###autoload
5515 (defun tramp-file-name-handler (operation &rest args)
5516 "Invoke Tramp file name handler.
5517 Falls back to normal file name handler if no Tramp file name handler exists."
5518 (if tramp-mode
5519 (save-match-data
5520 (let* ((filename
5521 (tramp-replace-environment-variables
5522 (apply 'tramp-file-name-for-operation operation args)))
5523 (completion (tramp-completion-mode-p))
5524 (foreign (tramp-find-foreign-file-name-handler filename)))
5525 (with-parsed-tramp-file-name filename nil
5526 ;; Call the backend function.
5527 (if foreign
5528 (condition-case err
5529 (apply foreign operation args)
5530 (error
5531 (cond
5532 ;; When we are in completion mode, some failed
5533 ;; operations shall return at least a default
5534 ;; value in order to give the user a chance to
5535 ;; correct the file name in the minibuffer.
5536 ((and completion (zerop (length localname))
5537 (memq operation '(file-exists-p file-directory-p)))
5538 t)
5539 ((and completion (zerop (length localname))
5540 (memq operation
5541 '(expand-file-name file-name-as-directory)))
5542 filename)
5543 ;; Propagate the error.
5544 (t (signal (car err) (cdr err))))))
5545 ;; Nothing to do for us.
5546 (tramp-run-real-handler operation args)))))
5547
5548 ;; When `tramp-mode' is not enabled, we don't do anything.
5549 (tramp-run-real-handler operation args)))
5550
5551 ;; In Emacs, there is some concurrency due to timers. If a timer
5552 ;; interrupts Tramp and wishes to use the same connection buffer as
5553 ;; the "main" Emacs, then garbage might occur in the connection
5554 ;; buffer. Therefore, we need to make sure that a timer does not use
5555 ;; the same connection buffer as the "main" Emacs. We implement a
5556 ;; cheap global lock, instead of locking each connection buffer
5557 ;; separately. The global lock is based on two variables,
5558 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5559 ;; (with setq) to indicate a lock. But Tramp also calls itself during
5560 ;; processing of a single file operation, so we need to allow
5561 ;; recursive calls. That's where the `tramp-locker' variable comes in
5562 ;; -- it is let-bound to t during the execution of the current
5563 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5564 ;; then we should just proceed because we have been called
5565 ;; recursively. But if `tramp-locker' is nil, then we are a timer
5566 ;; interrupting the "main" Emacs, and then we signal an error.
5567
5568 (defvar tramp-locked nil
5569 "If non-nil, then Tramp is currently busy.
5570 Together with `tramp-locker', this implements a locking mechanism
5571 preventing reentrant calls of Tramp.")
5572
5573 (defvar tramp-locker nil
5574 "If non-nil, then a caller has locked Tramp.
5575 Together with `tramp-locked', this implements a locking mechanism
5576 preventing reentrant calls of Tramp.")
5577
5578 (defun tramp-sh-file-name-handler (operation &rest args)
5579 "Invoke remote-shell Tramp file name handler.
5580 Fall back to normal file name handler if no Tramp handler exists."
5581 (when (and tramp-locked (not tramp-locker))
5582 (setq tramp-locked nil)
5583 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
5584 (let ((tl tramp-locked))
5585 (unwind-protect
5586 (progn
5587 (setq tramp-locked t)
5588 (let ((tramp-locker t))
5589 (save-match-data
5590 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5591 (if fn
5592 (apply (cdr fn) args)
5593 (tramp-run-real-handler operation args))))))
5594 (setq tramp-locked tl))))
5595
5596 (defun tramp-vc-file-name-handler (operation &rest args)
5597 "Invoke special file name handler, which collects files to be handled."
5598 (save-match-data
5599 (let ((filename
5600 (tramp-replace-environment-variables
5601 (apply 'tramp-file-name-for-operation operation args)))
5602 (fn (assoc operation tramp-file-name-handler-alist)))
5603 (with-parsed-tramp-file-name filename nil
5604 (cond
5605 ;; That's what we want: file names, for which checks are
5606 ;; applied. We assume, that VC uses only `file-exists-p' and
5607 ;; `file-readable-p' checks; otherwise we must extend the
5608 ;; list. We do not perform any action, but return nil, in
5609 ;; order to keep `vc-registered' running.
5610 ((and fn (memq operation '(file-exists-p file-readable-p)))
5611 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5612 nil)
5613 ;; Tramp file name handlers like `expand-file-name'. They
5614 ;; must still work.
5615 (fn
5616 (save-match-data (apply (cdr fn) args)))
5617 ;; Default file name handlers, we don't care.
5618 (t (tramp-run-real-handler operation args)))))))
5619
5620 ;;;###autoload
5621 (progn (defun tramp-completion-file-name-handler (operation &rest args)
5622 "Invoke Tramp file name completion handler.
5623 Falls back to normal file name handler if no Tramp file name handler exists."
5624 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5625 ;; would otherwise use backslash.
5626 (let ((directory-sep-char ?/)
5627 (fn (assoc operation tramp-completion-file-name-handler-alist)))
5628 (if (and
5629 ;; When `tramp-mode' is not enabled, we don't do anything.
5630 fn tramp-mode
5631 ;; For other syntaxes than `sep', the regexp matches many common
5632 ;; situations where the user doesn't actually want to use Tramp.
5633 ;; So to avoid autoloading Tramp after typing just "/s", we
5634 ;; disable this part of the completion, unless the user implicitly
5635 ;; indicated his interest in using a fancier completion system.
5636 (or (eq tramp-syntax 'sep)
5637 (featurep 'tramp) ;; If it's loaded, we may as well use it.
5638 ;; `partial-completion-mode' does not exist in XEmacs.
5639 ;; It is obsoleted with Emacs 24.1.
5640 (and (boundp 'partial-completion-mode) partial-completion-mode)
5641 ;; FIXME: These may have been loaded even if the user never
5642 ;; intended to use them.
5643 (featurep 'ido)
5644 (featurep 'icicles)))
5645 (save-match-data (apply (cdr fn) args))
5646 (tramp-completion-run-real-handler operation args)))))
5647
5648 ;;;###autoload
5649 (progn (defun tramp-register-file-name-handlers ()
5650 "Add Tramp file name handlers to `file-name-handler-alist'."
5651 ;; Remove autoloaded handlers from file name handler alist. Useful,
5652 ;; if `tramp-syntax' has been changed.
5653 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
5654 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5655 (let ((a1 (rassq
5656 'tramp-completion-file-name-handler file-name-handler-alist)))
5657 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5658 ;; Add the handlers.
5659 (add-to-list 'file-name-handler-alist
5660 (cons tramp-file-name-regexp 'tramp-file-name-handler))
5661 (put 'tramp-file-name-handler 'safe-magic t)
5662 (add-to-list 'file-name-handler-alist
5663 (cons tramp-completion-file-name-regexp
5664 'tramp-completion-file-name-handler))
5665 (put 'tramp-completion-file-name-handler 'safe-magic t)
5666 ;; If jka-compr or epa-file are already loaded, move them to the
5667 ;; front of `file-name-handler-alist'.
5668 (dolist (fnh '(epa-file-handler jka-compr-handler))
5669 (let ((entry (rassoc fnh file-name-handler-alist)))
5670 (when entry
5671 (setq file-name-handler-alist
5672 (cons entry (delete entry file-name-handler-alist))))))))
5673
5674 ;; `tramp-file-name-handler' must be registered before evaluation of
5675 ;; site-start and init files, because there might exist remote files
5676 ;; already, f.e. files kept via recentf-mode.
5677 ;;;###autoload(tramp-register-file-name-handlers)
5678 (tramp-register-file-name-handlers)
5679
5680 ;;;###autoload
5681 (defun tramp-unload-file-name-handlers ()
5682 (setq file-name-handler-alist
5683 (delete (rassoc 'tramp-file-name-handler
5684 file-name-handler-alist)
5685 (delete (rassoc 'tramp-completion-file-name-handler
5686 file-name-handler-alist)
5687 file-name-handler-alist))))
5688
5689 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
5690
5691 ;;; File name handler functions for completion mode:
5692
5693 (defvar tramp-completion-mode nil
5694 "If non-nil, external packages signal that they are in file name completion.
5695
5696 This is necessary, because Tramp uses a heuristic depending on last
5697 input event. This fails when external packages use other characters
5698 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5699 should never be set globally, the intention is to let-bind it.")
5700
5701 ;; Necessary because `tramp-file-name-regexp-unified' and
5702 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
5703 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5704 ;; to `tramp-file-name-handler'). Otherwise, it takes
5705 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5706 ;; risky, because completing a file might require loading other files,
5707 ;; like "~/.netrc", and for them it shouldn't be decided based on that
5708 ;; variable. On the other hand, those files shouldn't have partial
5709 ;; Tramp file name syntax. Maybe another variable should be introduced
5710 ;; overwriting this check in such cases. Or we change Tramp file name
5711 ;; syntax in order to avoid ambiguities, like in XEmacs ...
5712 (defun tramp-completion-mode-p ()
5713 "Check, whether method / user name / host name completion is active."
5714 (or
5715 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
5716 (and (boundp 'non-essential) (symbol-value 'non-essential))
5717 tramp-completion-mode
5718 ;; Emacs.
5719 (equal last-input-event 'tab)
5720 (and (natnump last-input-event)
5721 (or
5722 ;; ?\t has event-modifier 'control.
5723 (equal last-input-event ?\t)
5724 (and (not (event-modifiers last-input-event))
5725 (or (equal last-input-event ?\?)
5726 (equal last-input-event ?\ )))))
5727 ;; XEmacs.
5728 (and (featurep 'xemacs)
5729 ;; `last-input-event' might be nil.
5730 (not (null last-input-event))
5731 ;; `last-input-event' may have no character approximation.
5732 (funcall (symbol-function 'event-to-character) last-input-event)
5733 (or
5734 ;; ?\t has event-modifier 'control.
5735 (equal
5736 (funcall (symbol-function 'event-to-character)
5737 last-input-event) ?\t)
5738 (and (not (event-modifiers last-input-event))
5739 (or (equal
5740 (funcall (symbol-function 'event-to-character)
5741 last-input-event) ?\?)
5742 (equal
5743 (funcall (symbol-function 'event-to-character)
5744 last-input-event) ?\ )))))))
5745
5746 (defun tramp-connectable-p (filename)
5747 "Check, whether it is possible to connect the remote host w/o side-effects.
5748 This is true, if either the remote host is already connected, or if we are
5749 not in completion mode."
5750 (and (tramp-tramp-file-p filename)
5751 (with-parsed-tramp-file-name filename nil
5752 (or (get-buffer (tramp-buffer-name v))
5753 (not (tramp-completion-mode-p))))))
5754
5755 ;; Method, host name and user name completion.
5756 ;; `tramp-completion-dissect-file-name' returns a list of
5757 ;; tramp-file-name structures. For all of them we return possible completions.
5758 ;;;###autoload
5759 (defun tramp-completion-handle-file-name-all-completions (filename directory)
5760 "Like `file-name-all-completions' for partial Tramp files."
5761
5762 (let* ((fullname (tramp-drop-volume-letter
5763 (expand-file-name filename directory)))
5764 ;; Possible completion structures.
5765 (v (tramp-completion-dissect-file-name fullname))
5766 result result1)
5767
5768 (while v
5769 (let* ((car (car v))
5770 (method (tramp-file-name-method car))
5771 (user (tramp-file-name-user car))
5772 (host (tramp-file-name-host car))
5773 (localname (tramp-file-name-localname car))
5774 (m (tramp-find-method method user host))
5775 (tramp-current-user user) ; see `tramp-parse-passwd'
5776 all-user-hosts)
5777
5778 (unless localname ;; Nothing to complete.
5779
5780 (if (or user host)
5781
5782 ;; Method dependent user / host combinations.
5783 (progn
5784 (mapc
5785 (lambda (x)
5786 (setq all-user-hosts
5787 (append all-user-hosts
5788 (funcall (nth 0 x) (nth 1 x)))))
5789 (tramp-get-completion-function m))
5790
5791 (setq result
5792 (append result
5793 (mapcar
5794 (lambda (x)
5795 (tramp-get-completion-user-host
5796 method user host (nth 0 x) (nth 1 x)))
5797 (delq nil all-user-hosts)))))
5798
5799 ;; Possible methods.
5800 (setq result
5801 (append result (tramp-get-completion-methods m)))))
5802
5803 (setq v (cdr v))))
5804
5805 ;; Unify list, remove nil elements.
5806 (while result
5807 (let ((car (car result)))
5808 (when car
5809 (add-to-list
5810 'result1
5811 (substring car (length (tramp-drop-volume-letter directory)))))
5812 (setq result (cdr result))))
5813
5814 ;; Complete local parts.
5815 (append
5816 result1
5817 (condition-case nil
5818 (apply (if (tramp-connectable-p fullname)
5819 'tramp-completion-run-real-handler
5820 'tramp-run-real-handler)
5821 'file-name-all-completions (list (list filename directory)))
5822 (error nil)))))
5823
5824 ;; Method, host name and user name completion for a file.
5825 ;;;###autoload
5826 (defun tramp-completion-handle-file-name-completion
5827 (filename directory &optional predicate)
5828 "Like `file-name-completion' for Tramp files."
5829 (try-completion
5830 filename
5831 (mapcar 'list (file-name-all-completions filename directory))
5832 (when (and predicate
5833 (tramp-connectable-p (expand-file-name filename directory)))
5834 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
5835
5836 ;; I misuse a little bit the tramp-file-name structure in order to handle
5837 ;; completion possibilities for partial methods / user names / host names.
5838 ;; Return value is a list of tramp-file-name structures according to possible
5839 ;; completions. If "localname" is non-nil it means there
5840 ;; shouldn't be a completion anymore.
5841
5842 ;; Expected results:
5843
5844 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5845 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5846 ;; [nil "x" nil nil]
5847 ;; ["x" nil nil nil]
5848
5849 ;; "/x:" "/x:y" "/x:y:"
5850 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5851 ;; "/[x/" "/[x/y"
5852 ;; ["x" nil "" nil] ["x" nil "y" nil]
5853 ;; ["x" "" nil nil] ["x" "y" nil nil]
5854
5855 ;; "/x:y@" "/x:y@z" "/x:y@z:"
5856 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5857 ;; "/[x/y@" "/[x/y@z"
5858 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
5859 (defun tramp-completion-dissect-file-name (name)
5860 "Returns a list of `tramp-file-name' structures.
5861 They are collected by `tramp-completion-dissect-file-name1'."
5862
5863 (let* ((result)
5864 (x-nil "\\|\\(\\)")
5865 (tramp-completion-ipv6-regexp
5866 (format
5867 "[^%s]*"
5868 (if (zerop (length tramp-postfix-ipv6-format))
5869 tramp-postfix-host-format
5870 tramp-postfix-ipv6-format)))
5871 ;; "/method" "/[method"
5872 (tramp-completion-file-name-structure1
5873 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
5874 1 nil nil nil))
5875 ;; "/user" "/[user"
5876 (tramp-completion-file-name-structure2
5877 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
5878 nil 1 nil nil))
5879 ;; "/host" "/[host"
5880 (tramp-completion-file-name-structure3
5881 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
5882 nil nil 1 nil))
5883 ;; "/[ipv6" "/[ipv6"
5884 (tramp-completion-file-name-structure4
5885 (list (concat tramp-prefix-regexp
5886 tramp-prefix-ipv6-regexp
5887 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5888 nil nil 1 nil))
5889 ;; "/user@host" "/[user@host"
5890 (tramp-completion-file-name-structure5
5891 (list (concat tramp-prefix-regexp
5892 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5893 "\\(" tramp-host-regexp x-nil "\\)$")
5894 nil 1 2 nil))
5895 ;; "/user@[ipv6" "/[user@ipv6"
5896 (tramp-completion-file-name-structure6
5897 (list (concat tramp-prefix-regexp
5898 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5899 tramp-prefix-ipv6-regexp
5900 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5901 nil 1 2 nil))
5902 ;; "/method:user" "/[method/user" "/method://user"
5903 (tramp-completion-file-name-structure7
5904 (list (concat tramp-prefix-regexp
5905 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5906 "\\(" tramp-user-regexp x-nil "\\)$")
5907 1 2 nil nil))
5908 ;; "/method:host" "/[method/host" "/method://host"
5909 (tramp-completion-file-name-structure8
5910 (list (concat tramp-prefix-regexp
5911 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5912 "\\(" tramp-host-regexp x-nil "\\)$")
5913 1 nil 2 nil))
5914 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5915 (tramp-completion-file-name-structure9
5916 (list (concat tramp-prefix-regexp
5917 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5918 tramp-prefix-ipv6-regexp
5919 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5920 1 nil 2 nil))
5921 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
5922 (tramp-completion-file-name-structure10
5923 (list (concat tramp-prefix-regexp
5924 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5925 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5926 "\\(" tramp-host-regexp x-nil "\\)$")
5927 1 2 3 nil))
5928 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5929 (tramp-completion-file-name-structure11
5930 (list (concat tramp-prefix-regexp
5931 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5932 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5933 tramp-prefix-ipv6-regexp
5934 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5935 1 2 3 nil))
5936 ;; "/method: "/method:/"
5937 (tramp-completion-file-name-structure12
5938 (list
5939 (if (equal tramp-syntax 'url)
5940 (concat tramp-prefix-regexp
5941 "\\(" tramp-method-regexp "\\)"
5942 "\\(" (substring tramp-postfix-method-regexp 0 1)
5943 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5944 "\\(" "\\)$")
5945 ;; Should not match if not URL syntax.
5946 (concat tramp-prefix-regexp "/$"))
5947 1 3 nil nil))
5948 ;; "/method: "/method:/"
5949 (tramp-completion-file-name-structure13
5950 (list
5951 (if (equal tramp-syntax 'url)
5952 (concat tramp-prefix-regexp
5953 "\\(" tramp-method-regexp "\\)"
5954 "\\(" (substring tramp-postfix-method-regexp 0 1)
5955 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5956 "\\(" "\\)$")
5957 ;; Should not match if not URL syntax.
5958 (concat tramp-prefix-regexp "/$"))
5959 1 nil 3 nil)))
5960
5961 (mapc (lambda (regexp)
5962 (add-to-list 'result
5963 (tramp-completion-dissect-file-name1 regexp name)))
5964 (list
5965 tramp-completion-file-name-structure1
5966 tramp-completion-file-name-structure2
5967 tramp-completion-file-name-structure3
5968 tramp-completion-file-name-structure4
5969 tramp-completion-file-name-structure5
5970 tramp-completion-file-name-structure6
5971 tramp-completion-file-name-structure7
5972 tramp-completion-file-name-structure8
5973 tramp-completion-file-name-structure9
5974 tramp-completion-file-name-structure10
5975 tramp-completion-file-name-structure11
5976 tramp-completion-file-name-structure12
5977 tramp-completion-file-name-structure13
5978 tramp-file-name-structure))
5979
5980 (delq nil result)))
5981
5982 (defun tramp-completion-dissect-file-name1 (structure name)
5983 "Returns a `tramp-file-name' structure matching STRUCTURE.
5984 The structure consists of remote method, remote user,
5985 remote host and localname (filename on remote host)."
5986
5987 (save-match-data
5988 (when (string-match (nth 0 structure) name)
5989 (let ((method (and (nth 1 structure)
5990 (match-string (nth 1 structure) name)))
5991 (user (and (nth 2 structure)
5992 (match-string (nth 2 structure) name)))
5993 (host (and (nth 3 structure)
5994 (match-string (nth 3 structure) name)))
5995 (localname (and (nth 4 structure)
5996 (match-string (nth 4 structure) name))))
5997 (vector method user host localname)))))
5998
5999 ;; This function returns all possible method completions, adding the
6000 ;; trailing method delimeter.
6001 (defun tramp-get-completion-methods (partial-method)
6002 "Returns all method completions for PARTIAL-METHOD."
6003 (mapcar
6004 (lambda (method)
6005 (and method
6006 (string-match (concat "^" (regexp-quote partial-method)) method)
6007 (tramp-completion-make-tramp-file-name method nil nil nil)))
6008 (mapcar 'car tramp-methods)))
6009
6010 ;; Compares partial user and host names with possible completions.
6011 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
6012 "Returns the most expanded string for user and host name completion.
6013 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
6014 (cond
6015
6016 ((and partial-user partial-host)
6017 (if (and host
6018 (string-match (concat "^" (regexp-quote partial-host)) host)
6019 (string-equal partial-user (or user partial-user)))
6020 (setq user partial-user)
6021 (setq user nil
6022 host nil)))
6023
6024 (partial-user
6025 (setq host nil)
6026 (unless
6027 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
6028 (setq user nil)))
6029
6030 (partial-host
6031 (setq user nil)
6032 (unless
6033 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
6034 (setq host nil)))
6035
6036 (t (setq user nil
6037 host nil)))
6038
6039 (unless (zerop (+ (length user) (length host)))
6040 (tramp-completion-make-tramp-file-name method user host nil)))
6041
6042 (defun tramp-parse-rhosts (filename)
6043 "Return a list of (user host) tuples allowed to access.
6044 Either user or host may be nil."
6045 ;; On Windows, there are problems in completion when
6046 ;; `default-directory' is remote.
6047 (let ((default-directory (tramp-compat-temporary-file-directory))
6048 res)
6049 (when (file-readable-p filename)
6050 (with-temp-buffer
6051 (insert-file-contents filename)
6052 (goto-char (point-min))
6053 (while (not (eobp))
6054 (push (tramp-parse-rhosts-group) res))))
6055 res))
6056
6057 (defun tramp-parse-rhosts-group ()
6058 "Return a (user host) tuple allowed to access.
6059 Either user or host may be nil."
6060 (let ((result)
6061 (regexp
6062 (concat
6063 "^\\(" tramp-host-regexp "\\)"
6064 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6065 (narrow-to-region (point) (tramp-compat-line-end-position))
6066 (when (re-search-forward regexp nil t)
6067 (setq result (append (list (match-string 3) (match-string 1)))))
6068 (widen)
6069 (forward-line 1)
6070 result))
6071
6072 (defun tramp-parse-shosts (filename)
6073 "Return a list of (user host) tuples allowed to access.
6074 User is always nil."
6075 ;; On Windows, there are problems in completion when
6076 ;; `default-directory' is remote.
6077 (let ((default-directory (tramp-compat-temporary-file-directory))
6078 res)
6079 (when (file-readable-p filename)
6080 (with-temp-buffer
6081 (insert-file-contents filename)
6082 (goto-char (point-min))
6083 (while (not (eobp))
6084 (push (tramp-parse-shosts-group) res))))
6085 res))
6086
6087 (defun tramp-parse-shosts-group ()
6088 "Return a (user host) tuple allowed to access.
6089 User is always nil."
6090 (let ((result)
6091 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
6092 (narrow-to-region (point) (tramp-compat-line-end-position))
6093 (when (re-search-forward regexp nil t)
6094 (setq result (list nil (match-string 1))))
6095 (widen)
6096 (or
6097 (> (skip-chars-forward ",") 0)
6098 (forward-line 1))
6099 result))
6100
6101 (defun tramp-parse-sconfig (filename)
6102 "Return a list of (user host) tuples allowed to access.
6103 User is always nil."
6104 ;; On Windows, there are problems in completion when
6105 ;; `default-directory' is remote.
6106 (let ((default-directory (tramp-compat-temporary-file-directory))
6107 res)
6108 (when (file-readable-p filename)
6109 (with-temp-buffer
6110 (insert-file-contents filename)
6111 (goto-char (point-min))
6112 (while (not (eobp))
6113 (push (tramp-parse-sconfig-group) res))))
6114 res))
6115
6116 (defun tramp-parse-sconfig-group ()
6117 "Return a (user host) tuple allowed to access.
6118 User is always nil."
6119 (let ((result)
6120 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
6121 (narrow-to-region (point) (tramp-compat-line-end-position))
6122 (when (re-search-forward regexp nil t)
6123 (setq result (list nil (match-string 1))))
6124 (widen)
6125 (or
6126 (> (skip-chars-forward ",") 0)
6127 (forward-line 1))
6128 result))
6129
6130 (defun tramp-parse-shostkeys (dirname)
6131 "Return a list of (user host) tuples allowed to access.
6132 User is always nil."
6133 ;; On Windows, there are problems in completion when
6134 ;; `default-directory' is remote.
6135 (let* ((default-directory (tramp-compat-temporary-file-directory))
6136 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
6137 (files (when (file-directory-p dirname) (directory-files dirname)))
6138 result)
6139 (while files
6140 (when (string-match regexp (car files))
6141 (push (list nil (match-string 1 (car files))) result))
6142 (setq files (cdr files)))
6143 result))
6144
6145 (defun tramp-parse-sknownhosts (dirname)
6146 "Return a list of (user host) tuples allowed to access.
6147 User is always nil."
6148 ;; On Windows, there are problems in completion when
6149 ;; `default-directory' is remote.
6150 (let* ((default-directory (tramp-compat-temporary-file-directory))
6151 (regexp (concat "^\\(" tramp-host-regexp
6152 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
6153 (files (when (file-directory-p dirname) (directory-files dirname)))
6154 result)
6155 (while files
6156 (when (string-match regexp (car files))
6157 (push (list nil (match-string 1 (car files))) result))
6158 (setq files (cdr files)))
6159 result))
6160
6161 (defun tramp-parse-hosts (filename)
6162 "Return a list of (user host) tuples allowed to access.
6163 User is always nil."
6164 ;; On Windows, there are problems in completion when
6165 ;; `default-directory' is remote.
6166 (let ((default-directory (tramp-compat-temporary-file-directory))
6167 res)
6168 (when (file-readable-p filename)
6169 (with-temp-buffer
6170 (insert-file-contents filename)
6171 (goto-char (point-min))
6172 (while (not (eobp))
6173 (push (tramp-parse-hosts-group) res))))
6174 res))
6175
6176 (defun tramp-parse-hosts-group ()
6177 "Return a (user host) tuple allowed to access.
6178 User is always nil."
6179 (let ((result)
6180 (regexp
6181 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
6182 (narrow-to-region (point) (tramp-compat-line-end-position))
6183 (when (re-search-forward regexp nil t)
6184 (setq result (list nil (match-string 1))))
6185 (widen)
6186 (or
6187 (> (skip-chars-forward " \t") 0)
6188 (forward-line 1))
6189 result))
6190
6191 ;; For su-alike methods it would be desirable to return "root@localhost"
6192 ;; as default. Unfortunately, we have no information whether any user name
6193 ;; has been typed already. So we use `tramp-current-user' as indication,
6194 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
6195 (defun tramp-parse-passwd (filename)
6196 "Return a list of (user host) tuples allowed to access.
6197 Host is always \"localhost\"."
6198 ;; On Windows, there are problems in completion when
6199 ;; `default-directory' is remote.
6200 (let ((default-directory (tramp-compat-temporary-file-directory))
6201 res)
6202 (if (zerop (length tramp-current-user))
6203 '(("root" nil))
6204 (when (file-readable-p filename)
6205 (with-temp-buffer
6206 (insert-file-contents filename)
6207 (goto-char (point-min))
6208 (while (not (eobp))
6209 (push (tramp-parse-passwd-group) res))))
6210 res)))
6211
6212 (defun tramp-parse-passwd-group ()
6213 "Return a (user host) tuple allowed to access.
6214 Host is always \"localhost\"."
6215 (let ((result)
6216 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
6217 (narrow-to-region (point) (tramp-compat-line-end-position))
6218 (when (re-search-forward regexp nil t)
6219 (setq result (list (match-string 1) "localhost")))
6220 (widen)
6221 (forward-line 1)
6222 result))
6223
6224 (defun tramp-parse-netrc (filename)
6225 "Return a list of (user host) tuples allowed to access.
6226 User may be nil."
6227 ;; On Windows, there are problems in completion when
6228 ;; `default-directory' is remote.
6229 (let ((default-directory (tramp-compat-temporary-file-directory))
6230 res)
6231 (when (file-readable-p filename)
6232 (with-temp-buffer
6233 (insert-file-contents filename)
6234 (goto-char (point-min))
6235 (while (not (eobp))
6236 (push (tramp-parse-netrc-group) res))))
6237 res))
6238
6239 (defun tramp-parse-netrc-group ()
6240 "Return a (user host) tuple allowed to access.
6241 User may be nil."
6242 (let ((result)
6243 (regexp
6244 (concat
6245 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
6246 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6247 (narrow-to-region (point) (tramp-compat-line-end-position))
6248 (when (re-search-forward regexp nil t)
6249 (setq result (list (match-string 3) (match-string 1))))
6250 (widen)
6251 (forward-line 1)
6252 result))
6253
6254 (defun tramp-parse-putty (registry)
6255 "Return a list of (user host) tuples allowed to access.
6256 User is always nil."
6257 ;; On Windows, there are problems in completion when
6258 ;; `default-directory' is remote.
6259 (let ((default-directory (tramp-compat-temporary-file-directory))
6260 res)
6261 (with-temp-buffer
6262 (when (zerop (tramp-local-call-process "reg" nil t nil "query" registry))
6263 (goto-char (point-min))
6264 (while (not (eobp))
6265 (push (tramp-parse-putty-group registry) res))))
6266 res))
6267
6268 (defun tramp-parse-putty-group (registry)
6269 "Return a (user host) tuple allowed to access.
6270 User is always nil."
6271 (let ((result)
6272 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
6273 (narrow-to-region (point) (tramp-compat-line-end-position))
6274 (when (re-search-forward regexp nil t)
6275 (setq result (list nil (match-string 1))))
6276 (widen)
6277 (forward-line 1)
6278 result))
6279
6280 ;;; Internal Functions:
6281
6282 (defun tramp-maybe-send-script (vec script name)
6283 "Define in remote shell function NAME implemented as SCRIPT.
6284 Only send the definition if it has not already been done."
6285 (let* ((p (tramp-get-connection-process vec))
6286 (scripts (tramp-get-connection-property p "scripts" nil)))
6287 (unless (member name scripts)
6288 (tramp-message vec 5 "Sending script `%s'..." name)
6289 ;; The script could contain a call of Perl. This is masked with `%s'.
6290 (tramp-send-command-and-check
6291 vec
6292 (format "%s () {\n%s\n}" name
6293 (format script (tramp-get-remote-perl vec))))
6294 (tramp-set-connection-property p "scripts" (cons name scripts))
6295 (tramp-message vec 5 "Sending script `%s'...done." name))))
6296
6297 (defun tramp-set-auto-save ()
6298 (when (and ;; ange-ftp has its own auto-save mechanism
6299 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6300 'tramp-sh-file-name-handler)
6301 auto-save-default)
6302 (auto-save-mode 1)))
6303 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
6304 (add-hook 'tramp-unload-hook
6305 (lambda ()
6306 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
6307
6308 (defun tramp-run-test (switch filename)
6309 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6310 Returns the exit code of the `test' program."
6311 (with-parsed-tramp-file-name filename nil
6312 (tramp-send-command-and-check
6313 v
6314 (format
6315 "%s %s %s"
6316 (tramp-get-test-command v)
6317 switch
6318 (tramp-shell-quote-argument localname)))))
6319
6320 (defun tramp-run-test2 (format-string file1 file2)
6321 "Run `test'-like program on the remote system, given FILE1, FILE2.
6322 FORMAT-STRING contains the program name, switches, and place holders.
6323 Returns the exit code of the `test' program. Barfs if the methods,
6324 hosts, or files, disagree."
6325 (unless (tramp-equal-remote file1 file2)
6326 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6327 (tramp-error
6328 v 'file-error
6329 "tramp-run-test2 only implemented for same method, user, host")))
6330 (with-parsed-tramp-file-name file1 v1
6331 (with-parsed-tramp-file-name file1 v2
6332 (tramp-send-command-and-check
6333 v1
6334 (format format-string
6335 (tramp-shell-quote-argument v1-localname)
6336 (tramp-shell-quote-argument v2-localname))))))
6337
6338 (defun tramp-buffer-name (vec)
6339 "A name for the connection buffer VEC."
6340 ;; We must use `tramp-file-name-real-host', because for gateway
6341 ;; methods the default port will be expanded later on, which would
6342 ;; tamper the name.
6343 (let ((method (tramp-file-name-method vec))
6344 (user (tramp-file-name-user vec))
6345 (host (tramp-file-name-real-host vec)))
6346 (if (not (zerop (length user)))
6347 (format "*tramp/%s %s@%s*" method user host)
6348 (format "*tramp/%s %s*" method host))))
6349
6350 (defun tramp-delete-temp-file-function ()
6351 "Remove temporary files related to current buffer."
6352 (when (stringp tramp-temp-buffer-file-name)
6353 (condition-case nil
6354 (tramp-compat-delete-file tramp-temp-buffer-file-name 'force)
6355 (error nil))))
6356
6357 (add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6358 (add-hook 'tramp-cache-unload-hook
6359 (lambda ()
6360 (remove-hook 'kill-buffer-hook
6361 'tramp-delete-temp-file-function)))
6362
6363 (defun tramp-get-buffer (vec)
6364 "Get the connection buffer to be used for VEC."
6365 (or (get-buffer (tramp-buffer-name vec))
6366 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
6367 (setq buffer-undo-list t)
6368 (setq default-directory
6369 (tramp-make-tramp-file-name
6370 (tramp-file-name-method vec)
6371 (tramp-file-name-user vec)
6372 (tramp-file-name-host vec)
6373 "/"))
6374 (current-buffer))))
6375
6376 (defun tramp-get-connection-buffer (vec)
6377 "Get the connection buffer to be used for VEC.
6378 In case a second asynchronous communication has been started, it is different
6379 from `tramp-get-buffer'."
6380 (or (tramp-get-connection-property vec "process-buffer" nil)
6381 (tramp-get-buffer vec)))
6382
6383 (defun tramp-get-connection-process (vec)
6384 "Get the connection process to be used for VEC.
6385 In case a second asynchronous communication has been started, it is different
6386 from the default one."
6387 (get-process
6388 (or (tramp-get-connection-property vec "process-name" nil)
6389 (tramp-buffer-name vec))))
6390
6391 (defun tramp-debug-buffer-name (vec)
6392 "A name for the debug buffer for VEC."
6393 ;; We must use `tramp-file-name-real-host', because for gateway
6394 ;; methods the default port will be expanded later on, which would
6395 ;; tamper the name.
6396 (let ((method (tramp-file-name-method vec))
6397 (user (tramp-file-name-user vec))
6398 (host (tramp-file-name-real-host vec)))
6399 (if (not (zerop (length user)))
6400 (format "*debug tramp/%s %s@%s*" method user host)
6401 (format "*debug tramp/%s %s*" method host))))
6402
6403 (defconst tramp-debug-outline-regexp
6404 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6405
6406 (defun tramp-get-debug-buffer (vec)
6407 "Get the debug buffer for VEC."
6408 (with-current-buffer
6409 (get-buffer-create (tramp-debug-buffer-name vec))
6410 (when (bobp)
6411 (setq buffer-undo-list t)
6412 ;; Activate `outline-mode'. This runs `text-mode-hook' and
6413 ;; `outline-mode-hook'. We must prevent that local processes
6414 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
6415 ;; Furthermore, `outline-regexp' must have the correct value
6416 ;; already, because it is used by `font-lock-compile-keywords'.
6417 (let ((default-directory (tramp-compat-temporary-file-directory))
6418 (outline-regexp tramp-debug-outline-regexp))
6419 (outline-mode))
6420 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
6421 (set (make-local-variable 'outline-level) 'tramp-outline-level))
6422 (current-buffer)))
6423
6424 (defun tramp-outline-level ()
6425 "Return the depth to which a statement is nested in the outline.
6426 Point must be at the beginning of a header line.
6427
6428 The outline level is equal to the verbosity of the Tramp message."
6429 (1+ (string-to-number (match-string 1))))
6430
6431 (defun tramp-find-executable
6432 (vec progname dirlist &optional ignore-tilde ignore-path)
6433 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6434 First arg VEC specifies the connection, PROGNAME is the program
6435 to search for, and DIRLIST gives the list of directories to
6436 search. If IGNORE-TILDE is non-nil, directory names starting
6437 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6438 only in DIRLIST.
6439
6440 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
6441
6442 This function expects to be in the right *tramp* buffer."
6443 (with-current-buffer (tramp-get-connection-buffer vec)
6444 (let (result)
6445 ;; Check whether the executable is in $PATH. "which(1)" does not
6446 ;; report always a correct error code; therefore we check the
6447 ;; number of words it returns.
6448 (unless ignore-path
6449 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6450 (goto-char (point-min))
6451 (if (looking-at "^\\s-*1$")
6452 (setq result (concat "\\" progname))))
6453 (unless result
6454 (when ignore-tilde
6455 ;; Remove all ~/foo directories from dirlist. In XEmacs,
6456 ;; `remove' is in CL, and we want to avoid CL dependencies.
6457 (let (newdl d)
6458 (while dirlist
6459 (setq d (car dirlist))
6460 (setq dirlist (cdr dirlist))
6461 (unless (char-equal ?~ (aref d 0))
6462 (setq newdl (cons d newdl))))
6463 (setq dirlist (nreverse newdl))))
6464 (tramp-send-command
6465 vec
6466 (format (concat "while read d; "
6467 "do if test -x $d/%s -a -f $d/%s; "
6468 "then echo tramp_executable $d/%s; "
6469 "break; fi; done <<'EOF'\n"
6470 "%s\nEOF")
6471 progname progname progname (mapconcat 'identity dirlist "\n")))
6472 (goto-char (point-max))
6473 (when (search-backward "tramp_executable " nil t)
6474 (skip-chars-forward "^ ")
6475 (skip-chars-forward " ")
6476 (setq result (buffer-substring
6477 (point) (tramp-compat-line-end-position)))))
6478 result)))
6479
6480 (defun tramp-set-remote-path (vec)
6481 "Sets the remote environment PATH to existing directories.
6482 I.e., for each directory in `tramp-remote-path', it is tested
6483 whether it exists and if so, it is added to the environment
6484 variable PATH."
6485 (tramp-message vec 5 (format "Setting $PATH environment variable"))
6486 (tramp-send-command
6487 vec (format "PATH=%s; export PATH"
6488 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
6489
6490 ;; ------------------------------------------------------------
6491 ;; -- Communication with external shell --
6492 ;; ------------------------------------------------------------
6493
6494 (defun tramp-find-file-exists-command (vec)
6495 "Find a command on the remote host for checking if a file exists.
6496 Here, we are looking for a command which has zero exit status if the
6497 file exists and nonzero exit status otherwise."
6498 (let ((existing "/")
6499 (nonexisting
6500 (tramp-shell-quote-argument "/ this file does not exist "))
6501 result)
6502 ;; The algorithm is as follows: we try a list of several commands.
6503 ;; For each command, we first run `$cmd /' -- this should return
6504 ;; true, as the root directory always exists. And then we run
6505 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
6506 ;; does not exist. This should return false. We use the first
6507 ;; command we find that seems to work.
6508 ;; The list of commands to try is as follows:
6509 ;; `ls -d' This works on most systems, but NetBSD 1.4
6510 ;; has a bug: `ls' always returns zero exit
6511 ;; status, even for files which don't exist.
6512 ;; `test -e' Some Bourne shells have a `test' builtin
6513 ;; which does not know the `-e' option.
6514 ;; `/bin/test -e' For those, the `test' binary on disk normally
6515 ;; provides the option. Alas, the binary
6516 ;; is sometimes `/bin/test' and sometimes it's
6517 ;; `/usr/bin/test'.
6518 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
6519 (unless (or
6520 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6521 (zerop (tramp-send-command-and-check
6522 vec (format "%s %s" result existing)))
6523 (not (zerop (tramp-send-command-and-check
6524 vec (format "%s %s" result nonexisting)))))
6525 (and (setq result "/bin/test -e")
6526 (zerop (tramp-send-command-and-check
6527 vec (format "%s %s" result existing)))
6528 (not (zerop (tramp-send-command-and-check
6529 vec (format "%s %s" result nonexisting)))))
6530 (and (setq result "/usr/bin/test -e")
6531 (zerop (tramp-send-command-and-check
6532 vec (format "%s %s" result existing)))
6533 (not (zerop (tramp-send-command-and-check
6534 vec (format "%s %s" result nonexisting)))))
6535 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6536 (zerop (tramp-send-command-and-check
6537 vec (format "%s %s" result existing)))
6538 (not (zerop (tramp-send-command-and-check
6539 vec (format "%s %s" result nonexisting))))))
6540 (tramp-error
6541 vec 'file-error "Couldn't find command to check if file exists"))
6542 result))
6543
6544 ;; CCC test ksh or bash found for tilde expansion?
6545 (defun tramp-find-shell (vec)
6546 "Opens a shell on the remote host which groks tilde expansion."
6547 (unless (tramp-get-connection-property vec "remote-shell" nil)
6548 (let (shell)
6549 (with-current-buffer (tramp-get-buffer vec)
6550 (tramp-send-command vec "echo ~root" t)
6551 (cond
6552 ((or (string-match "^~root$" (buffer-string))
6553 ;; The default shell (ksh93) of OpenSolaris is buggy.
6554 (string-equal (tramp-get-connection-property vec "uname" "")
6555 "SunOS 5.11"))
6556 (setq shell
6557 (or (tramp-find-executable
6558 vec "bash" (tramp-get-remote-path vec) t t)
6559 (tramp-find-executable
6560 vec "ksh" (tramp-get-remote-path vec) t t)))
6561 (unless shell
6562 (tramp-error
6563 vec 'file-error
6564 "Couldn't find a shell which groks tilde expansion"))
6565 ;; Find arguments for this shell.
6566 (let ((alist tramp-sh-extra-args)
6567 item extra-args)
6568 (while (and alist (null extra-args))
6569 (setq item (pop alist))
6570 (when (string-match (car item) shell)
6571 (setq extra-args (cdr item))))
6572 (when extra-args (setq shell (concat shell " " extra-args))))
6573 (tramp-message
6574 vec 5 "Starting remote shell `%s' for tilde expansion..." shell)
6575 (let ((tramp-end-of-output tramp-initial-end-of-output))
6576 (tramp-send-command
6577 vec
6578 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6579 (shell-quote-argument tramp-end-of-output) shell)
6580 t))
6581 ;; Setting prompts.
6582 (tramp-message vec 5 "Setting remote shell prompt...")
6583 (tramp-send-command
6584 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6585 (tramp-send-command vec "PS2=''" t)
6586 (tramp-send-command vec "PS3=''" t)
6587 (tramp-send-command vec "PROMPT_COMMAND=''" t)
6588 (tramp-message vec 5 "Setting remote shell prompt...done"))
6589
6590 (t (tramp-message
6591 vec 5 "Remote `%s' groks tilde expansion, good"
6592 (tramp-get-method-parameter
6593 (tramp-file-name-method vec) 'tramp-remote-sh))
6594 (tramp-set-connection-property
6595 vec "remote-shell"
6596 (tramp-get-method-parameter
6597 (tramp-file-name-method vec) 'tramp-remote-sh))))))))
6598
6599 ;; ------------------------------------------------------------
6600 ;; -- Functions for establishing connection --
6601 ;; ------------------------------------------------------------
6602
6603 ;; The following functions are actions to be taken when seeing certain
6604 ;; prompts from the remote host. See the variable
6605 ;; `tramp-actions-before-shell' for usage of these functions.
6606
6607 (defun tramp-action-login (proc vec)
6608 "Send the login name."
6609 (when (not (stringp tramp-current-user))
6610 (save-window-excursion
6611 (let ((enable-recursive-minibuffers t))
6612 (pop-to-buffer (tramp-get-connection-buffer vec))
6613 (setq tramp-current-user (read-string (match-string 0))))))
6614 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
6615 (with-current-buffer (tramp-get-connection-buffer vec)
6616 (tramp-message vec 6 "\n%s" (buffer-string)))
6617 (tramp-send-string vec tramp-current-user))
6618
6619 (defun tramp-action-password (proc vec)
6620 "Query the user for a password."
6621 (with-current-buffer (process-buffer proc)
6622 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
6623 (tramp-message vec 3 "Sending %s" (match-string 1)))
6624 (tramp-enter-password proc))
6625
6626 (defun tramp-action-succeed (proc vec)
6627 "Signal success in finding shell prompt."
6628 (throw 'tramp-action 'ok))
6629
6630 (defun tramp-action-permission-denied (proc vec)
6631 "Signal permission denied."
6632 (kill-process proc)
6633 (throw 'tramp-action 'permission-denied))
6634
6635 (defun tramp-action-yesno (proc vec)
6636 "Ask the user for confirmation using `yes-or-no-p'.
6637 Send \"yes\" to remote process on confirmation, abort otherwise.
6638 See also `tramp-action-yn'."
6639 (save-window-excursion
6640 (let ((enable-recursive-minibuffers t))
6641 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6642 (unless (yes-or-no-p (match-string 0))
6643 (kill-process proc)
6644 (throw 'tramp-action 'permission-denied))
6645 (with-current-buffer (tramp-get-connection-buffer vec)
6646 (tramp-message vec 6 "\n%s" (buffer-string)))
6647 (tramp-send-string vec "yes"))))
6648
6649 (defun tramp-action-yn (proc vec)
6650 "Ask the user for confirmation using `y-or-n-p'.
6651 Send \"y\" to remote process on confirmation, abort otherwise.
6652 See also `tramp-action-yesno'."
6653 (save-window-excursion
6654 (let ((enable-recursive-minibuffers t))
6655 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6656 (unless (y-or-n-p (match-string 0))
6657 (kill-process proc)
6658 (throw 'tramp-action 'permission-denied))
6659 (with-current-buffer (tramp-get-connection-buffer vec)
6660 (tramp-message vec 6 "\n%s" (buffer-string)))
6661 (tramp-send-string vec "y"))))
6662
6663 (defun tramp-action-terminal (proc vec)
6664 "Tell the remote host which terminal type to use.
6665 The terminal type can be configured with `tramp-terminal-type'."
6666 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
6667 (with-current-buffer (tramp-get-connection-buffer vec)
6668 (tramp-message vec 6 "\n%s" (buffer-string)))
6669 (tramp-send-string vec tramp-terminal-type))
6670
6671 (defun tramp-action-process-alive (proc vec)
6672 "Check, whether a process has finished."
6673 (unless (memq (process-status proc) '(run open))
6674 (throw 'tramp-action 'process-died)))
6675
6676 (defun tramp-action-out-of-band (proc vec)
6677 "Check, whether an out-of-band copy has finished."
6678 (cond ((and (memq (process-status proc) '(stop exit))
6679 (zerop (process-exit-status proc)))
6680 (tramp-message vec 3 "Process has finished.")
6681 (throw 'tramp-action 'ok))
6682 ((or (and (memq (process-status proc) '(stop exit))
6683 (not (zerop (process-exit-status proc))))
6684 (memq (process-status proc) '(signal)))
6685 ;; `scp' could have copied correctly, but set modes could have failed.
6686 ;; This can be ignored.
6687 (with-current-buffer (process-buffer proc)
6688 (goto-char (point-min))
6689 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
6690 (progn
6691 (tramp-message vec 5 "'set mode' error ignored.")
6692 (tramp-message vec 3 "Process has finished.")
6693 (throw 'tramp-action 'ok))
6694 (tramp-message vec 3 "Process has died.")
6695 (throw 'tramp-action 'process-died))))
6696 (t nil)))
6697
6698 ;; Functions for processing the actions.
6699
6700 (defun tramp-process-one-action (proc vec actions)
6701 "Wait for output from the shell and perform one action."
6702 (let (found todo item pattern action)
6703 (while (not found)
6704 ;; Reread output once all actions have been performed.
6705 ;; Obviously, the output was not complete.
6706 (tramp-accept-process-output proc 1)
6707 (setq todo actions)
6708 (while todo
6709 (setq item (pop todo))
6710 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
6711 (setq action (nth 1 item))
6712 (tramp-message
6713 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
6714 (when (tramp-check-for-regexp proc pattern)
6715 (tramp-message vec 5 "Call `%s'" (symbol-name action))
6716 (setq found (funcall action proc vec)))))
6717 found))
6718
6719 (defun tramp-process-actions (proc vec actions &optional timeout)
6720 "Perform actions until success or TIMEOUT."
6721 ;; Enable auth-source and password-cache.
6722 (tramp-set-connection-property vec "first-password-request" t)
6723 (let (exit)
6724 (while (not exit)
6725 (tramp-message proc 3 "Waiting for prompts from remote shell")
6726 (setq exit
6727 (catch 'tramp-action
6728 (if timeout
6729 (with-timeout (timeout)
6730 (tramp-process-one-action proc vec actions))
6731 (tramp-process-one-action proc vec actions)))))
6732 (with-current-buffer (tramp-get-connection-buffer vec)
6733 (tramp-message vec 6 "\n%s" (buffer-string)))
6734 (unless (eq exit 'ok)
6735 (tramp-clear-passwd vec)
6736 (tramp-error-with-buffer
6737 nil vec 'file-error
6738 (cond
6739 ((eq exit 'permission-denied) "Permission denied")
6740 ((eq exit 'process-died) "Process died")
6741 (t "Login failed"))))))
6742
6743 ;; Utility functions.
6744
6745 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
6746 "Like `accept-process-output' for Tramp processes.
6747 This is needed in order to hide `last-coding-system-used', which is set
6748 for process communication also."
6749 (with-current-buffer (process-buffer proc)
6750 (tramp-message proc 10 "%s %s" proc (process-status proc))
6751 (let (buffer-read-only last-coding-system-used)
6752 ;; Under Windows XP, accept-process-output doesn't return
6753 ;; sometimes. So we add an additional timeout.
6754 (with-timeout ((or timeout 1))
6755 (accept-process-output proc timeout timeout-msecs)))
6756 (tramp-message proc 10 "\n%s" (buffer-string))))
6757
6758 (defun tramp-check-for-regexp (proc regexp)
6759 "Check, whether REGEXP is contained in process buffer of PROC.
6760 Erase echoed commands if exists."
6761 (with-current-buffer (process-buffer proc)
6762 (goto-char (point-min))
6763
6764 ;; Check whether we need to remove echo output.
6765 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
6766 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
6767 (let ((begin (match-beginning 0)))
6768 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
6769 ;; Discard echo from remote output.
6770 (tramp-set-connection-property proc "check-remote-echo" nil)
6771 (tramp-message proc 5 "echo-mark found")
6772 (forward-line 1)
6773 (delete-region begin (point))
6774 (goto-char (point-min)))))
6775
6776 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
6777 ;; Sometimes, the echo string is suppressed on the remote side.
6778 (not (string-equal
6779 (substring-no-properties
6780 tramp-echo-mark-marker
6781 0 (min tramp-echo-mark-marker-length (1- (point-max))))
6782 (buffer-substring-no-properties
6783 1 (min (1+ tramp-echo-mark-marker-length) (point-max))))))
6784 ;; No echo to be handled, now we can look for the regexp.
6785 (goto-char (point-min))
6786 (re-search-forward regexp nil t))))
6787
6788 (defun tramp-wait-for-regexp (proc timeout regexp)
6789 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6790 Expects the output of PROC to be sent to the current buffer. Returns
6791 the string that matched, or nil. Waits indefinitely if TIMEOUT is
6792 nil."
6793 (with-current-buffer (process-buffer proc)
6794 (let ((found (tramp-check-for-regexp proc regexp))
6795 (start-time (current-time)))
6796 (cond (timeout
6797 ;; Work around a bug in XEmacs 21, where the timeout
6798 ;; expires faster than it should. This degenerates
6799 ;; to polling for buggy XEmacsen, but oh, well.
6800 (while (and (not found)
6801 (< (tramp-time-diff (current-time) start-time)
6802 timeout))
6803 (with-timeout (timeout)
6804 (while (not found)
6805 (tramp-accept-process-output proc 1)
6806 (unless (memq (process-status proc) '(run open))
6807 (tramp-error-with-buffer
6808 nil proc 'file-error "Process has died"))
6809 (setq found (tramp-check-for-regexp proc regexp))))))
6810 (t
6811 (while (not found)
6812 (tramp-accept-process-output proc 1)
6813 (unless (memq (process-status proc) '(run open))
6814 (tramp-error-with-buffer
6815 nil proc 'file-error "Process has died"))
6816 (setq found (tramp-check-for-regexp proc regexp)))))
6817 (tramp-message proc 6 "\n%s" (buffer-string))
6818 (when (not found)
6819 (if timeout
6820 (tramp-error
6821 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
6822 regexp timeout)
6823 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6824 found)))
6825
6826 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6827 "Wait for shell prompt and barf if none appears.
6828 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6829 seconds. If not, it produces an error message with the given ERROR-ARGS."
6830 (unless
6831 (tramp-wait-for-regexp
6832 proc timeout
6833 (format
6834 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
6835 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6836
6837 ;; We don't call `tramp-send-string' in order to hide the password
6838 ;; from the debug buffer, and because end-of-line handling of the
6839 ;; string.
6840 (defun tramp-enter-password (proc)
6841 "Prompt for a password and send it to the remote end."
6842 (process-send-string
6843 proc (concat (tramp-read-passwd proc)
6844 (or (tramp-get-method-parameter
6845 tramp-current-method
6846 'tramp-password-end-of-line)
6847 tramp-default-password-end-of-line))))
6848
6849 (defun tramp-open-connection-setup-interactive-shell (proc vec)
6850 "Set up an interactive shell.
6851 Mainly sets the prompt and the echo correctly. PROC is the shell
6852 process to set up. VEC specifies the connection."
6853 (let ((tramp-end-of-output tramp-initial-end-of-output))
6854 ;; It is useful to set the prompt in the following command because
6855 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6856 ;; about and thus /bin/sh will display a strange prompt. For
6857 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6858 ;; display the current working directory but /bin/sh will display
6859 ;; a dollar sign. The following command line sets $PS1 to a sane
6860 ;; value, and works under Bourne-ish shells as well as csh-like
6861 ;; shells. Daniel Pittman reports that the unusual positioning of
6862 ;; the single quotes makes it work under `rc', too. We also unset
6863 ;; the variable $ENV because that is read by some sh
6864 ;; implementations (eg, bash when called as sh) on startup; this
6865 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6866 ;; is another way to set the prompt in /bin/bash, it must be
6867 ;; discarded as well.
6868 (tramp-send-command
6869 vec
6870 (format
6871 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6872 (shell-quote-argument tramp-end-of-output)
6873 (tramp-get-method-parameter
6874 (tramp-file-name-method vec) 'tramp-remote-sh))
6875 t)
6876
6877 ;; Disable echo.
6878 (tramp-message vec 5 "Setting up remote shell environment")
6879 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
6880 ;; Check whether the echo has really been disabled. Some
6881 ;; implementations, like busybox of embedded GNU/Linux, don't
6882 ;; support disabling.
6883 (tramp-send-command vec "echo foo" t)
6884 (with-current-buffer (process-buffer proc)
6885 (goto-char (point-min))
6886 (when (looking-at "echo foo")
6887 (tramp-set-connection-property proc "remote-echo" t)
6888 (tramp-message vec 5 "Remote echo still on. Ok.")
6889 ;; Make sure backspaces and their echo are enabled and no line
6890 ;; width magic interferes with them.
6891 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
6892
6893 (tramp-message vec 5 "Setting shell prompt")
6894 (tramp-send-command
6895 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6896 (tramp-send-command vec "PS2=''" t)
6897 (tramp-send-command vec "PS3=''" t)
6898 (tramp-send-command vec "PROMPT_COMMAND=''" t)
6899
6900 ;; Try to set up the coding system correctly.
6901 ;; CCC this can't be the right way to do it. Hm.
6902 (tramp-message vec 5 "Determining coding system")
6903 (tramp-send-command vec "echo foo ; echo bar" t)
6904 (with-current-buffer (process-buffer proc)
6905 (goto-char (point-min))
6906 (if (featurep 'mule)
6907 ;; Use MULE to select the right EOL convention for communicating
6908 ;; with the process.
6909 (let* ((cs (or (funcall (symbol-function 'process-coding-system) proc)
6910 (cons 'undecided 'undecided)))
6911 cs-decode cs-encode)
6912 (when (symbolp cs) (setq cs (cons cs cs)))
6913 (setq cs-decode (car cs))
6914 (setq cs-encode (cdr cs))
6915 (unless cs-decode (setq cs-decode 'undecided))
6916 (unless cs-encode (setq cs-encode 'undecided))
6917 (setq cs-encode (tramp-coding-system-change-eol-conversion
6918 cs-encode 'unix))
6919 (when (search-forward "\r" nil t)
6920 (setq cs-decode (tramp-coding-system-change-eol-conversion
6921 cs-decode 'dos)))
6922 (funcall (symbol-function 'set-buffer-process-coding-system)
6923 cs-decode cs-encode)
6924 (tramp-message
6925 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
6926 ;; Look for ^M and do something useful if found.
6927 (when (search-forward "\r" nil t)
6928 ;; We have found a ^M but cannot frob the process coding system
6929 ;; because we're running on a non-MULE Emacs. Let's try
6930 ;; stty, instead.
6931 (tramp-send-command vec "stty -onlcr" t))))
6932 ;; Dump stty settings in the traces.
6933 (when (>= tramp-verbose 10)
6934 (tramp-send-command vec "stty -a" t))
6935 (tramp-send-command vec "set +o vi +o emacs" t)
6936
6937 ;; Check whether the output of "uname -sr" has been changed. If
6938 ;; yes, this is a strong indication that we must expire all
6939 ;; connection properties. We start again with
6940 ;; `tramp-maybe-open-connection', it will be catched there.
6941 (tramp-message vec 5 "Checking system information")
6942 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
6943 (new-uname
6944 (tramp-set-connection-property
6945 vec "uname"
6946 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
6947 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
6948 (with-current-buffer (tramp-get-debug-buffer vec)
6949 ;; Keep the debug buffer.
6950 (rename-buffer
6951 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
6952 (funcall (symbol-function 'tramp-cleanup-connection) vec)
6953 (if (= (point-min) (point-max))
6954 (kill-buffer nil)
6955 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
6956 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
6957 (tramp-get-buffer vec)
6958 (tramp-message
6959 vec 3
6960 "Connection reset, because remote host changed from `%s' to `%s'"
6961 old-uname new-uname)
6962 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
6963
6964 ;; Check whether the remote host suffers from buggy
6965 ;; `send-process-string'. This is known for FreeBSD (see comment in
6966 ;; `send_process', file process.c). I've tested sending 624 bytes
6967 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
6968 ;; this host type is detected locally. It cannot handle remote
6969 ;; hosts, though.
6970 (with-connection-property proc "chunksize"
6971 (cond
6972 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
6973 tramp-chunksize)
6974 (t
6975 (tramp-message
6976 vec 5 "Checking remote host type for `send-process-string' bug")
6977 (if (string-match
6978 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
6979 500 0))))
6980
6981 ;; Set remote PATH variable.
6982 (tramp-set-remote-path vec)
6983
6984 ;; Search for a good shell before searching for a command which
6985 ;; checks if a file exists. This is done because Tramp wants to use
6986 ;; "test foo; echo $?" to check if various conditions hold, and
6987 ;; there are buggy /bin/sh implementations which don't execute the
6988 ;; "echo $?" part if the "test" part has an error. In particular,
6989 ;; the OpenSolaris /bin/sh is a problem. There are also other
6990 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
6991 ;; in in function declarations, or changing HISTFILE in place.
6992 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
6993 ;; detected.
6994 (tramp-find-shell vec)
6995
6996 ;; Disable unexpected output.
6997 (tramp-send-command vec "mesg n; biff n" t)
6998
6999 ;; Set the environment.
7000 (tramp-message vec 5 "Setting default environment")
7001
7002 (let ((env (copy-sequence tramp-remote-process-environment))
7003 unset item)
7004 (while env
7005 (setq item (tramp-compat-split-string (car env) "="))
7006 (setcdr item (mapconcat 'identity (cdr item) "="))
7007 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
7008 (tramp-send-command
7009 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
7010 (push (car item) unset))
7011 (setq env (cdr env)))
7012 (when unset
7013 (tramp-send-command
7014 vec (format "unset %s" (mapconcat 'identity unset " "))))) t)
7015
7016 ;; CCC: We should either implement a Perl version of base64 encoding
7017 ;; and decoding. Then we just use that in the last item. The other
7018 ;; alternative is to use the Perl version of UU encoding. But then
7019 ;; we need a Lisp version of uuencode.
7020 ;;
7021 ;; Old text from documentation of tramp-methods:
7022 ;; Using a uuencode/uudecode inline method is discouraged, please use one
7023 ;; of the base64 methods instead since base64 encoding is much more
7024 ;; reliable and the commands are more standardized between the different
7025 ;; Unix versions. But if you can't use base64 for some reason, please
7026 ;; note that the default uudecode command does not work well for some
7027 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
7028 ;; the following command for uudecode:
7029 ;;
7030 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7031 ;;
7032 ;; For Irix, no solution is known yet.
7033
7034 (defconst tramp-local-coding-commands
7035 '((b64 base64-encode-region base64-decode-region)
7036 (uu tramp-uuencode-region uudecode-decode-region)
7037 (pack
7038 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7039 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7040 "List of local coding commands for inline transfer.
7041 Each item is a list that looks like this:
7042
7043 \(FORMAT ENCODING DECODING\)
7044
7045 FORMAT is symbol describing the encoding/decoding format. It can be
7046 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7047
7048 ENCODING and DECODING can be strings, giving commands, or symbols,
7049 giving functions. If they are strings, then they can contain
7050 the \"%s\" format specifier. If that specifier is present, the input
7051 filename will be put into the command line at that spot. If the
7052 specifier is not present, the input should be read from standard
7053 input.
7054
7055 If they are functions, they will be called with two arguments, start
7056 and end of region, and are expected to replace the region contents
7057 with the encoded or decoded results, respectively.")
7058
7059 (defconst tramp-remote-coding-commands
7060 '((b64 "base64" "base64 -d")
7061 (b64 "mimencode -b" "mimencode -u -b")
7062 (b64 "mmencode -b" "mmencode -u -b")
7063 (b64 "recode data..base64" "recode base64..data")
7064 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
7065 (b64 tramp-perl-encode tramp-perl-decode)
7066 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7067 (uu "uuencode xxx" "uudecode -o -")
7068 (uu "uuencode xxx" "uudecode -p")
7069 (uu "uuencode xxx" tramp-uudecode)
7070 (pack
7071 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7072 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7073 "List of remote coding commands for inline transfer.
7074 Each item is a list that looks like this:
7075
7076 \(FORMAT ENCODING DECODING\)
7077
7078 FORMAT is symbol describing the encoding/decoding format. It can be
7079 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7080
7081 ENCODING and DECODING can be strings, giving commands, or symbols,
7082 giving variables. If they are strings, then they can contain
7083 the \"%s\" format specifier. If that specifier is present, the input
7084 filename will be put into the command line at that spot. If the
7085 specifier is not present, the input should be read from standard
7086 input.
7087
7088 If they are variables, this variable is a string containing a Perl
7089 implementation for this functionality. This Perl program will be transferred
7090 to the remote host, and it is available as shell function with the same name.")
7091
7092 (defun tramp-find-inline-encoding (vec)
7093 "Find an inline transfer encoding that works.
7094 Goes through the list `tramp-local-coding-commands' and
7095 `tramp-remote-coding-commands'."
7096 (save-excursion
7097 (let ((local-commands tramp-local-coding-commands)
7098 (magic "xyzzy")
7099 loc-enc loc-dec rem-enc rem-dec litem ritem found)
7100 (while (and local-commands (not found))
7101 (setq litem (pop local-commands))
7102 (catch 'wont-work-local
7103 (let ((format (nth 0 litem))
7104 (remote-commands tramp-remote-coding-commands))
7105 (setq loc-enc (nth 1 litem))
7106 (setq loc-dec (nth 2 litem))
7107 ;; If the local encoder or decoder is a string, the
7108 ;; corresponding command has to work locally.
7109 (if (not (stringp loc-enc))
7110 (tramp-message
7111 vec 5 "Checking local encoding function `%s'" loc-enc)
7112 (tramp-message
7113 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
7114 (unless (zerop (tramp-call-local-coding-command
7115 loc-enc nil nil))
7116 (throw 'wont-work-local nil)))
7117 (if (not (stringp loc-dec))
7118 (tramp-message
7119 vec 5 "Checking local decoding function `%s'" loc-dec)
7120 (tramp-message
7121 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
7122 (unless (zerop (tramp-call-local-coding-command
7123 loc-dec nil nil))
7124 (throw 'wont-work-local nil)))
7125 ;; Search for remote coding commands with the same format
7126 (while (and remote-commands (not found))
7127 (setq ritem (pop remote-commands))
7128 (catch 'wont-work-remote
7129 (when (equal format (nth 0 ritem))
7130 (setq rem-enc (nth 1 ritem))
7131 (setq rem-dec (nth 2 ritem))
7132 ;; Check if remote encoding and decoding commands can be
7133 ;; called remotely with null input and output. This makes
7134 ;; sure there are no syntax errors and the command is really
7135 ;; found. Note that we do not redirect stdout to /dev/null,
7136 ;; for two reasons: when checking the decoding command, we
7137 ;; actually check the output it gives. And also, when
7138 ;; redirecting "mimencode" output to /dev/null, then as root
7139 ;; it might change the permissions of /dev/null!
7140 (when (not (stringp rem-enc))
7141 (let ((name (symbol-name rem-enc)))
7142 (while (string-match (regexp-quote "-") name)
7143 (setq name (replace-match "_" nil t name)))
7144 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
7145 (setq rem-enc name)))
7146 (tramp-message
7147 vec 5
7148 "Checking remote encoding command `%s' for sanity" rem-enc)
7149 (unless (zerop (tramp-send-command-and-check
7150 vec (format "%s </dev/null" rem-enc) t))
7151 (throw 'wont-work-remote nil))
7152
7153 (when (not (stringp rem-dec))
7154 (let ((name (symbol-name rem-dec)))
7155 (while (string-match (regexp-quote "-") name)
7156 (setq name (replace-match "_" nil t name)))
7157 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
7158 (setq rem-dec name)))
7159 (tramp-message
7160 vec 5
7161 "Checking remote decoding command `%s' for sanity" rem-dec)
7162 (unless (zerop (tramp-send-command-and-check
7163 vec
7164 (format "echo %s | %s | %s"
7165 magic rem-enc rem-dec)
7166 t))
7167 (throw 'wont-work-remote nil))
7168
7169 (with-current-buffer (tramp-get-buffer vec)
7170 (goto-char (point-min))
7171 (unless (looking-at (regexp-quote magic))
7172 (throw 'wont-work-remote nil)))
7173
7174 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7175 (setq rem-enc (nth 1 ritem))
7176 (setq rem-dec (nth 2 ritem))
7177 (setq found t)))))))
7178
7179 ;; Did we find something?
7180 (unless found
7181 (tramp-error
7182 vec 'file-error "Couldn't find an inline transfer encoding"))
7183
7184 ;; Set connection properties.
7185 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
7186 (tramp-set-connection-property vec "local-encoding" loc-enc)
7187 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
7188 (tramp-set-connection-property vec "local-decoding" loc-dec)
7189 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
7190 (tramp-set-connection-property vec "remote-encoding" rem-enc)
7191 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
7192 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
7193
7194 (defun tramp-call-local-coding-command (cmd input output)
7195 "Call the local encoding or decoding command.
7196 If CMD contains \"%s\", provide input file INPUT there in command.
7197 Otherwise, INPUT is passed via standard input.
7198 INPUT can also be nil which means `/dev/null'.
7199 OUTPUT can be a string (which specifies a filename), or t (which
7200 means standard output and thus the current buffer), or nil (which
7201 means discard it)."
7202 (tramp-local-call-process
7203 tramp-encoding-shell
7204 (when (and input (not (string-match "%s" cmd))) input)
7205 (if (eq output t) t nil)
7206 nil
7207 tramp-encoding-command-switch
7208 (concat
7209 (if (string-match "%s" cmd) (format cmd input) cmd)
7210 (if (stringp output) (concat "> " output) ""))))
7211
7212 (defconst tramp-inline-compress-commands
7213 '(("gzip" "gzip -d")
7214 ("bzip2" "bzip2 -d")
7215 ("compress" "compress -d"))
7216 "List of compress and decompress commands for inline transfer.
7217 Each item is a list that looks like this:
7218
7219 \(COMPRESS DECOMPRESS\)
7220
7221 COMPRESS or DECOMPRESS are strings with the respective commands.")
7222
7223 (defun tramp-find-inline-compress (vec)
7224 "Find an inline transfer compress command that works.
7225 Goes through the list `tramp-inline-compress-commands'."
7226 (save-excursion
7227 (let ((commands tramp-inline-compress-commands)
7228 (magic "xyzzy")
7229 item compress decompress
7230 found)
7231 (while (and commands (not found))
7232 (catch 'next
7233 (setq item (pop commands)
7234 compress (nth 0 item)
7235 decompress (nth 1 item))
7236 (tramp-message
7237 vec 5
7238 "Checking local compress command `%s', `%s' for sanity"
7239 compress decompress)
7240 (unless (zerop (tramp-call-local-coding-command
7241 (format "echo %s | %s | %s"
7242 magic compress decompress) nil nil))
7243 (throw 'next nil))
7244 (tramp-message
7245 vec 5
7246 "Checking remote compress command `%s', `%s' for sanity"
7247 compress decompress)
7248 (unless (zerop (tramp-send-command-and-check
7249 vec (format "echo %s | %s | %s"
7250 magic compress decompress) t))
7251 (throw 'next nil))
7252 (setq found t)))
7253
7254 ;; Did we find something?
7255 (if found
7256 (progn
7257 ;; Set connection properties.
7258 (tramp-message
7259 vec 5 "Using inline transfer compress command `%s'" compress)
7260 (tramp-set-connection-property vec "inline-compress" compress)
7261 (tramp-message
7262 vec 5 "Using inline transfer decompress command `%s'" decompress)
7263 (tramp-set-connection-property vec "inline-decompress" decompress))
7264
7265 (tramp-set-connection-property vec "inline-compress" nil)
7266 (tramp-set-connection-property vec "inline-decompress" nil)
7267 (tramp-message
7268 vec 2 "Couldn't find an inline transfer compress command")))))
7269
7270 (defun tramp-compute-multi-hops (vec)
7271 "Expands VEC according to `tramp-default-proxies-alist'.
7272 Gateway hops are already opened."
7273 (let ((target-alist `(,vec))
7274 (choices tramp-default-proxies-alist)
7275 item proxy)
7276
7277 ;; Look for proxy hosts to be passed.
7278 (while choices
7279 (setq item (pop choices)
7280 proxy (eval (nth 2 item)))
7281 (when (and
7282 ;; host
7283 (string-match (or (eval (nth 0 item)) "")
7284 (or (tramp-file-name-host (car target-alist)) ""))
7285 ;; user
7286 (string-match (or (eval (nth 1 item)) "")
7287 (or (tramp-file-name-user (car target-alist)) "")))
7288 (if (null proxy)
7289 ;; No more hops needed.
7290 (setq choices nil)
7291 ;; Replace placeholders.
7292 (setq proxy
7293 (format-spec
7294 proxy
7295 (format-spec-make
7296 ?u (or (tramp-file-name-user (car target-alist)) "")
7297 ?h (or (tramp-file-name-host (car target-alist)) ""))))
7298 (with-parsed-tramp-file-name proxy l
7299 ;; Add the hop.
7300 (add-to-list 'target-alist l)
7301 ;; Start next search.
7302 (setq choices tramp-default-proxies-alist)))))
7303
7304 ;; Handle gateways.
7305 (when (and (boundp 'tramp-gw-tunnel-method)
7306 (string-match (format
7307 "^\\(%s\\|%s\\)$"
7308 (symbol-value 'tramp-gw-tunnel-method)
7309 (symbol-value 'tramp-gw-socks-method))
7310 (tramp-file-name-method (car target-alist))))
7311 (let ((gw (pop target-alist))
7312 (hop (pop target-alist)))
7313 ;; Is the method prepared for gateways?
7314 (unless (tramp-get-method-parameter
7315 (tramp-file-name-method hop) 'tramp-default-port)
7316 (tramp-error
7317 vec 'file-error
7318 "Method `%s' is not supported for gateway access."
7319 (tramp-file-name-method hop)))
7320 ;; Add default port if needed.
7321 (unless
7322 (string-match
7323 tramp-host-with-port-regexp (tramp-file-name-host hop))
7324 (aset hop 2
7325 (concat
7326 (tramp-file-name-host hop) tramp-prefix-port-format
7327 (number-to-string
7328 (tramp-get-method-parameter
7329 (tramp-file-name-method hop) 'tramp-default-port)))))
7330 ;; Open the gateway connection.
7331 (add-to-list
7332 'target-alist
7333 (vector
7334 (tramp-file-name-method hop) (tramp-file-name-user hop)
7335 (funcall (symbol-function 'tramp-gw-open-connection) vec gw hop) nil))
7336 ;; For the password prompt, we need the correct values.
7337 ;; Therefore, we must remember the gateway vector. But we
7338 ;; cannot do it as connection property, because it shouldn't
7339 ;; be persistent. And we have no started process yet either.
7340 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
7341
7342 ;; Foreign and out-of-band methods are not supported for multi-hops.
7343 (when (cdr target-alist)
7344 (setq choices target-alist)
7345 (while choices
7346 (setq item (pop choices))
7347 (when
7348 (or
7349 (not
7350 (tramp-get-method-parameter
7351 (tramp-file-name-method item) 'tramp-login-program))
7352 (tramp-get-method-parameter
7353 (tramp-file-name-method item) 'tramp-copy-program))
7354 (tramp-error
7355 vec 'file-error
7356 "Method `%s' is not supported for multi-hops."
7357 (tramp-file-name-method item)))))
7358
7359 ;; In case the host name is not used for the remote shell
7360 ;; command, the user could be misguided by applying a random
7361 ;; hostname.
7362 (let* ((v (car target-alist))
7363 (method (tramp-file-name-method v))
7364 (host (tramp-file-name-host v)))
7365 (unless
7366 (or
7367 ;; There are multi-hops.
7368 (cdr target-alist)
7369 ;; The host name is used for the remote shell command.
7370 (member
7371 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7372 ;; The host is local. We cannot use `tramp-local-host-p'
7373 ;; here, because it opens a connection as well.
7374 (string-match tramp-local-host-regexp host))
7375 (tramp-error
7376 v 'file-error
7377 "Host `%s' looks like a remote host, `%s' can only use the local host"
7378 host method)))
7379
7380 ;; Result.
7381 target-alist))
7382
7383 (defun tramp-maybe-open-connection (vec)
7384 "Maybe open a connection VEC.
7385 Does not do anything if a connection is already open, but re-opens the
7386 connection if a previous connection has died for some reason."
7387 (catch 'uname-changed
7388 (let ((p (tramp-get-connection-process vec))
7389 (process-environment (copy-sequence process-environment)))
7390
7391 ;; If too much time has passed since last command was sent, look
7392 ;; whether process is still alive. If it isn't, kill it. When
7393 ;; using ssh, it can sometimes happen that the remote end has
7394 ;; hung up but the local ssh client doesn't recognize this until
7395 ;; it tries to send some data to the remote end. So that's why
7396 ;; we try to send a command from time to time, then look again
7397 ;; whether the process is really alive.
7398 (condition-case nil
7399 (when (and (> (tramp-time-diff
7400 (current-time)
7401 (tramp-get-connection-property
7402 p "last-cmd-time" '(0 0 0)))
7403 60)
7404 p (processp p) (memq (process-status p) '(run open)))
7405 (tramp-send-command vec "echo are you awake" t t)
7406 (unless (and (memq (process-status p) '(run open))
7407 (tramp-wait-for-output p 10))
7408 ;; The error will be catched locally.
7409 (tramp-error vec 'file-error "Awake did fail")))
7410 (file-error
7411 (tramp-flush-connection-property vec)
7412 (tramp-flush-connection-property p)
7413 (delete-process p)
7414 (setq p nil)))
7415
7416 ;; New connection must be opened.
7417 (unless (and p (processp p) (memq (process-status p) '(run open)))
7418
7419 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7420 ;; messages from the beginning.
7421 (tramp-get-buffer vec)
7422 (if (zerop (length (tramp-file-name-user vec)))
7423 (tramp-message
7424 vec 3 "Opening connection for %s using %s..."
7425 (tramp-file-name-host vec)
7426 (tramp-file-name-method vec))
7427 (tramp-message
7428 vec 3 "Opening connection for %s@%s using %s..."
7429 (tramp-file-name-user vec)
7430 (tramp-file-name-host vec)
7431 (tramp-file-name-method vec)))
7432
7433 ;; Start new process.
7434 (when (and p (processp p))
7435 (delete-process p))
7436 (setenv "TERM" tramp-terminal-type)
7437 (setenv "LC_ALL" "C")
7438 (setenv "PROMPT_COMMAND")
7439 (setenv "PS1" tramp-initial-end-of-output)
7440 (let* ((target-alist (tramp-compute-multi-hops vec))
7441 (process-connection-type tramp-process-connection-type)
7442 (process-adaptive-read-buffering nil)
7443 (coding-system-for-read nil)
7444 ;; This must be done in order to avoid our file name handler.
7445 (p (let ((default-directory
7446 (tramp-compat-temporary-file-directory)))
7447 (start-process
7448 (or (tramp-get-connection-property vec "process-name" nil)
7449 (tramp-buffer-name vec))
7450 (tramp-get-connection-buffer vec)
7451 tramp-encoding-shell))))
7452
7453 (tramp-message
7454 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
7455
7456 ;; Check whether process is alive.
7457 (tramp-set-process-query-on-exit-flag p nil)
7458 (with-progress-reporter vec 3 "Waiting 60s for local shell to come up"
7459 (tramp-barf-if-no-shell-prompt
7460 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell))
7461
7462 ;; Now do all the connections as specified.
7463 (while target-alist
7464 (let* ((hop (car target-alist))
7465 (l-method (tramp-file-name-method hop))
7466 (l-user (tramp-file-name-user hop))
7467 (l-host (tramp-file-name-host hop))
7468 (l-port nil)
7469 (login-program
7470 (tramp-get-method-parameter l-method 'tramp-login-program))
7471 (login-args
7472 (tramp-get-method-parameter l-method 'tramp-login-args))
7473 (gw-args
7474 (tramp-get-method-parameter l-method 'tramp-gw-args))
7475 (gw (tramp-get-file-property hop "" "gateway" nil))
7476 (g-method (and gw (tramp-file-name-method gw)))
7477 (g-user (and gw (tramp-file-name-user gw)))
7478 (g-host (and gw (tramp-file-name-host gw)))
7479 (command login-program)
7480 ;; We don't create the temporary file. In fact, it
7481 ;; is just a prefix for the ControlPath option of
7482 ;; ssh; the real temporary file has another name, and
7483 ;; it is created and protected by ssh. It is also
7484 ;; removed by ssh, when the connection is closed.
7485 (tmpfile
7486 (tramp-set-connection-property
7487 p "temp-file"
7488 (make-temp-name
7489 (expand-file-name
7490 tramp-temp-name-prefix
7491 (tramp-compat-temporary-file-directory)))))
7492 spec)
7493
7494 ;; Add gateway arguments if necessary.
7495 (when (and gw gw-args)
7496 (setq login-args (append login-args gw-args)))
7497
7498 ;; Check for port number. Until now, there's no need
7499 ;; for handling like method, user, host.
7500 (when (string-match tramp-host-with-port-regexp l-host)
7501 (setq l-port (match-string 2 l-host)
7502 l-host (match-string 1 l-host)))
7503
7504 ;; Set variables for computing the prompt for reading
7505 ;; password. They can also be derived from a gateway.
7506 (setq tramp-current-method (or g-method l-method)
7507 tramp-current-user (or g-user l-user)
7508 tramp-current-host (or g-host l-host))
7509
7510 ;; Replace login-args place holders.
7511 (setq
7512 l-host (or l-host "")
7513 l-user (or l-user "")
7514 l-port (or l-port "")
7515 spec (format-spec-make ?h l-host ?u l-user ?p l-port ?t tmpfile)
7516 command
7517 (concat
7518 ;; We do not want to see the trailing local prompt in
7519 ;; `start-file-process'.
7520 (unless (memq system-type '(windows-nt)) "exec ")
7521 command " "
7522 (mapconcat
7523 (lambda (x)
7524 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7525 (unless (member "" x) (mapconcat 'identity x " ")))
7526 login-args " ")
7527 ;; Local shell could be a Windows COMSPEC. It doesn't
7528 ;; know the ";" syntax, but we must exit always for
7529 ;; `start-file-process'. "exec" does not work either.
7530 (if (memq system-type '(windows-nt)) " && exit || exit")))
7531
7532 ;; Send the command.
7533 (tramp-message vec 3 "Sending command `%s'" command)
7534 (tramp-send-command vec command t t)
7535 (tramp-process-actions p vec tramp-actions-before-shell 60)
7536 (tramp-message vec 3 "Found remote shell prompt on `%s'" l-host))
7537 ;; Next hop.
7538 (setq target-alist (cdr target-alist)))
7539
7540 ;; Make initial shell settings.
7541 (tramp-open-connection-setup-interactive-shell p vec))))))
7542
7543 (defun tramp-send-command (vec command &optional neveropen nooutput)
7544 "Send the COMMAND to connection VEC.
7545 Erases temporary buffer before sending the command. If optional
7546 arg NEVEROPEN is non-nil, never try to open the connection. This
7547 is meant to be used from `tramp-maybe-open-connection' only. The
7548 function waits for output unless NOOUTPUT is set."
7549 (unless neveropen (tramp-maybe-open-connection vec))
7550 (let ((p (tramp-get-connection-process vec)))
7551 (when (tramp-get-connection-property p "remote-echo" nil)
7552 ;; We mark the command string that it can be erased in the output buffer.
7553 (tramp-set-connection-property p "check-remote-echo" t)
7554 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7555 (tramp-message vec 6 "%s" command)
7556 (tramp-send-string vec command)
7557 (unless nooutput (tramp-wait-for-output p))))
7558
7559 (defun tramp-wait-for-output (proc &optional timeout)
7560 "Wait for output from remote command."
7561 (unless (buffer-live-p (process-buffer proc))
7562 (delete-process proc)
7563 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
7564 (with-current-buffer (process-buffer proc)
7565 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
7566 ;; be leading escape sequences, which must be ignored.
7567 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
7568 ;; Sometimes, the commands do not return a newline but a
7569 ;; null byte before the shell prompt, for example "git
7570 ;; ls-files -c -z ...".
7571 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7572 (found (tramp-wait-for-regexp proc timeout regexp1)))
7573 (if found
7574 (let (buffer-read-only)
7575 ;; A simple-minded busybox has sent " ^H" sequences.
7576 ;; Delete them.
7577 (goto-char (point-min))
7578 (when (re-search-forward
7579 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
7580 (forward-line 1)
7581 (delete-region (point-min) (point)))
7582 ;; Delete the prompt.
7583 (goto-char (point-max))
7584 (re-search-backward regexp nil t)
7585 (delete-region (point) (point-max)))
7586 (if timeout
7587 (tramp-error
7588 proc 'file-error
7589 "[[Remote prompt `%s' not found in %d secs]]"
7590 tramp-end-of-output timeout)
7591 (tramp-error
7592 proc 'file-error
7593 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7594 ;; Return value is whether end-of-output sentinel was found.
7595 found)))
7596
7597 (defun tramp-send-command-and-check
7598 (vec command &optional subshell dont-suppress-err)
7599 "Run COMMAND and check its exit status.
7600 Sends `echo $?' along with the COMMAND for checking the exit status. If
7601 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7602
7603 If the optional argument SUBSHELL is non-nil, the command is
7604 executed in a subshell, ie surrounded by parentheses. If
7605 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
7606 (tramp-send-command
7607 vec
7608 (concat (if subshell "( " "")
7609 command
7610 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
7611 "echo tramp_exit_status $?"
7612 (if subshell " )" "")))
7613 (with-current-buffer (tramp-get-connection-buffer vec)
7614 (goto-char (point-max))
7615 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7616 (tramp-error
7617 vec 'file-error "Couldn't find exit status of `%s'" command))
7618 (skip-chars-forward "^ ")
7619 (prog1
7620 (read (current-buffer))
7621 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7622
7623 (defun tramp-barf-unless-okay (vec command fmt &rest args)
7624 "Run COMMAND, check exit status, throw error if exit status not okay.
7625 Similar to `tramp-send-command-and-check' but accepts two more arguments
7626 FMT and ARGS which are passed to `error'."
7627 (unless (zerop (tramp-send-command-and-check vec command))
7628 (apply 'tramp-error vec 'file-error fmt args)))
7629
7630 (defun tramp-send-command-and-read (vec command)
7631 "Run COMMAND and return the output, which must be a Lisp expression.
7632 In case there is no valid Lisp expression, it raises an error"
7633 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7634 (with-current-buffer (tramp-get-connection-buffer vec)
7635 ;; Read the expression.
7636 (goto-char (point-min))
7637 (condition-case nil
7638 (prog1 (read (current-buffer))
7639 ;; Error handling.
7640 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
7641 (error nil)))
7642 (error (tramp-error
7643 vec 'file-error
7644 "`%s' does not return a valid Lisp expression: `%s'"
7645 command (buffer-string))))))
7646
7647 ;; It seems that Tru64 Unix does not like it if long strings are sent
7648 ;; to it in one go. (This happens when sending the Perl
7649 ;; `file-attributes' implementation, for instance.) Therefore, we
7650 ;; have this function which sends the string in chunks.
7651 (defun tramp-send-string (vec string)
7652 "Send the STRING via connection VEC.
7653
7654 The STRING is expected to use Unix line-endings, but the lines sent to
7655 the remote host use line-endings as defined in the variable
7656 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7657 (let* ((p (tramp-get-connection-process vec))
7658 (chunksize (tramp-get-connection-property p "chunksize" nil)))
7659 (unless p
7660 (tramp-error
7661 vec 'file-error "Can't send string to remote host -- not logged in"))
7662 (tramp-set-connection-property p "last-cmd-time" (current-time))
7663 (tramp-message vec 10 "%s" string)
7664 (with-current-buffer (tramp-get-connection-buffer vec)
7665 ;; Clean up the buffer. We cannot call `erase-buffer' because
7666 ;; narrowing might be in effect.
7667 (let (buffer-read-only) (delete-region (point-min) (point-max)))
7668 ;; Replace "\n" by `tramp-rsh-end-of-line'.
7669 (setq string
7670 (mapconcat 'identity
7671 (tramp-compat-split-string string "\n")
7672 tramp-rsh-end-of-line))
7673 (unless (or (string= string "")
7674 (string-equal (substring string -1) tramp-rsh-end-of-line))
7675 (setq string (concat string tramp-rsh-end-of-line)))
7676 ;; Send the string.
7677 (if (and chunksize (not (zerop chunksize)))
7678 (let ((pos 0)
7679 (end (length string)))
7680 (while (< pos end)
7681 (tramp-message
7682 vec 10 "Sending chunk from %s to %s"
7683 pos (min (+ pos chunksize) end))
7684 (process-send-string
7685 p (substring string pos (min (+ pos chunksize) end)))
7686 (setq pos (+ pos chunksize))))
7687 (process-send-string p string)))))
7688
7689 (defun tramp-mode-string-to-int (mode-string)
7690 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
7691 (let* (case-fold-search
7692 (mode-chars (string-to-vector mode-string))
7693 (owner-read (aref mode-chars 1))
7694 (owner-write (aref mode-chars 2))
7695 (owner-execute-or-setid (aref mode-chars 3))
7696 (group-read (aref mode-chars 4))
7697 (group-write (aref mode-chars 5))
7698 (group-execute-or-setid (aref mode-chars 6))
7699 (other-read (aref mode-chars 7))
7700 (other-write (aref mode-chars 8))
7701 (other-execute-or-sticky (aref mode-chars 9)))
7702 (save-match-data
7703 (logior
7704 (cond
7705 ((char-equal owner-read ?r) (tramp-octal-to-decimal "00400"))
7706 ((char-equal owner-read ?-) 0)
7707 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7708 (cond
7709 ((char-equal owner-write ?w) (tramp-octal-to-decimal "00200"))
7710 ((char-equal owner-write ?-) 0)
7711 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7712 (cond
7713 ((char-equal owner-execute-or-setid ?x)
7714 (tramp-octal-to-decimal "00100"))
7715 ((char-equal owner-execute-or-setid ?S)
7716 (tramp-octal-to-decimal "04000"))
7717 ((char-equal owner-execute-or-setid ?s)
7718 (tramp-octal-to-decimal "04100"))
7719 ((char-equal owner-execute-or-setid ?-) 0)
7720 (t (error "Fourth char `%c' must be one of `xsS-'"
7721 owner-execute-or-setid)))
7722 (cond
7723 ((char-equal group-read ?r) (tramp-octal-to-decimal "00040"))
7724 ((char-equal group-read ?-) 0)
7725 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7726 (cond
7727 ((char-equal group-write ?w) (tramp-octal-to-decimal "00020"))
7728 ((char-equal group-write ?-) 0)
7729 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7730 (cond
7731 ((char-equal group-execute-or-setid ?x)
7732 (tramp-octal-to-decimal "00010"))
7733 ((char-equal group-execute-or-setid ?S)
7734 (tramp-octal-to-decimal "02000"))
7735 ((char-equal group-execute-or-setid ?s)
7736 (tramp-octal-to-decimal "02010"))
7737 ((char-equal group-execute-or-setid ?-) 0)
7738 (t (error "Seventh char `%c' must be one of `xsS-'"
7739 group-execute-or-setid)))
7740 (cond
7741 ((char-equal other-read ?r)
7742 (tramp-octal-to-decimal "00004"))
7743 ((char-equal other-read ?-) 0)
7744 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7745 (cond
7746 ((char-equal other-write ?w) (tramp-octal-to-decimal "00002"))
7747 ((char-equal other-write ?-) 0)
7748 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
7749 (cond
7750 ((char-equal other-execute-or-sticky ?x)
7751 (tramp-octal-to-decimal "00001"))
7752 ((char-equal other-execute-or-sticky ?T)
7753 (tramp-octal-to-decimal "01000"))
7754 ((char-equal other-execute-or-sticky ?t)
7755 (tramp-octal-to-decimal "01001"))
7756 ((char-equal other-execute-or-sticky ?-) 0)
7757 (t (error "Tenth char `%c' must be one of `xtT-'"
7758 other-execute-or-sticky)))))))
7759
7760 (defun tramp-convert-file-attributes (vec attr)
7761 "Convert file-attributes ATTR generated by perl script, stat or ls.
7762 Convert file mode bits to string and set virtual device number.
7763 Return ATTR."
7764 (when attr
7765 ;; Convert last access time.
7766 (unless (listp (nth 4 attr))
7767 (setcar (nthcdr 4 attr)
7768 (list (floor (nth 4 attr) 65536)
7769 (floor (mod (nth 4 attr) 65536)))))
7770 ;; Convert last modification time.
7771 (unless (listp (nth 5 attr))
7772 (setcar (nthcdr 5 attr)
7773 (list (floor (nth 5 attr) 65536)
7774 (floor (mod (nth 5 attr) 65536)))))
7775 ;; Convert last status change time.
7776 (unless (listp (nth 6 attr))
7777 (setcar (nthcdr 6 attr)
7778 (list (floor (nth 6 attr) 65536)
7779 (floor (mod (nth 6 attr) 65536)))))
7780 ;; Convert file size.
7781 (when (< (nth 7 attr) 0)
7782 (setcar (nthcdr 7 attr) -1))
7783 (when (and (floatp (nth 7 attr))
7784 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7785 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7786 ;; Convert file mode bits to string.
7787 (unless (stringp (nth 8 attr))
7788 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7789 (when (stringp (car attr))
7790 (aset (nth 8 attr) 0 ?l)))
7791 ;; Convert directory indication bit.
7792 (when (string-match "^d" (nth 8 attr))
7793 (setcar attr t))
7794 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7795 (when (consp (car attr))
7796 (if (and (stringp (caar attr))
7797 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7798 (setcar attr (match-string 1 (caar attr)))
7799 (setcar attr nil)))
7800 ;; Set file's gid change bit.
7801 (setcar (nthcdr 9 attr)
7802 (if (numberp (nth 3 attr))
7803 (not (= (nth 3 attr)
7804 (tramp-get-remote-gid vec 'integer)))
7805 (not (string-equal
7806 (nth 3 attr)
7807 (tramp-get-remote-gid vec 'string)))))
7808 ;; Convert inode.
7809 (unless (listp (nth 10 attr))
7810 (setcar (nthcdr 10 attr)
7811 (condition-case nil
7812 (cons (floor (nth 10 attr) 65536)
7813 (floor (mod (nth 10 attr) 65536)))
7814 ;; Inodes can be incredible huge. We must hide this.
7815 (error (tramp-get-inode vec)))))
7816 ;; Set virtual device number.
7817 (setcar (nthcdr 11 attr)
7818 (tramp-get-device vec))
7819 attr))
7820
7821 (defun tramp-check-cached-permissions (vec access)
7822 "Check `file-attributes' caches for VEC.
7823 Return t if according to the cache access type ACCESS is known to
7824 be granted."
7825 (let ((result nil)
7826 (offset (cond
7827 ((eq ?r access) 1)
7828 ((eq ?w access) 2)
7829 ((eq ?x access) 3))))
7830 (dolist (suffix '("string" "integer") result)
7831 (setq
7832 result
7833 (or
7834 result
7835 (let ((file-attr
7836 (tramp-get-file-property
7837 vec (tramp-file-name-localname vec)
7838 (concat "file-attributes-" suffix) nil))
7839 (remote-uid
7840 (tramp-get-connection-property
7841 vec (concat "uid-" suffix) nil))
7842 (remote-gid
7843 (tramp-get-connection-property
7844 vec (concat "gid-" suffix) nil)))
7845 (and
7846 file-attr
7847 (or
7848 ;; Not a symlink
7849 (eq t (car file-attr))
7850 (null (car file-attr)))
7851 (or
7852 ;; World accessible.
7853 (eq access (aref (nth 8 file-attr) (+ offset 6)))
7854 ;; User accessible and owned by user.
7855 (and
7856 (eq access (aref (nth 8 file-attr) offset))
7857 (equal remote-uid (nth 2 file-attr)))
7858 ;; Group accessible and owned by user's
7859 ;; principal group.
7860 (and
7861 (eq access (aref (nth 8 file-attr) (+ offset 3)))
7862 (equal remote-gid (nth 3 file-attr)))))))))))
7863
7864 (defun tramp-get-inode (vec)
7865 "Returns the virtual inode number.
7866 If it doesn't exist, generate a new one."
7867 (let ((string (tramp-make-tramp-file-name
7868 (tramp-file-name-method vec)
7869 (tramp-file-name-user vec)
7870 (tramp-file-name-host vec)
7871 "")))
7872 (unless (assoc string tramp-inodes)
7873 (add-to-list 'tramp-inodes
7874 (list string (length tramp-inodes))))
7875 (nth 1 (assoc string tramp-inodes))))
7876
7877 (defun tramp-get-device (vec)
7878 "Returns the virtual device number.
7879 If it doesn't exist, generate a new one."
7880 (let ((string (tramp-make-tramp-file-name
7881 (tramp-file-name-method vec)
7882 (tramp-file-name-user vec)
7883 (tramp-file-name-host vec)
7884 "")))
7885 (unless (assoc string tramp-devices)
7886 (add-to-list 'tramp-devices
7887 (list string (length tramp-devices))))
7888 (cons -1 (nth 1 (assoc string tramp-devices)))))
7889
7890 (defun tramp-file-mode-from-int (mode)
7891 "Turn an integer representing a file mode into an ls(1)-like string."
7892 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
7893 (user (logand (lsh mode -6) 7))
7894 (group (logand (lsh mode -3) 7))
7895 (other (logand (lsh mode -0) 7))
7896 (suid (> (logand (lsh mode -9) 4) 0))
7897 (sgid (> (logand (lsh mode -9) 2) 0))
7898 (sticky (> (logand (lsh mode -9) 1) 0)))
7899 (setq user (tramp-file-mode-permissions user suid "s"))
7900 (setq group (tramp-file-mode-permissions group sgid "s"))
7901 (setq other (tramp-file-mode-permissions other sticky "t"))
7902 (concat type user group other)))
7903
7904 (defun tramp-file-mode-permissions (perm suid suid-text)
7905 "Convert a permission bitset into a string.
7906 This is used internally by `tramp-file-mode-from-int'."
7907 (let ((r (> (logand perm 4) 0))
7908 (w (> (logand perm 2) 0))
7909 (x (> (logand perm 1) 0)))
7910 (concat (or (and r "r") "-")
7911 (or (and w "w") "-")
7912 (or (and suid x suid-text) ; suid, execute
7913 (and suid (upcase suid-text)) ; suid, !execute
7914 (and x "x") "-")))) ; !suid
7915
7916 (defun tramp-decimal-to-octal (i)
7917 "Return a string consisting of the octal digits of I.
7918 Not actually used. Use `(format \"%o\" i)' instead?"
7919 (cond ((< i 0) (error "Cannot convert negative number to octal"))
7920 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
7921 ((zerop i) "0")
7922 (t (concat (tramp-decimal-to-octal (/ i 8))
7923 (number-to-string (% i 8))))))
7924
7925 ;; Kudos to Gerd Moellmann for this suggestion.
7926 (defun tramp-octal-to-decimal (ostr)
7927 "Given a string of octal digits, return a decimal number."
7928 (let ((x (or ostr "")))
7929 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7930 (unless (string-match "\\`[0-7]*\\'" x)
7931 (error "Non-octal junk in string `%s'" x))
7932 (string-to-number ostr 8)))
7933
7934 (defun tramp-shell-case-fold (string)
7935 "Converts STRING to shell glob pattern which ignores case."
7936 (mapconcat
7937 (lambda (c)
7938 (if (equal (downcase c) (upcase c))
7939 (vector c)
7940 (format "[%c%c]" (downcase c) (upcase c))))
7941 string
7942 ""))
7943
7944
7945 ;; ------------------------------------------------------------
7946 ;; -- Tramp file names --
7947 ;; ------------------------------------------------------------
7948 ;; Conversion functions between external representation and
7949 ;; internal data structure. Convenience functions for internal
7950 ;; data structure.
7951
7952 (defun tramp-file-name-p (vec)
7953 "Check, whether VEC is a Tramp object."
7954 (and (vectorp vec) (= 4 (length vec))))
7955
7956 (defun tramp-file-name-method (vec)
7957 "Return method component of VEC."
7958 (and (tramp-file-name-p vec) (aref vec 0)))
7959
7960 (defun tramp-file-name-user (vec)
7961 "Return user component of VEC."
7962 (and (tramp-file-name-p vec) (aref vec 1)))
7963
7964 (defun tramp-file-name-host (vec)
7965 "Return host component of VEC."
7966 (and (tramp-file-name-p vec) (aref vec 2)))
7967
7968 (defun tramp-file-name-localname (vec)
7969 "Return localname component of VEC."
7970 (and (tramp-file-name-p vec) (aref vec 3)))
7971
7972 ;; The user part of a Tramp file name vector can be of kind
7973 ;; "user%domain". Sometimes, we must extract these parts.
7974 (defun tramp-file-name-real-user (vec)
7975 "Return the user name of VEC without domain."
7976 (save-match-data
7977 (let ((user (tramp-file-name-user vec)))
7978 (if (and (stringp user)
7979 (string-match tramp-user-with-domain-regexp user))
7980 (match-string 1 user)
7981 user))))
7982
7983 (defun tramp-file-name-domain (vec)
7984 "Return the domain name of VEC."
7985 (save-match-data
7986 (let ((user (tramp-file-name-user vec)))
7987 (and (stringp user)
7988 (string-match tramp-user-with-domain-regexp user)
7989 (match-string 2 user)))))
7990
7991 ;; The host part of a Tramp file name vector can be of kind
7992 ;; "host#port". Sometimes, we must extract these parts.
7993 (defun tramp-file-name-real-host (vec)
7994 "Return the host name of VEC without port."
7995 (save-match-data
7996 (let ((host (tramp-file-name-host vec)))
7997 (if (and (stringp host)
7998 (string-match tramp-host-with-port-regexp host))
7999 (match-string 1 host)
8000 host))))
8001
8002 (defun tramp-file-name-port (vec)
8003 "Return the port number of VEC."
8004 (save-match-data
8005 (let ((host (tramp-file-name-host vec)))
8006 (and (stringp host)
8007 (string-match tramp-host-with-port-regexp host)
8008 (string-to-number (match-string 2 host))))))
8009
8010 (defun tramp-tramp-file-p (name)
8011 "Return t if NAME is a string with Tramp file name syntax."
8012 (save-match-data
8013 (and (stringp name) (string-match tramp-file-name-regexp name))))
8014
8015 (defun tramp-find-method (method user host)
8016 "Return the right method string to use.
8017 This is METHOD, if non-nil. Otherwise, do a lookup in
8018 `tramp-default-method-alist'."
8019 (or method
8020 (let ((choices tramp-default-method-alist)
8021 lmethod item)
8022 (while choices
8023 (setq item (pop choices))
8024 (when (and (string-match (or (nth 0 item) "") (or host ""))
8025 (string-match (or (nth 1 item) "") (or user "")))
8026 (setq lmethod (nth 2 item))
8027 (setq choices nil)))
8028 lmethod)
8029 tramp-default-method))
8030
8031 (defun tramp-find-user (method user host)
8032 "Return the right user string to use.
8033 This is USER, if non-nil. Otherwise, do a lookup in
8034 `tramp-default-user-alist'."
8035 (or user
8036 (let ((choices tramp-default-user-alist)
8037 luser item)
8038 (while choices
8039 (setq item (pop choices))
8040 (when (and (string-match (or (nth 0 item) "") (or method ""))
8041 (string-match (or (nth 1 item) "") (or host "")))
8042 (setq luser (nth 2 item))
8043 (setq choices nil)))
8044 luser)
8045 tramp-default-user))
8046
8047 (defun tramp-find-host (method user host)
8048 "Return the right host string to use.
8049 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
8050 (or (and (> (length host) 0) host)
8051 tramp-default-host))
8052
8053 (defun tramp-dissect-file-name (name &optional nodefault)
8054 "Return a `tramp-file-name' structure.
8055 The structure consists of remote method, remote user, remote host
8056 and localname (file name on remote host). If NODEFAULT is
8057 non-nil, the file name parts are not expanded to their default
8058 values."
8059 (save-match-data
8060 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
8061 (unless match (error "Not a Tramp file name: %s" name))
8062 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
8063 (user (match-string (nth 2 tramp-file-name-structure) name))
8064 (host (match-string (nth 3 tramp-file-name-structure) name))
8065 (localname (match-string (nth 4 tramp-file-name-structure) name)))
8066 (when (member method '("multi" "multiu"))
8067 (error
8068 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
8069 method))
8070 (when host
8071 (when (string-match tramp-prefix-ipv6-regexp host)
8072 (setq host (replace-match "" nil t host)))
8073 (when (string-match tramp-postfix-ipv6-regexp host)
8074 (setq host (replace-match "" nil t host))))
8075 (if nodefault
8076 (vector method user host localname)
8077 (vector
8078 (tramp-find-method method user host)
8079 (tramp-find-user method user host)
8080 (tramp-find-host method user host)
8081 localname))))))
8082
8083 (defun tramp-equal-remote (file1 file2)
8084 "Check, whether the remote parts of FILE1 and FILE2 are identical.
8085 The check depends on method, user and host name of the files. If
8086 one of the components is missing, the default values are used.
8087 The local file name parts of FILE1 and FILE2 are not taken into
8088 account.
8089
8090 Example:
8091
8092 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
8093
8094 would yield `t'. On the other hand, the following check results in nil:
8095
8096 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
8097 (and (stringp (file-remote-p file1))
8098 (stringp (file-remote-p file2))
8099 (string-equal (file-remote-p file1) (file-remote-p file2))))
8100
8101 (defun tramp-make-tramp-file-name (method user host localname)
8102 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
8103 (concat tramp-prefix-format
8104 (when (not (zerop (length method)))
8105 (concat method tramp-postfix-method-format))
8106 (when (not (zerop (length user)))
8107 (concat user tramp-postfix-user-format))
8108 (when host
8109 (if (string-match tramp-ipv6-regexp host)
8110 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8111 host))
8112 tramp-postfix-host-format
8113 (when localname localname)))
8114
8115 (defun tramp-completion-make-tramp-file-name (method user host localname)
8116 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
8117 It must not be a complete Tramp file name, but as long as there are
8118 necessary only. This function will be used in file name completion."
8119 (concat tramp-prefix-format
8120 (when (not (zerop (length method)))
8121 (concat method tramp-postfix-method-format))
8122 (when (not (zerop (length user)))
8123 (concat user tramp-postfix-user-format))
8124 (when (not (zerop (length host)))
8125 (concat
8126 (if (string-match tramp-ipv6-regexp host)
8127 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8128 host)
8129 tramp-postfix-host-format))
8130 (when localname localname)))
8131
8132 (defun tramp-make-copy-program-file-name (vec)
8133 "Create a file name suitable to be passed to `rcp' and workalikes."
8134 (let ((user (tramp-file-name-user vec))
8135 (host (tramp-file-name-real-host vec))
8136 (localname (tramp-shell-quote-argument
8137 (tramp-file-name-localname vec))))
8138 (if (not (zerop (length user)))
8139 (format "%s@%s:%s" user host localname)
8140 (format "%s:%s" host localname))))
8141
8142 (defun tramp-method-out-of-band-p (vec size)
8143 "Return t if this is an out-of-band method, nil otherwise."
8144 (and
8145 ;; It shall be an out-of-band method.
8146 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
8147 ;; Either the file size is large enough, or (in rare cases) there
8148 ;; does not exist a remote encoding.
8149 (or (null tramp-copy-size-limit)
8150 (> size tramp-copy-size-limit)
8151 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
8152
8153 (defun tramp-local-host-p (vec)
8154 "Return t if this points to the local host, nil otherwise."
8155 ;; We cannot use `tramp-file-name-real-host'. A port is an
8156 ;; indication for an ssh tunnel or alike.
8157 (let ((host (tramp-file-name-host vec)))
8158 (and
8159 (stringp host)
8160 (string-match tramp-local-host-regexp host)
8161 ;; The method shall be applied to one of the shell file name
8162 ;; handler. `tramp-local-host-p' is also called for "smb" and
8163 ;; alike, where it must fail.
8164 (tramp-get-method-parameter
8165 (tramp-file-name-method vec) 'tramp-login-program)
8166 ;; The local temp directory must be writable for the other user.
8167 (file-writable-p
8168 (tramp-make-tramp-file-name
8169 (tramp-file-name-method vec)
8170 (tramp-file-name-user vec)
8171 host
8172 (tramp-compat-temporary-file-directory)))
8173 ;; On some systems, chown runs only for root.
8174 (or (zerop (user-uid))
8175 (zerop (tramp-get-remote-uid vec 'integer))))))
8176
8177 ;; Variables local to connection.
8178
8179 (defun tramp-get-remote-path (vec)
8180 (with-connection-property
8181 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8182 ;; cache the result for the session only. Otherwise, the result
8183 ;; is cached persistently.
8184 (if (memq 'tramp-own-remote-path tramp-remote-path)
8185 (tramp-get-connection-process vec)
8186 vec)
8187 "remote-path"
8188 (let* ((remote-path (copy-tree tramp-remote-path))
8189 (elt1 (memq 'tramp-default-remote-path remote-path))
8190 (elt2 (memq 'tramp-own-remote-path remote-path))
8191 (default-remote-path
8192 (when elt1
8193 (condition-case nil
8194 (tramp-send-command-and-read
8195 vec "echo \\\"`getconf PATH`\\\"")
8196 ;; Default if "getconf" is not available.
8197 (error
8198 (tramp-message
8199 vec 3
8200 "`getconf PATH' not successful, using default value \"%s\"."
8201 "/bin:/usr/bin")
8202 "/bin:/usr/bin"))))
8203 (own-remote-path
8204 (when elt2
8205 (condition-case nil
8206 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
8207 ;; Default if "getconf" is not available.
8208 (error
8209 (tramp-message
8210 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8211 nil)))))
8212
8213 ;; Replace place holder `tramp-default-remote-path'.
8214 (when elt1
8215 (setcdr elt1
8216 (append
8217 (tramp-compat-split-string default-remote-path ":")
8218 (cdr elt1)))
8219 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
8220
8221 ;; Replace place holder `tramp-own-remote-path'.
8222 (when elt2
8223 (setcdr elt2
8224 (append
8225 (tramp-compat-split-string own-remote-path ":")
8226 (cdr elt2)))
8227 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
8228
8229 ;; Remove double entries.
8230 (setq elt1 remote-path)
8231 (while (consp elt1)
8232 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
8233 (setcar elt2 nil))
8234 (setq elt1 (cdr elt1)))
8235
8236 ;; Remove non-existing directories.
8237 (delq
8238 nil
8239 (mapcar
8240 (lambda (x)
8241 (and
8242 (stringp x)
8243 (file-directory-p
8244 (tramp-make-tramp-file-name
8245 (tramp-file-name-method vec)
8246 (tramp-file-name-user vec)
8247 (tramp-file-name-host vec)
8248 x))
8249 x))
8250 remote-path)))))
8251
8252 (defun tramp-get-remote-tmpdir (vec)
8253 (with-connection-property vec "tmp-directory"
8254 (let ((dir (tramp-shell-quote-argument "/tmp")))
8255 (if (and (zerop
8256 (tramp-send-command-and-check
8257 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
8258 (zerop
8259 (tramp-send-command-and-check
8260 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
8261 dir
8262 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
8263
8264 (defun tramp-get-ls-command (vec)
8265 (with-connection-property vec "ls"
8266 (tramp-message vec 5 "Finding a suitable `ls' command")
8267 (or
8268 (catch 'ls-found
8269 (dolist (cmd '("ls" "gnuls" "gls"))
8270 (let ((dl (tramp-get-remote-path vec))
8271 result)
8272 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
8273 ;; Check parameters. On busybox, "ls" output coloring is
8274 ;; enabled by default sometimes. So we try to disable it
8275 ;; when possible. $LS_COLORING is not supported there.
8276 (when (zerop (tramp-send-command-and-check
8277 vec (format "%s -lnd /" result)))
8278 (when (zerop (tramp-send-command-and-check
8279 vec (format "%s --color=never /" result)))
8280 (setq result (concat result " --color=never")))
8281 (throw 'ls-found result))
8282 (setq dl (cdr dl))))))
8283 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
8284
8285 (defun tramp-get-ls-command-with-dired (vec)
8286 (save-match-data
8287 (with-connection-property vec "ls-dired"
8288 (tramp-message vec 5 "Checking, whether `ls --dired' works")
8289 (zerop (tramp-send-command-and-check
8290 vec (format "%s --dired /" (tramp-get-ls-command vec)))))))
8291
8292 (defun tramp-get-test-command (vec)
8293 (with-connection-property vec "test"
8294 (tramp-message vec 5 "Finding a suitable `test' command")
8295 (if (zerop (tramp-send-command-and-check vec "test 0"))
8296 "test"
8297 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
8298
8299 (defun tramp-get-test-nt-command (vec)
8300 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8301 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8302 ;; for otherwise the shell crashes.
8303 (with-connection-property vec "test-nt"
8304 (or
8305 (progn
8306 (tramp-send-command
8307 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
8308 (with-current-buffer (tramp-get-buffer vec)
8309 (goto-char (point-min))
8310 (when (looking-at (regexp-quote tramp-end-of-output))
8311 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
8312 (progn
8313 (tramp-send-command
8314 vec
8315 (format
8316 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8317 (tramp-get-test-command vec)))
8318 "tramp_test_nt %s %s"))))
8319
8320 (defun tramp-get-file-exists-command (vec)
8321 (with-connection-property vec "file-exists"
8322 (tramp-message vec 5 "Finding command to check if file exists")
8323 (tramp-find-file-exists-command vec)))
8324
8325 (defun tramp-get-remote-ln (vec)
8326 (with-connection-property vec "ln"
8327 (tramp-message vec 5 "Finding a suitable `ln' command")
8328 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
8329
8330 (defun tramp-get-remote-perl (vec)
8331 (with-connection-property vec "perl"
8332 (tramp-message vec 5 "Finding a suitable `perl' command")
8333 (let ((result
8334 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
8335 (tramp-find-executable
8336 vec "perl" (tramp-get-remote-path vec)))))
8337 ;; We must check also for some Perl modules.
8338 (when result
8339 (with-connection-property vec "perl-file-spec"
8340 (zerop
8341 (tramp-send-command-and-check
8342 vec (format "%s -e 'use File::Spec;'" result))))
8343 (with-connection-property vec "perl-cwd-realpath"
8344 (zerop
8345 (tramp-send-command-and-check
8346 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
8347 result)))
8348
8349 (defun tramp-get-remote-stat (vec)
8350 (with-connection-property vec "stat"
8351 (tramp-message vec 5 "Finding a suitable `stat' command")
8352 (let ((result (tramp-find-executable
8353 vec "stat" (tramp-get-remote-path vec)))
8354 tmp)
8355 ;; Check whether stat(1) returns usable syntax. %s does not
8356 ;; work on older AIX systems.
8357 (when result
8358 (setq tmp
8359 ;; We don't want to display an error message.
8360 (with-temp-message (or (current-message) "")
8361 (condition-case nil
8362 (tramp-send-command-and-read
8363 vec (format "%s -c '(\"%%N\" %%s)' /" result))
8364 (error nil))))
8365 (unless (and (listp tmp) (stringp (car tmp))
8366 (string-match "^./.$" (car tmp))
8367 (integerp (cadr tmp)))
8368 (setq result nil)))
8369 result)))
8370
8371 (defun tramp-get-remote-readlink (vec)
8372 (with-connection-property vec "readlink"
8373 (tramp-message vec 5 "Finding a suitable `readlink' command")
8374 (let ((result (tramp-find-executable
8375 vec "readlink" (tramp-get-remote-path vec))))
8376 (when (and result
8377 ;; We don't want to display an error message.
8378 (with-temp-message (or (current-message) "")
8379 (condition-case nil
8380 (zerop
8381 (tramp-send-command-and-check
8382 vec (format "%s --canonicalize-missing /" result)))
8383 (error nil))))
8384 result))))
8385
8386 (defun tramp-get-remote-id (vec)
8387 (with-connection-property vec "id"
8388 (tramp-message vec 5 "Finding POSIX `id' command")
8389 (or
8390 (catch 'id-found
8391 (let ((dl (tramp-get-remote-path vec))
8392 result)
8393 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8394 ;; Check POSIX parameter.
8395 (when (zerop (tramp-send-command-and-check
8396 vec (format "%s -u" result)))
8397 (throw 'id-found result))
8398 (setq dl (cdr dl)))))
8399 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
8400
8401 (defun tramp-get-remote-uid (vec id-format)
8402 (with-connection-property vec (format "uid-%s" id-format)
8403 (let ((res (tramp-send-command-and-read
8404 vec
8405 (format "%s -u%s %s"
8406 (tramp-get-remote-id vec)
8407 (if (equal id-format 'integer) "" "n")
8408 (if (equal id-format 'integer)
8409 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8410 ;; The command might not always return a number.
8411 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8412
8413 (defun tramp-get-remote-gid (vec id-format)
8414 (with-connection-property vec (format "gid-%s" id-format)
8415 (let ((res (tramp-send-command-and-read
8416 vec
8417 (format "%s -g%s %s"
8418 (tramp-get-remote-id vec)
8419 (if (equal id-format 'integer) "" "n")
8420 (if (equal id-format 'integer)
8421 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8422 ;; The command might not always return a number.
8423 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8424
8425 (defun tramp-get-local-uid (id-format)
8426 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8427
8428 (defun tramp-get-local-gid (id-format)
8429 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8430
8431 ;; Some predefined connection properties.
8432 (defun tramp-get-inline-compress (vec prop size)
8433 "Return the compress command related to PROP.
8434 PROP is either `inline-compress' or `inline-decompress'. SIZE is
8435 the length of the file to be compressed.
8436
8437 If no corresponding command is found, nil is returned."
8438 (when (and (integerp tramp-inline-compress-start-size)
8439 (> size tramp-inline-compress-start-size))
8440 (with-connection-property vec prop
8441 (tramp-find-inline-compress vec)
8442 (tramp-get-connection-property vec prop nil))))
8443
8444 (defun tramp-get-inline-coding (vec prop size)
8445 "Return the coding command related to PROP.
8446 PROP is either `remote-encoding', `remode-decoding',
8447 `local-encoding' or `local-decoding'.
8448
8449 SIZE is the length of the file to be coded. Depending on SIZE,
8450 compression might be applied.
8451
8452 If no corresponding command is found, nil is returned.
8453 Otherwise, either a string is returned which contains a `%s' mark
8454 to be used for the respective input or output file; or a Lisp
8455 function cell is returned to be applied on a buffer."
8456 (let ((coding
8457 (with-connection-property vec prop
8458 (tramp-find-inline-encoding vec)
8459 (tramp-get-connection-property vec prop nil)))
8460 (prop1 (if (string-match "encoding" prop)
8461 "inline-compress" "inline-decompress"))
8462 compress)
8463 ;; The connection property might have been cached. So we must send
8464 ;; the script to the remote side - maybe.
8465 (when (and coding (symbolp coding) (string-match "remote" prop))
8466 (let ((name (symbol-name coding)))
8467 (while (string-match (regexp-quote "-") name)
8468 (setq name (replace-match "_" nil t name)))
8469 (tramp-maybe-send-script vec (symbol-value coding) name)
8470 (setq coding name)))
8471 (when coding
8472 ;; Check for the `compress' command.
8473 (setq compress (tramp-get-inline-compress vec prop1 size))
8474 ;; Return the value.
8475 (cond
8476 ((and compress (symbolp coding))
8477 (if (string-match "decompress" prop1)
8478 `(lambda (beg end)
8479 (,coding beg end)
8480 (let ((coding-system-for-write 'binary)
8481 (coding-system-for-read 'binary))
8482 (apply
8483 'call-process-region (point-min) (point-max)
8484 (car (split-string ,compress)) t t nil
8485 (cdr (split-string ,compress)))))
8486 `(lambda (beg end)
8487 (let ((coding-system-for-write 'binary)
8488 (coding-system-for-read 'binary))
8489 (apply
8490 'call-process-region beg end
8491 (car (split-string ,compress)) t t nil
8492 (cdr (split-string ,compress))))
8493 (,coding (point-min) (point-max)))))
8494 ((symbolp coding)
8495 coding)
8496 ((and compress (string-match "decoding" prop))
8497 (format "(%s | %s >%%s)" coding compress))
8498 (compress
8499 (format "(%s <%%s | %s)" compress coding))
8500 ((string-match "decoding" prop)
8501 (format "%s >%%s" coding))
8502 (t
8503 (format "%s <%%s" coding))))))
8504
8505 (defun tramp-get-method-parameter (method param)
8506 "Return the method parameter PARAM.
8507 If the `tramp-methods' entry does not exist, return nil."
8508 (let ((entry (assoc param (assoc method tramp-methods))))
8509 (when entry (cadr entry))))
8510
8511 ;; Auto saving to a special directory.
8512
8513 (defun tramp-exists-file-name-handler (operation &rest args)
8514 "Check, whether OPERATION runs a file name handler."
8515 ;; The file name handler is determined on base of either an
8516 ;; argument, `buffer-file-name', or `default-directory'.
8517 (condition-case nil
8518 (let* ((buffer-file-name "/")
8519 (default-directory "/")
8520 (fnha file-name-handler-alist)
8521 (check-file-name-operation operation)
8522 (file-name-handler-alist
8523 (list
8524 (cons "/"
8525 (lambda (operation &rest args)
8526 "Returns OPERATION if it is the one to be checked."
8527 (if (equal check-file-name-operation operation)
8528 operation
8529 (let ((file-name-handler-alist fnha))
8530 (apply operation args))))))))
8531 (equal (apply operation args) operation))
8532 (error nil)))
8533
8534 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8535 (defadvice make-auto-save-file-name
8536 (around tramp-advice-make-auto-save-file-name () activate)
8537 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
8538 (if (tramp-tramp-file-p (buffer-file-name))
8539 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8540 ;; directly, because this would bypass the locking mechanism.
8541 (setq ad-return-value
8542 (tramp-file-name-handler 'make-auto-save-file-name))
8543 ad-do-it))
8544 (add-hook
8545 'tramp-unload-hook
8546 (lambda ()
8547 (ad-remove-advice
8548 'make-auto-save-file-name
8549 'around 'tramp-advice-make-auto-save-file-name)
8550 (ad-activate 'make-auto-save-file-name))))
8551
8552 ;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
8553 ;; umask. This is a security threat.
8554
8555 (defun tramp-set-auto-save-file-modes ()
8556 "Set permissions of autosaved remote files to the original permissions."
8557 (let ((bfn (buffer-file-name)))
8558 (when (and (tramp-tramp-file-p bfn)
8559 (buffer-modified-p)
8560 (stringp buffer-auto-save-file-name)
8561 (not (equal bfn buffer-auto-save-file-name)))
8562 (unless (file-exists-p buffer-auto-save-file-name)
8563 (write-region "" nil buffer-auto-save-file-name))
8564 ;; Permissions should be set always, because there might be an old
8565 ;; auto-saved file belonging to another original file. This could
8566 ;; be a security threat.
8567 (set-file-modes buffer-auto-save-file-name
8568 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
8569
8570 (unless (and (featurep 'xemacs)
8571 (= emacs-major-version 21)
8572 (> emacs-minor-version 4))
8573 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
8574 (add-hook 'tramp-unload-hook
8575 (lambda ()
8576 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
8577
8578 (defun tramp-subst-strs-in-string (alist string)
8579 "Replace all occurrences of the string FROM with TO in STRING.
8580 ALIST is of the form ((FROM . TO) ...)."
8581 (save-match-data
8582 (while alist
8583 (let* ((pr (car alist))
8584 (from (car pr))
8585 (to (cdr pr)))
8586 (while (string-match (regexp-quote from) string)
8587 (setq string (replace-match to t t string)))
8588 (setq alist (cdr alist))))
8589 string))
8590
8591 ;; ------------------------------------------------------------
8592 ;; -- Compatibility functions section --
8593 ;; ------------------------------------------------------------
8594
8595 (defun tramp-read-passwd (proc &optional prompt)
8596 "Read a password from user (compat function).
8597 Consults the auth-source package.
8598 Invokes `password-read' if available, `read-passwd' else."
8599 (let* ((key (tramp-make-tramp-file-name
8600 tramp-current-method tramp-current-user
8601 tramp-current-host ""))
8602 (pw-prompt
8603 (or prompt
8604 (with-current-buffer (process-buffer proc)
8605 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
8606 (format "%s for %s " (capitalize (match-string 1)) key)))))
8607 (with-parsed-tramp-file-name key nil
8608 (prog1
8609 (or
8610 ;; See if auth-sources contains something useful, if it's bound.
8611 (and (boundp 'auth-sources)
8612 (tramp-get-connection-property v "first-password-request" nil)
8613 ;; Try with Tramp's current method.
8614 (funcall (symbol-function 'auth-source-user-or-password)
8615 "password" tramp-current-host tramp-current-method))
8616 ;; Try the password cache.
8617 (when (functionp 'password-read)
8618 (unless (tramp-get-connection-property
8619 v "first-password-request" nil)
8620 (funcall (symbol-function 'password-cache-remove) key))
8621 (let ((password
8622 (funcall (symbol-function 'password-read) pw-prompt key)))
8623 (funcall (symbol-function 'password-cache-add) key password)
8624 password))
8625 ;; Else, get the password interactively.
8626 (read-passwd pw-prompt))
8627 (tramp-set-connection-property v "first-password-request" nil)))))
8628
8629 (defun tramp-clear-passwd (vec)
8630 "Clear password cache for connection related to VEC."
8631 (when (functionp 'password-cache-remove)
8632 (funcall
8633 (symbol-function 'password-cache-remove)
8634 (tramp-make-tramp-file-name
8635 (tramp-file-name-method vec)
8636 (tramp-file-name-user vec)
8637 (tramp-file-name-host vec)
8638 ""))))
8639
8640 ;; Snarfed code from time-date.el and parse-time.el
8641
8642 (defconst tramp-half-a-year '(241 17024)
8643 "Evaluated by \"(days-to-time 183)\".")
8644
8645 (defconst tramp-parse-time-months
8646 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
8647 ("apr" . 4) ("may" . 5) ("jun" . 6)
8648 ("jul" . 7) ("aug" . 8) ("sep" . 9)
8649 ("oct" . 10) ("nov" . 11) ("dec" . 12))
8650 "Alist mapping month names to integers.")
8651
8652 (defun tramp-time-less-p (t1 t2)
8653 "Say whether time value T1 is less than time value T2."
8654 (unless t1 (setq t1 '(0 0)))
8655 (unless t2 (setq t2 '(0 0)))
8656 (or (< (car t1) (car t2))
8657 (and (= (car t1) (car t2))
8658 (< (nth 1 t1) (nth 1 t2)))))
8659
8660 (defun tramp-time-subtract (t1 t2)
8661 "Subtract two time values.
8662 Return the difference in the format of a time value."
8663 (unless t1 (setq t1 '(0 0)))
8664 (unless t2 (setq t2 '(0 0)))
8665 (let ((borrow (< (cadr t1) (cadr t2))))
8666 (list (- (car t1) (car t2) (if borrow 1 0))
8667 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
8668
8669 (defun tramp-time-diff (t1 t2)
8670 "Return the difference between the two times, in seconds.
8671 T1 and T2 are time values (as returned by `current-time' for example)."
8672 ;; Pacify byte-compiler with `symbol-function'.
8673 (cond ((and (fboundp 'subtract-time)
8674 (fboundp 'float-time))
8675 (funcall (symbol-function 'float-time)
8676 (funcall (symbol-function 'subtract-time) t1 t2)))
8677 ((and (fboundp 'subtract-time)
8678 (fboundp 'time-to-seconds))
8679 (funcall (symbol-function 'time-to-seconds)
8680 (funcall (symbol-function 'subtract-time) t1 t2)))
8681 ((fboundp 'itimer-time-difference)
8682 (funcall (symbol-function 'itimer-time-difference)
8683 (if (< (length t1) 3) (append t1 '(0)) t1)
8684 (if (< (length t2) 3) (append t2 '(0)) t2)))
8685 (t
8686 (let ((time (tramp-time-subtract t1 t2)))
8687 (+ (* (car time) 65536.0)
8688 (cadr time)
8689 (/ (or (nth 2 time) 0) 1000000.0))))))
8690
8691 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
8692 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8693 EOL-TYPE can be one of `dos', `unix', or `mac'."
8694 (cond ((fboundp 'coding-system-change-eol-conversion)
8695 (funcall (symbol-function 'coding-system-change-eol-conversion)
8696 coding-system eol-type))
8697 ((fboundp 'subsidiary-coding-system)
8698 (funcall (symbol-function 'subsidiary-coding-system)
8699 coding-system
8700 (cond ((eq eol-type 'dos) 'crlf)
8701 ((eq eol-type 'unix) 'lf)
8702 ((eq eol-type 'mac) 'cr)
8703 (t
8704 (error "Unknown EOL-TYPE `%s', must be %s"
8705 eol-type
8706 "`dos', `unix', or `mac'")))))
8707 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8708
8709 (defun tramp-set-process-query-on-exit-flag (process flag)
8710 "Specify if query is needed for process when Emacs is exited.
8711 If the second argument flag is non-nil, Emacs will query the user before
8712 exiting if process is running."
8713 (if (fboundp 'set-process-query-on-exit-flag)
8714 (funcall (symbol-function 'set-process-query-on-exit-flag) process flag)
8715 (funcall (symbol-function 'process-kill-without-query) process flag)))
8716
8717
8718 ;; ------------------------------------------------------------
8719 ;; -- Kludges section --
8720 ;; ------------------------------------------------------------
8721
8722 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8723 ;; does not deal well with newline characters. Newline is replaced by
8724 ;; backslash newline. But if, say, the string `a backslash newline b'
8725 ;; is passed to a shell, the shell will expand this into "ab",
8726 ;; completely omitting the newline. This is not what was intended.
8727 ;; It does not appear to be possible to make the function
8728 ;; `shell-quote-argument' work with newlines without making it
8729 ;; dependent on the shell used. But within this package, we know that
8730 ;; we will always use a Bourne-like shell, so we use an approach which
8731 ;; groks newlines.
8732 ;;
8733 ;; The approach is simple: we call `shell-quote-argument', then
8734 ;; massage the newline part of the result.
8735 ;;
8736 ;; This function should produce a string which is grokked by a Unix
8737 ;; shell, even if the Emacs is running on Windows. Since this is the
8738 ;; kludges section, we bind `system-type' in such a way that
8739 ;; `shell-quote-arguments' behaves as if on Unix.
8740 ;;
8741 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8742 ;; function to work with Bourne-like shells.
8743 ;;
8744 ;; CCC: This function should be rewritten so that
8745 ;; `shell-quote-argument' is not used. This way, we are safe from
8746 ;; changes in `shell-quote-argument'.
8747 (defun tramp-shell-quote-argument (s)
8748 "Similar to `shell-quote-argument', but groks newlines.
8749 Only works for Bourne-like shells."
8750 (let ((system-type 'not-windows))
8751 (save-match-data
8752 (let ((result (shell-quote-argument s))
8753 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
8754 (when (and (>= (length result) 2)
8755 (string= (substring result 0 2) "\\~"))
8756 (setq result (substring result 1)))
8757 (while (string-match nl result)
8758 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
8759 t t result)))
8760 result))))
8761
8762 ;; Checklist for `tramp-unload-hook'
8763 ;; - Unload all `tramp-*' packages
8764 ;; - Reset `file-name-handler-alist'
8765 ;; - Cleanup hooks where Tramp functions are in
8766 ;; - Cleanup advised functions
8767 ;; - Cleanup autoloads
8768 ;;;###autoload
8769 (defun tramp-unload-tramp ()
8770 "Discard Tramp from loading remote files."
8771 (interactive)
8772 ;; When Tramp is not loaded yet, its autoloads are still active.
8773 (tramp-unload-file-name-handlers)
8774 ;; ange-ftp settings must be enabled.
8775 (when (functionp 'tramp-ftp-enable-ange-ftp)
8776 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
8777 ;; Maybe its not loaded yet.
8778 (condition-case nil
8779 (unload-feature 'tramp 'force)
8780 (error nil)))
8781
8782 (when (and load-in-progress
8783 (string-match "Loading tramp..." (or (current-message) "")))
8784 (message "Loading tramp...done"))
8785
8786 (provide 'tramp)
8787
8788 ;;; TODO:
8789
8790 ;; * Handle nonlocal exits such as C-g.
8791 ;; * But it would probably be better to use with-local-quit at the
8792 ;; place where it's actually needed: around any potentially
8793 ;; indefinitely blocking piece of code. In this case it would be
8794 ;; within Tramp around one of its calls to accept-process-output (or
8795 ;; around one of the loops that calls accept-process-output)
8796 ;; (Stefan Monnier).
8797 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
8798 ;; `shell-quote-argument'.
8799 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
8800 ;; by the files in that directory. Add this here.
8801 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8802 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
8803 ;; * Case-insensitive filename completion. (Norbert Goevert.)
8804 ;; * Don't use globbing for directories with many files, as this is
8805 ;; likely to produce long command lines, and some shells choke on
8806 ;; long command lines.
8807 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
8808 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8809 ;; * abbreviate-file-name
8810 ;; * Better error checking. At least whenever we see something
8811 ;; strange when doing zerop, we should kill the process and start
8812 ;; again. (Greg Stark)
8813 ;; * Remove unneeded parameters from methods.
8814 ;; * Make it work for different encodings, and for different file name
8815 ;; encodings, too. (Daniel Pittman)
8816 ;; * Don't search for perl5 and perl. Instead, only search for perl and
8817 ;; then look if it's the right version (with `perl -v').
8818 ;; * When editing a remote CVS controlled file as a different user, VC
8819 ;; gets confused about the file locking status. Try to find out why
8820 ;; the workaround doesn't work.
8821 ;; * Username and hostname completion.
8822 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8823 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
8824 ;; Code is nearly identical.
8825 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8826 ;; until the last but one hop via `start-file-process'. Apply it
8827 ;; also for ftp and smb.
8828 ;; * WIBNI if we had a command "trampclient"? If I was editing in
8829 ;; some shell with root priviledges, it would be nice if I could
8830 ;; just call
8831 ;; trampclient filename.c
8832 ;; as an editor, and the _current_ shell would connect to an Emacs
8833 ;; server and would be used in an existing non-priviledged Emacs
8834 ;; session for doing the editing in question.
8835 ;; That way, I need not tell Emacs my password again and be afraid
8836 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
8837 ;; once display a just typed password in the context of a keyboard
8838 ;; sequence prompt for a question immediately following in a shell
8839 ;; script run within Emacs -- nasty).
8840 ;; And if I have some ssh session running to a different computer,
8841 ;; having the possibility of passing a local file there to a local
8842 ;; Emacs session (in case I can arrange for a connection back) would
8843 ;; be nice.
8844 ;; Likely the corresponding Tramp server should not allow the
8845 ;; equivalent of the emacsclient -eval option in order to make this
8846 ;; reasonably unproblematic. And maybe trampclient should have some
8847 ;; way of passing credentials, like by using an SSL socket or
8848 ;; something. (David Kastrup)
8849 ;; * Reconnect directly to a compliant shell without first going
8850 ;; through the user's default shell. (Pete Forman)
8851 ;; * Make `tramp-default-user' obsolete.
8852 ;; * How can I interrupt the remote process with a signal
8853 ;; (interrupt-process seems not to work)? (Markus Triska)
8854 ;; * Avoid the local shell entirely for starting remote processes. If
8855 ;; so, I think even a signal, when delivered directly to the local
8856 ;; SSH instance, would correctly be propagated to the remote process
8857 ;; automatically; possibly SSH would have to be started with
8858 ;; "-t". (Markus Triska)
8859 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
8860 ;; isn't on the remote host. (Mark A. Hershberger)
8861 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
8862 ;; * Implement a general server-local-variable mechanism, as there are
8863 ;; probably other variables that need different values for different
8864 ;; servers too. The user could then configure a variable (such as
8865 ;; tramp-server-local-variable-alist) to define any such variables
8866 ;; that they need to, which would then be let bound as appropriate
8867 ;; in tramp functions. (Jason Rumney)
8868 ;; * Optimize out-of-band copying, when both methods are scp-like (not
8869 ;; rsync).
8870 ;; * Keep a second connection open for out-of-band methods like scp or
8871 ;; rsync.
8872 ;; * Support ptys in `tramp-handle-start-file-process'. (Bug#4604)
8873 ;; * IMHO, it's a drawback that currently Tramp doesn't support
8874 ;; Unicode in Dired file names by default. Is it possible to
8875 ;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8876 ;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
8877 ;; expects only English messages? (Juri Linkov)
8878 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8879 ;; * Do not handle files with drive letter as remote. (Bug#5447)
8880 ;; * Load Tramp subpackages only when needed. (Bug#1529, Bug#5448, Bug#5705)
8881 ;; * Try telnet+curl as new method. It might be useful for busybox,
8882 ;; without built-in uuencode/uudecode.
8883 ;; * Let `shell-dynamic-complete-*' and `comint-dynamic-complete' work
8884 ;; on remote hosts.
8885 ;; * Use secrets.el for password handling.
8886 ;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
8887
8888 ;; Functions for file-name-handler-alist:
8889 ;; diff-latest-backup-file -- in diff.el
8890
8891 ;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
8892 ;;; tramp.el ends here
8893
8894 ;; Local Variables:
8895 ;; mode: Emacs-Lisp
8896 ;; coding: utf-8
8897 ;; End: