* Makefile.in (install-arch-indep): Delete any old info .gz files first.
[bpt/emacs.git] / lisp / net / tramp.el
CommitLineData
b1a2b924 1;;; tramp.el --- Transparent Remote Access, Multiple Protocol
fb7933a3 2
5fd6d89f 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
fb7933a3 5
cbd12ed7
GM
6;; (copyright statements below in code to be updated with the above notice)
7
cdd44874 8;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
d2a2c17f 9;; Michael Albinus <michael.albinus@gmx.de>
fb7933a3
KG
10;; Keywords: comm, processes
11
12;; This file is part of GNU Emacs.
13
874a927a 14;; GNU Emacs is free software: you can redistribute it and/or modify
fb7933a3 15;; it under the terms of the GNU General Public License as published by
874a927a
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
fb7933a3
KG
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
874a927a 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fb7933a3
KG
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;;
b1a2b924 34;; For more detailed instructions, please see the info file.
fb7933a3
KG
35;;
36;; Notes:
37;; -----
bf247b6e 38;;
b533bc97 39;; This package only works for Emacs 22.1 and higher, and for XEmacs 21.4
00d6fd04 40;; and higher. For XEmacs 21, you need the package `fsf-compat' for
7f4d4a97 41;; the `with-timeout' macro.
fb7933a3 42;;
fb7933a3
KG
43;; Also see the todo list at the bottom of this file.
44;;
b1a2b924 45;; The current version of Tramp can be retrieved from the following URL:
340b8d4f 46;; http://ftp.gnu.org/gnu/tramp/
fb7933a3
KG
47;;
48;; There's a mailing list for this, as well. Its name is:
340b8d4f
MA
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
fb7933a3
KG
52;;
53;; For the adventurous, the current development sources are available
54;; via CVS. You can find instructions about this at the following URL:
c62c9d08 55;; http://savannah.gnu.org/projects/tramp/
fb7933a3
KG
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
ccb4a481
MA
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
b1a2b924
KG
69;; The Tramp version number and bug report address, as prepared by configure.
70(require 'trampver)
a69c01a0 71(add-hook 'tramp-unload-hook
aa485f7c
MA
72 (lambda ()
73 (when (featurep 'trampver)
74 (unload-feature 'trampver 'force))))
a69c01a0 75
9e6ab520 76(require 'tramp-compat)
94be87e8 77(add-hook 'tramp-unload-hook
aa485f7c
MA
78 (lambda ()
79 (when (featurep 'tramp-compat)
80 (unload-feature 'tramp-compat 'force))))
fb7933a3 81
b533bc97 82(require 'format-spec)
5ec2cc41
KG
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)
fd48cd18
GM
87 (or (require 'password-cache nil 'noerror)
88 (require 'password nil 'noerror))) ; from No Gnus, also in tar ball
5ec2cc41 89
fb7933a3
KG
90(require 'shell)
91(require 'advice)
92
bcb04d98
GM
93(eval-and-compile
94 (if (featurep 'xemacs)
95 (load "auth-source" 'noerror)
96 (require 'auth-source nil 'noerror)))
5615d63f 97
00d6fd04
MA
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")
00d6fd04
MA
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
aa485f7c
MA
108 (lambda ()
109 (when (featurep 'tramp-cache)
110 (unload-feature 'tramp-cache 'force))))
00d6fd04 111
16674e4f
KG
112(autoload 'tramp-uuencode-region "tramp-uu"
113 "Implementation of `uuencode' in Lisp.")
a69c01a0 114(add-hook 'tramp-unload-hook
aa485f7c
MA
115 (lambda ()
116 (when (featurep 'tramp-uu)
117 (unload-feature 'tramp-uu 'force))))
16674e4f 118
00d6fd04 119(autoload 'uudecode-decode-region "uudecode")
16674e4f 120
0664ff72
MA
121;; The following Tramp packages must be loaded after tramp.el, because
122;; they require it as well.
00d6fd04 123(eval-after-load "tramp"
9c13938d
MA
124 '(dolist
125 (feature
126 (list
127
0664ff72 128 ;; Tramp interactive commands.
9c13938d
MA
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
00d6fd04 142
70c11b0b 143 ;; tramp-gvfs needs D-Bus messages. Available since Emacs 23
8e754ea2
MA
144 ;; on some system types. We don't call `dbus-ping', because
145 ;; this would load dbus.el.
70c11b0b 146 (when (and (featurep 'dbusbind)
2ac33804 147 (condition-case nil
0d5852cf 148 (tramp-compat-funcall 'dbus-get-unique-name :session)
2ac33804 149 (error nil))
70c11b0b
MA
150 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
151 'tramp-gvfs)
152
9c13938d 153 ;; Load gateways. It needs `make-network-process' from Emacs 22.
03db0efc
MA
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)))
9c13938d
MA
160
161 (when feature
70c11b0b
MA
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)))))
fb7933a3
KG
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."
589d30dd 178 :group 'files
26f4b8ab 179 :group 'comm
bf247b6e 180 :version "22.1")
fb7933a3 181
2e271195
MA
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.
186If it is set to nil, all remote file names are used literally."
187 :group 'tramp
188 :type 'boolean)
189
00d6fd04 190(defcustom tramp-verbose 3
263c02ef 191 "*Verbosity level for Tramp messages.
00d6fd04
MA
192Any 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
20310 traces (huge)."
fb7933a3
KG
204 :group 'tramp
205 :type 'integer)
206
263c02ef 207;; Emacs case.
38c65fca
KG
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.
212Each element looks like (REGEXP . DIRECTORY), with the same meaning like
213in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
214is a local file name, the backup directory is prepended with Tramp file
00d6fd04 215name prefix \(method, user, host\) of file.
38c65fca
KG
216
217\(setq tramp-backup-directory-alist backup-directory-alist\)
218
219gives the same backup policy for Tramp files on their hosts like the
220policy 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 ...))
231It 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
233file name, the backup directory is prepended with Tramp file name prefix
00d6fd04 234\(method, user, host\) of file.
38c65fca
KG
235
236\(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
237
238gives the same backup policy for Tramp files on their hosts like the
239policy for local files."
bf247b6e 240 :type '(repeat
38c65fca
KG
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
fb7933a3
KG
250(defcustom tramp-auto-save-directory nil
251 "*Put auto-save files in this directory, if set.
252The idea is to use a local directory so that auto-saving is faster."
253 :group 'tramp
00d6fd04 254 :type '(choice (const nil) string))
fb7933a3 255
16674e4f
KG
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.
261This shell is used to execute the encoding and decoding command on the
262local host, so if you want to use `~' in those commands, you should
263choose a shell here which groks tilde expansion. `/bin/sh' normally
264does not understand tilde expansion.
265
266For encoding and deocding, commands like the following are executed:
267
268 /bin/sh -c COMMAND < INPUT > OUTPUT
269
270This variable can be used to change the \"/bin/sh\" part. See the
00d6fd04 271variable `tramp-encoding-command-switch' for the \"-c\" part.
fb7933a3
KG
272
273Note that this variable is not used for remote commands. There are
274mechanisms in tramp.el which automatically determine the right shell to
275use for the remote host."
276 :group 'tramp
277 :type '(file :must-match t))
278
16674e4f
KG
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.
284See the variable `tramp-encoding-shell' for more information."
285 :group 'tramp
286 :type 'string)
287
6a29a838
MA
288(defcustom tramp-inline-compress-start-size 4096
289 "*The minimum size of compressing where inline transfer.
290When inline transfer, compress transfered data of file
291whose size is this value or above (up to `tramp-copy-size-limit').
292If it is nil, no compression at all will be applied."
293 :group 'tramp
294 :type '(choice (const nil) integer))
295
00d6fd04 296(defcustom tramp-copy-size-limit 10240
6a29a838
MA
297 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
298If it is nil, inline out-of-the-band copy will be used without a check."
16674e4f 299 :group 'tramp
6a29a838 300 :type '(choice (const nil) integer))
90dc758d 301
00d6fd04
MA
302(defcustom tramp-terminal-type "dumb"
303 "*Value of TERM environment variable for logging in to remote host.
304Because Tramp wants to parse the output of the remote shell, it is easily
305confused by ANSI color escape sequences and suchlike. Often, shell init
306files conditionalize this setup based on the TERM environment variable."
90dc758d 307 :group 'tramp
00d6fd04 308 :type 'string)
90dc758d 309
dab816a9
MA
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.
319The '$' character at the end is quoted; the string cannot be
320detected as prompt when being sent on echoing hosts, therefore.")
321
322(defconst tramp-initial-end-of-output "#$ "
323 "Prompt when establishing a connection.")
324
00d6fd04
MA
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")
263c02ef 330 (tramp-copy-args (("-p" "%k") ("-r")))
00d6fd04 331 (tramp-copy-keep-date t)
263c02ef 332 (tramp-copy-recursive t)
00d6fd04
MA
333 (tramp-password-end-of-line nil))
334 ("scp" (tramp-login-program "ssh")
c04b03f8 335 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 336 ("-e" "none")))
a7723e05 337 (tramp-async-args (("-q")))
00d6fd04
MA
338 (tramp-remote-sh "/bin/sh")
339 (tramp-copy-program "scp")
263c02ef
MA
340 (tramp-copy-args (("-P" "%p") ("-p" "%k")
341 ("-q") ("-r")))
00d6fd04 342 (tramp-copy-keep-date t)
263c02ef 343 (tramp-copy-recursive t)
00d6fd04
MA
344 (tramp-password-end-of-line nil)
345 (tramp-gw-args (("-o"
346 "GlobalKnownHostsFile=/dev/null")
347 ("-o" "UserKnownHostsFile=/dev/null")
348 ("-o" "StrictHostKeyChecking=no")))
349 (tramp-default-port 22))
350 ("scp1" (tramp-login-program "ssh")
c04b03f8 351 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 352 ("-1" "-e" "none")))
a7723e05 353 (tramp-async-args (("-q")))
00d6fd04
MA
354 (tramp-remote-sh "/bin/sh")
355 (tramp-copy-program "scp")
356 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
263c02ef 357 ("-q") ("-r")))
00d6fd04 358 (tramp-copy-keep-date t)
263c02ef 359 (tramp-copy-recursive t)
00d6fd04
MA
360 (tramp-password-end-of-line nil)
361 (tramp-gw-args (("-o"
362 "GlobalKnownHostsFile=/dev/null")
363 ("-o" "UserKnownHostsFile=/dev/null")
364 ("-o" "StrictHostKeyChecking=no")))
365 (tramp-default-port 22))
366 ("scp2" (tramp-login-program "ssh")
c04b03f8 367 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 368 ("-2" "-e" "none")))
a7723e05 369 (tramp-async-args (("-q")))
00d6fd04
MA
370 (tramp-remote-sh "/bin/sh")
371 (tramp-copy-program "scp")
372 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
263c02ef 373 ("-q") ("-r")))
00d6fd04 374 (tramp-copy-keep-date t)
263c02ef 375 (tramp-copy-recursive t)
00d6fd04
MA
376 (tramp-password-end-of-line nil)
377 (tramp-gw-args (("-o"
378 "GlobalKnownHostsFile=/dev/null")
379 ("-o" "UserKnownHostsFile=/dev/null")
380 ("-o" "StrictHostKeyChecking=no")))
381 (tramp-default-port 22))
382 ("scp1_old"
383 (tramp-login-program "ssh1")
384 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
385 ("-e" "none")))
386 (tramp-remote-sh "/bin/sh")
387 (tramp-copy-program "scp1")
263c02ef 388 (tramp-copy-args (("-p" "%k") ("-r")))
00d6fd04 389 (tramp-copy-keep-date t)
263c02ef 390 (tramp-copy-recursive t)
00d6fd04
MA
391 (tramp-password-end-of-line nil))
392 ("scp2_old"
393 (tramp-login-program "ssh2")
394 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
395 ("-e" "none")))
396 (tramp-remote-sh "/bin/sh")
397 (tramp-copy-program "scp2")
263c02ef 398 (tramp-copy-args (("-p" "%k") ("-r")))
00d6fd04 399 (tramp-copy-keep-date t)
263c02ef 400 (tramp-copy-recursive t)
00d6fd04
MA
401 (tramp-password-end-of-line nil))
402 ("sftp" (tramp-login-program "ssh")
403 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
404 ("-e" "none")))
a7723e05 405 (tramp-async-args (("-q")))
00d6fd04
MA
406 (tramp-remote-sh "/bin/sh")
407 (tramp-copy-program "sftp")
408 (tramp-copy-args nil)
409 (tramp-copy-keep-date nil)
410 (tramp-password-end-of-line nil))
411 ("rsync" (tramp-login-program "ssh")
412 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
413 ("-e" "none")))
a7723e05 414 (tramp-async-args (("-q")))
00d6fd04
MA
415 (tramp-remote-sh "/bin/sh")
416 (tramp-copy-program "rsync")
263c02ef 417 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
00d6fd04 418 (tramp-copy-keep-date t)
b88f2d0a 419 (tramp-copy-keep-tmpfile t)
263c02ef 420 (tramp-copy-recursive t)
00d6fd04 421 (tramp-password-end-of-line nil))
263c02ef
MA
422 ("rsyncc"
423 (tramp-login-program "ssh")
946a5aeb
MA
424 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
425 ("-o" "ControlPath=%t.%%r@%%h:%%p")
426 ("-o" "ControlMaster=yes")
427 ("-e" "none")))
a7723e05 428 (tramp-async-args (("-q")))
946a5aeb
MA
429 (tramp-remote-sh "/bin/sh")
430 (tramp-copy-program "rsync")
263c02ef 431 (tramp-copy-args (("-t" "%k") ("-r")))
946a5aeb
MA
432 (tramp-copy-env (("RSYNC_RSH")
433 (,(concat
434 "ssh"
435 " -o ControlPath=%t.%%r@%%h:%%p"
436 " -o ControlMaster=auto"))))
437 (tramp-copy-keep-date t)
b88f2d0a 438 (tramp-copy-keep-tmpfile t)
263c02ef 439 (tramp-copy-recursive t)
946a5aeb 440 (tramp-password-end-of-line nil))
00d6fd04
MA
441 ("remcp" (tramp-login-program "remsh")
442 (tramp-login-args (("%h") ("-l" "%u")))
443 (tramp-remote-sh "/bin/sh")
444 (tramp-copy-program "rcp")
445 (tramp-copy-args (("-p" "%k")))
446 (tramp-copy-keep-date t)
447 (tramp-password-end-of-line nil))
448 ("rsh" (tramp-login-program "rsh")
449 (tramp-login-args (("%h") ("-l" "%u")))
450 (tramp-remote-sh "/bin/sh")
451 (tramp-copy-program nil)
452 (tramp-copy-args nil)
453 (tramp-copy-keep-date nil)
454 (tramp-password-end-of-line nil))
455 ("ssh" (tramp-login-program "ssh")
c04b03f8 456 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 457 ("-e" "none")))
a7723e05 458 (tramp-async-args (("-q")))
00d6fd04
MA
459 (tramp-remote-sh "/bin/sh")
460 (tramp-copy-program nil)
461 (tramp-copy-args nil)
462 (tramp-copy-keep-date nil)
463 (tramp-password-end-of-line nil)
464 (tramp-gw-args (("-o"
465 "GlobalKnownHostsFile=/dev/null")
466 ("-o" "UserKnownHostsFile=/dev/null")
467 ("-o" "StrictHostKeyChecking=no")))
468 (tramp-default-port 22))
469 ("ssh1" (tramp-login-program "ssh")
c04b03f8 470 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 471 ("-1" "-e" "none")))
a7723e05 472 (tramp-async-args (("-q")))
00d6fd04
MA
473 (tramp-remote-sh "/bin/sh")
474 (tramp-copy-program nil)
475 (tramp-copy-args nil)
476 (tramp-copy-keep-date nil)
477 (tramp-password-end-of-line nil)
478 (tramp-gw-args (("-o"
479 "GlobalKnownHostsFile=/dev/null")
480 ("-o" "UserKnownHostsFile=/dev/null")
481 ("-o" "StrictHostKeyChecking=no")))
482 (tramp-default-port 22))
483 ("ssh2" (tramp-login-program "ssh")
c04b03f8 484 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 485 ("-2" "-e" "none")))
a7723e05 486 (tramp-async-args (("-q")))
00d6fd04
MA
487 (tramp-remote-sh "/bin/sh")
488 (tramp-copy-program nil)
489 (tramp-copy-args nil)
490 (tramp-copy-keep-date nil)
491 (tramp-password-end-of-line nil)
492 (tramp-gw-args (("-o"
493 "GlobalKnownHostsFile=/dev/null")
494 ("-o" "UserKnownHostsFile=/dev/null")
495 ("-o" "StrictHostKeyChecking=no")))
496 (tramp-default-port 22))
497 ("ssh1_old"
498 (tramp-login-program "ssh1")
499 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
500 ("-e" "none")))
a7723e05 501 (tramp-async-args (("-q")))
00d6fd04
MA
502 (tramp-remote-sh "/bin/sh")
503 (tramp-copy-program nil)
504 (tramp-copy-args nil)
505 (tramp-copy-keep-date nil)
506 (tramp-password-end-of-line nil))
507 ("ssh2_old"
508 (tramp-login-program "ssh2")
509 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
510 ("-e" "none")))
511 (tramp-remote-sh "/bin/sh")
512 (tramp-copy-program nil)
513 (tramp-copy-args nil)
514 (tramp-copy-keep-date nil)
515 (tramp-password-end-of-line nil))
516 ("remsh" (tramp-login-program "remsh")
517 (tramp-login-args (("%h") ("-l" "%u")))
518 (tramp-remote-sh "/bin/sh")
519 (tramp-copy-program nil)
520 (tramp-copy-args nil)
521 (tramp-copy-keep-date nil)
522 (tramp-password-end-of-line nil))
523 ("telnet"
524 (tramp-login-program "telnet")
525 (tramp-login-args (("%h") ("%p")))
526 (tramp-remote-sh "/bin/sh")
527 (tramp-copy-program nil)
528 (tramp-copy-args nil)
529 (tramp-copy-keep-date nil)
530 (tramp-password-end-of-line nil)
531 (tramp-default-port 23))
532 ("su" (tramp-login-program "su")
533 (tramp-login-args (("-") ("%u")))
534 (tramp-remote-sh "/bin/sh")
535 (tramp-copy-program nil)
536 (tramp-copy-args nil)
537 (tramp-copy-keep-date nil)
538 (tramp-password-end-of-line nil))
539 ("sudo" (tramp-login-program "sudo")
540 (tramp-login-args (("-u" "%u")
42bc9b6d 541 ("-s") ("-H") ("-p" "Password:")))
00d6fd04
MA
542 (tramp-remote-sh "/bin/sh")
543 (tramp-copy-program nil)
544 (tramp-copy-args nil)
545 (tramp-copy-keep-date nil)
546 (tramp-password-end-of-line nil))
547 ("scpc" (tramp-login-program "ssh")
c04b03f8 548 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
549 ("-o" "ControlPath=%t.%%r@%%h:%%p")
550 ("-o" "ControlMaster=yes")
551 ("-e" "none")))
a7723e05 552 (tramp-async-args (("-q")))
00d6fd04
MA
553 (tramp-remote-sh "/bin/sh")
554 (tramp-copy-program "scp")
555 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
556 ("-o" "ControlPath=%t.%%r@%%h:%%p")
557 ("-o" "ControlMaster=auto")))
558 (tramp-copy-keep-date t)
559 (tramp-password-end-of-line nil)
560 (tramp-gw-args (("-o"
561 "GlobalKnownHostsFile=/dev/null")
562 ("-o" "UserKnownHostsFile=/dev/null")
563 ("-o" "StrictHostKeyChecking=no")))
564 (tramp-default-port 22))
565 ("scpx" (tramp-login-program "ssh")
c04b03f8 566 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 567 ("-e" "none" "-t" "-t" "/bin/sh")))
a7723e05 568 (tramp-async-args (("-q")))
00d6fd04
MA
569 (tramp-remote-sh "/bin/sh")
570 (tramp-copy-program "scp")
571 (tramp-copy-args (("-p" "%k")))
572 (tramp-copy-keep-date t)
573 (tramp-password-end-of-line nil)
574 (tramp-gw-args (("-o"
575 "GlobalKnownHostsFile=/dev/null")
576 ("-o" "UserKnownHostsFile=/dev/null")
577 ("-o" "StrictHostKeyChecking=no")))
578 (tramp-default-port 22))
579 ("sshx" (tramp-login-program "ssh")
c04b03f8 580 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04 581 ("-e" "none" "-t" "-t" "/bin/sh")))
a7723e05 582 (tramp-async-args (("-q")))
00d6fd04
MA
583 (tramp-remote-sh "/bin/sh")
584 (tramp-copy-program nil)
585 (tramp-copy-args nil)
586 (tramp-copy-keep-date nil)
587 (tramp-password-end-of-line nil)
588 (tramp-gw-args (("-o"
589 "GlobalKnownHostsFile=/dev/null")
590 ("-o" "UserKnownHostsFile=/dev/null")
591 ("-o" "StrictHostKeyChecking=no")))
592 (tramp-default-port 22))
593 ("krlogin"
594 (tramp-login-program "krlogin")
595 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
596 (tramp-remote-sh "/bin/sh")
597 (tramp-copy-program nil)
598 (tramp-copy-args nil)
599 (tramp-copy-keep-date nil)
600 (tramp-password-end-of-line nil))
601 ("plink" (tramp-login-program "plink")
602 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
603 ("-ssh")))
604 (tramp-remote-sh "/bin/sh")
605 (tramp-copy-program nil)
606 (tramp-copy-args nil)
607 (tramp-copy-keep-date nil)
608 (tramp-password-end-of-line "xy") ;see docstring for "xy"
609 (tramp-default-port 22))
610 ("plink1"
611 (tramp-login-program "plink")
612 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
613 ("-1" "-ssh")))
614 (tramp-remote-sh "/bin/sh")
615 (tramp-copy-program nil)
616 (tramp-copy-args nil)
617 (tramp-copy-keep-date nil)
618 (tramp-password-end-of-line "xy") ;see docstring for "xy"
619 (tramp-default-port 22))
620 ("plinkx"
621 (tramp-login-program "plink")
42bc9b6d
MA
622 ;; ("%h") must be a single element, see
623 ;; `tramp-compute-multi-hops'.
624 (tramp-login-args (("-load") ("%h") ("-t")
ce3f516f 625 (,(format
dab816a9
MA
626 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
627 tramp-terminal-type
628 tramp-initial-end-of-output))
00d6fd04
MA
629 ("/bin/sh")))
630 (tramp-remote-sh "/bin/sh")
631 (tramp-copy-program nil)
632 (tramp-copy-args nil)
633 (tramp-copy-keep-date nil)
634 (tramp-password-end-of-line nil))
635 ("pscp" (tramp-login-program "plink")
636 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
637 ("-ssh")))
638 (tramp-remote-sh "/bin/sh")
639 (tramp-copy-program "pscp")
60f2c210 640 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
00d6fd04
MA
641 (tramp-copy-keep-date t)
642 (tramp-password-end-of-line "xy") ;see docstring for "xy"
643 (tramp-default-port 22))
644 ("psftp" (tramp-login-program "plink")
645 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
646 ("-ssh")))
647 (tramp-remote-sh "/bin/sh")
648 (tramp-copy-program "pscp")
60f2c210 649 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
00d6fd04
MA
650 (tramp-copy-keep-date t)
651 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
652 ("fcp" (tramp-login-program "fsh")
653 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
654 (tramp-remote-sh "/bin/sh -i")
655 (tramp-copy-program "fcp")
656 (tramp-copy-args (("-p" "%k")))
657 (tramp-copy-keep-date t)
658 (tramp-password-end-of-line nil)))
fb7933a3
KG
659 "*Alist of methods for remote files.
660This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
661Each NAME stands for a remote access method. Each PARAM is a
662pair of the form (KEY VALUE). The following KEYs are defined:
fb7933a3
KG
663 * `tramp-remote-sh'
664 This specifies the Bourne shell to use on the remote host. This
665 MUST be a Bourne-like shell. It is normally not necessary to set
a4aeb9a4 666 this to any value other than \"/bin/sh\": Tramp wants to use a shell
fb7933a3
KG
667 which groks tilde expansion, but it can search for it. Also note
668 that \"/bin/sh\" exists on all Unixen, this might not be true for
669 the value that you decide to use. You Have Been Warned.
b25a52cc
KG
670 * `tramp-login-program'
671 This specifies the name of the program to use for logging in to the
00d6fd04
MA
672 remote host. This may be the name of rsh or a workalike program,
673 or the name of telnet or a workalike, or the name of su or a workalike.
b25a52cc 674 * `tramp-login-args'
fb7933a3 675 This specifies the list of arguments to pass to the above
00d6fd04 676 mentioned program. Please note that this is a list of list of arguments,
fb7933a3 677 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
00d6fd04
MA
678 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
679 There are some patterns: \"%h\" in this list is replaced by the host
680 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
681 port number, and \"%%\" can be used to obtain a literal percent character.
682 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
683 expansion (i.e. no host or no user specified), this list is not used as
684 argument. By this, arguments like (\"-l\" \"%u\") are optional.
685 \"%t\" is replaced by the temporary file name produced with
686 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
687 parameter of a program, if exists.
a7723e05
MA
688 * `tramp-async-args'
689 When an asynchronous process is started, we know already that
690 the connection works. Therefore, we can pass additional
691 parameters to suppress diagnostic messages, in order not to
692 tamper the process output.
b25a52cc
KG
693 * `tramp-copy-program'
694 This specifies the name of the program to use for remotely copying
695 the file; this might be the absolute filename of rcp or the name of
696 a workalike program.
697 * `tramp-copy-args'
fb7933a3 698 This specifies the list of parameters to pass to the above mentioned
b25a52cc 699 program, the hints for `tramp-login-args' also apply here.
00d6fd04
MA
700 * `tramp-copy-keep-date'
701 This specifies whether the copying program when the preserves the
702 timestamp of the original file.
b88f2d0a
MA
703 * `tramp-copy-keep-tmpfile'
704 This specifies whether a temporary local file shall be kept
705 for optimization reasons (useful for \"rsync\" methods).
706 * `tramp-copy-recursive'
707 Whether the operation copies directories recursively.
00d6fd04
MA
708 * `tramp-default-port'
709 The default port of a method is needed in case of gateway connections.
710 Additionally, it is used as indication which method is prepared for
711 passing gateways.
712 * `tramp-gw-args'
713 As the attribute name says, additional arguments are specified here
714 when a method is applied via a gateway.
90f8dc03
KG
715 * `tramp-password-end-of-line'
716 This specifies the string to use for terminating the line after
717 submitting the password. If this method parameter is nil, then the
718 value of the normal variable `tramp-default-password-end-of-line'
719 is used. This parameter is necessary because the \"plink\" program
720 requires any two characters after sending the password. These do
721 not have to be newline or carriage return characters. Other login
722 programs are happy with just one character, the newline character.
723 We use \"xy\" as the value for methods using \"plink\".
b25a52cc
KG
724
725What does all this mean? Well, you should specify `tramp-login-program'
726for all methods; this program is used to log in to the remote site. Then,
727there are two ways to actually transfer the files between the local and the
728remote side. One way is using an additional rcp-like program. If you want
729to do this, set `tramp-copy-program' in the method.
fb7933a3
KG
730
731Another possibility for file transfer is inline transfer, i.e. the
b25a52cc 732file is passed through the same buffer used by `tramp-login-program'. In
fb7933a3 733this case, the file contents need to be protected since the
b25a52cc 734`tramp-login-program' might use escape codes or the connection might not
fb7933a3 735be eight-bit clean. Therefore, file contents are encoded for transit.
00d6fd04
MA
736See the variables `tramp-local-coding-commands' and
737`tramp-remote-coding-commands' for details.
fb7933a3 738
16674e4f 739So, to summarize: if the method is an out-of-band method, then you
b25a52cc 740must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
00d6fd04
MA
741inline method, then these two parameters should be nil. Methods which
742are fit for gateways must have `tramp-default-port' at least.
fb7933a3
KG
743
744Notes:
745
00d6fd04
MA
746When using `su' or `sudo' the phrase `open connection to a remote
747host' sounds strange, but it is used nevertheless, for consistency.
748No connection is opened to a remote host, but `su' or `sudo' is
749started on the local host. You should specify a remote host
750`localhost' or the name of the local host. Another host name is
751useful only in combination with `tramp-default-proxies-alist'.")
fb7933a3 752
a92375d9
MA
753(defun tramp-detect-ssh-controlmaster ()
754 "Call ssh to detect whether it supports the ControlMaster argument.
755This function may return nil when the argument is supported, but
756shouldn't return t when it isn't."
757 (ignore-errors
758 (with-temp-buffer
759 (call-process "ssh" nil t nil "-o" "ControlMaster")
760 (goto-char (point-min))
761 (search-forward-regexp "Missing ControlMaster argument" nil t))))
762
b25a52cc 763(defcustom tramp-default-method
83e20b5c
MA
764 ;; An external copy method seems to be preferred, because it is much
765 ;; more performant for large files, and it hasn't too serious delays
766 ;; for small files. But it must be ensured that there aren't
767 ;; permanent password queries. Either a password agent like
263c02ef
MA
768 ;; "ssh-agent" or "Pageant" shall run, or the optional
769 ;; password-cache.el or auth-sources.el packages shall be active for
a92375d9
MA
770 ;; password caching. "scpc" is chosen if we detect that the user is
771 ;; running OpenSSH 4.0 or newer.
00d6fd04
MA
772 (cond
773 ;; PuTTY is installed.
774 ((executable-find "pscp")
775 (if (or (fboundp 'password-read)
263c02ef 776 (fboundp 'auth-source-user-or-password)
00d6fd04 777 ;; Pageant is running.
70c11b0b 778 (tramp-compat-process-running-p "Pageant"))
00d6fd04
MA
779 "pscp"
780 "plink"))
781 ;; There is an ssh installation.
782 ((executable-find "scp")
a92375d9
MA
783 (cond
784 ((tramp-detect-ssh-controlmaster) "scpc")
785 ((or (fboundp 'password-read)
786 (fboundp 'auth-source-user-or-password)
787 ;; ssh-agent is running.
788 (getenv "SSH_AUTH_SOCK")
789 (getenv "SSH_AGENT_PID"))
790 "scp")
791 (t "ssh")))
00d6fd04
MA
792 ;; Fallback.
793 (t "ftp"))
fb7933a3 794 "*Default method to use for transferring files.
c62c9d08 795See `tramp-methods' for possibilities.
4007ba5b 796Also see `tramp-default-method-alist'."
c62c9d08
KG
797 :group 'tramp
798 :type 'string)
799
505edaeb 800(defcustom tramp-default-method-alist
4007ba5b 801 '(("\\`localhost\\'" "\\`root\\'" "su"))
00d6fd04 802 "*Default method to use for specific host/user pairs.
c62c9d08
KG
803This is an alist of items (HOST USER METHOD). The first matching item
804specifies the method to use for a file name which does not specify a
805method. HOST and USER are regular expressions or nil, which is
806interpreted as a regular expression which always matches. If no entry
807matches, the variable `tramp-default-method' takes effect.
808
809If the file name does not specify the user, lookup is done using the
810empty string for the user name.
811
812See `tramp-methods' for a list of possibilities for METHOD."
813 :group 'tramp
814 :type '(repeat (list (regexp :tag "Host regexp")
815 (regexp :tag "User regexp")
816 (string :tag "Method"))))
817
00d6fd04
MA
818(defcustom tramp-default-user
819 nil
820 "*Default user to use for transferring files.
821It is nil by default; otherwise settings in configuration files like
822\"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
823
824This variable is regarded as obsolete, and will be removed soon."
825 :group 'tramp
826 :type '(choice (const nil) string))
827
828(defcustom tramp-default-user-alist
829 `(("\\`su\\(do\\)?\\'" nil "root")
830 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
831 nil ,(user-login-name)))
832 "*Default user to use for specific method/host pairs.
833This is an alist of items (METHOD HOST USER). The first matching item
834specifies the user to use for a file name which does not specify a
835user. METHOD and USER are regular expressions or nil, which is
836interpreted as a regular expression which always matches. If no entry
837matches, the variable `tramp-default-user' takes effect.
838
839If the file name does not specify the method, lookup is done using the
840empty string for the method name."
841 :group 'tramp
842 :type '(repeat (list (regexp :tag "Method regexp")
843 (regexp :tag "Host regexp")
844 (string :tag "User"))))
845
846(defcustom tramp-default-host
847 (system-name)
848 "*Default host to use for transferring files.
849Useful for su and sudo methods mostly."
850 :group 'tramp
851 :type 'string)
852
853(defcustom tramp-default-proxies-alist nil
854 "*Route to be followed for specific host/user pairs.
855This is an alist of items (HOST USER PROXY). The first matching
856item specifies the proxy to be passed for a file name located on
857a remote target matching USER@HOST. HOST and USER are regular
70c11b0b
MA
858expressions. PROXY must be a Tramp filename without a localname
859part. Method and user name on PROXY are optional, which is
860interpreted with the default values. PROXY can contain the
861patterns %h and %u, which are replaced by the strings matching
862HOST or USER, respectively.
863
864HOST, USER or PROXY could also be Lisp forms, which will be
865evaluated. The result must be a string or nil, which is
866interpreted as a regular expression which always matches."
00d6fd04 867 :group 'tramp
70c11b0b
MA
868 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
869 (choice :tag "User regexp" regexp sexp)
870 (choice :tag "Proxy remote name" string (const nil)))))
00d6fd04 871
b96e6899
MA
872(defconst tramp-local-host-regexp
873 (concat
874 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$")
875 "*Host names which are regarded as local host.")
876
16674e4f 877(defconst tramp-completion-function-alist-rsh
00d6fd04
MA
878 '((tramp-parse-rhosts "/etc/hosts.equiv")
879 (tramp-parse-rhosts "~/.rhosts"))
b25a52cc 880 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
16674e4f 881
16674e4f 882(defconst tramp-completion-function-alist-ssh
00d6fd04
MA
883 '((tramp-parse-rhosts "/etc/hosts.equiv")
884 (tramp-parse-rhosts "/etc/shosts.equiv")
885 (tramp-parse-shosts "/etc/ssh_known_hosts")
886 (tramp-parse-sconfig "/etc/ssh_config")
887 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
888 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
889 (tramp-parse-rhosts "~/.rhosts")
890 (tramp-parse-rhosts "~/.shosts")
891 (tramp-parse-shosts "~/.ssh/known_hosts")
892 (tramp-parse-sconfig "~/.ssh/config")
893 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
894 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
b25a52cc 895 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
16674e4f 896
16674e4f 897(defconst tramp-completion-function-alist-telnet
00d6fd04 898 '((tramp-parse-hosts "/etc/hosts"))
b25a52cc 899 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
16674e4f 900
16674e4f 901(defconst tramp-completion-function-alist-su
00d6fd04 902 '((tramp-parse-passwd "/etc/passwd"))
b25a52cc 903 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
292ffc15 904
00d6fd04
MA
905(defconst tramp-completion-function-alist-putty
906 '((tramp-parse-putty
907 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
908 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
909
5ec2cc41 910(defvar tramp-completion-function-alist nil
16674e4f 911 "*Alist of methods for remote files.
b533bc97 912This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
16674e4f 913Each NAME stands for a remote access method. Each PAIR is of the form
b533bc97 914\(FUNCTION FILE\). FUNCTION is responsible to extract user names and host
16674e4f
KG
915names from FILE for completion. The following predefined FUNCTIONs exists:
916
5ec2cc41
KG
917 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
918 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
919 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
920 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
921 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
922 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
923 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
924 * `tramp-parse-netrc' for \"~/.netrc\" like files.
00d6fd04 925 * `tramp-parse-putty' for PuTTY registry keys.
5ec2cc41
KG
926
927FUNCTION can also be a customer defined function. For more details see
928the info pages.")
929
930(eval-after-load "tramp"
931 '(progn
932 (tramp-set-completion-function
933 "rcp" tramp-completion-function-alist-rsh)
934 (tramp-set-completion-function
935 "scp" tramp-completion-function-alist-ssh)
936 (tramp-set-completion-function
937 "scp1" tramp-completion-function-alist-ssh)
938 (tramp-set-completion-function
939 "scp2" tramp-completion-function-alist-ssh)
940 (tramp-set-completion-function
941 "scp1_old" tramp-completion-function-alist-ssh)
942 (tramp-set-completion-function
943 "scp2_old" tramp-completion-function-alist-ssh)
944 (tramp-set-completion-function
70c11b0b 945 "rsync" tramp-completion-function-alist-ssh)
946a5aeb
MA
946 (tramp-set-completion-function
947 "rsyncc" tramp-completion-function-alist-ssh)
5ec2cc41
KG
948 (tramp-set-completion-function
949 "remcp" tramp-completion-function-alist-rsh)
950 (tramp-set-completion-function
951 "rsh" tramp-completion-function-alist-rsh)
952 (tramp-set-completion-function
953 "ssh" tramp-completion-function-alist-ssh)
954 (tramp-set-completion-function
955 "ssh1" tramp-completion-function-alist-ssh)
956 (tramp-set-completion-function
957 "ssh2" tramp-completion-function-alist-ssh)
958 (tramp-set-completion-function
959 "ssh1_old" tramp-completion-function-alist-ssh)
960 (tramp-set-completion-function
961 "ssh2_old" tramp-completion-function-alist-ssh)
962 (tramp-set-completion-function
963 "remsh" tramp-completion-function-alist-rsh)
964 (tramp-set-completion-function
965 "telnet" tramp-completion-function-alist-telnet)
966 (tramp-set-completion-function
967 "su" tramp-completion-function-alist-su)
968 (tramp-set-completion-function
969 "sudo" tramp-completion-function-alist-su)
bf247b6e 970 (tramp-set-completion-function
5ec2cc41
KG
971 "scpx" tramp-completion-function-alist-ssh)
972 (tramp-set-completion-function
973 "sshx" tramp-completion-function-alist-ssh)
974 (tramp-set-completion-function
975 "krlogin" tramp-completion-function-alist-rsh)
976 (tramp-set-completion-function
977 "plink" tramp-completion-function-alist-ssh)
978 (tramp-set-completion-function
979 "plink1" tramp-completion-function-alist-ssh)
00d6fd04
MA
980 (tramp-set-completion-function
981 "plinkx" tramp-completion-function-alist-putty)
5ec2cc41
KG
982 (tramp-set-completion-function
983 "pscp" tramp-completion-function-alist-ssh)
984 (tramp-set-completion-function
985 "fcp" tramp-completion-function-alist-ssh)))
16674e4f 986
674da028
MA
987(defconst tramp-echo-mark-marker "_echo"
988 "String marker to surround echoed commands.")
989
68712eb6
MA
990(defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
991 "String length of `tramp-echo-mark-marker'.")
992
993(defconst tramp-echo-mark
994 (concat tramp-echo-mark-marker
995 (make-string tramp-echo-mark-marker-length ?\b))
00d6fd04
MA
996 "String mark to be transmitted around shell commands.
997Used to separate their echo from the output they produce. This
998will only be used if we cannot disable remote echo via stty.
999This string must have no effect on the remote shell except for
1000producing some echo which can later be detected by
674da028
MA
1001`tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
1002followed by an equal number of backspaces to erase them will
1003usually suffice.")
00d6fd04 1004
68712eb6
MA
1005(defconst tramp-echoed-echo-mark-regexp
1006 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
1007 tramp-echo-mark-marker tramp-echo-mark-marker-length)
00d6fd04
MA
1008 "Regexp which matches `tramp-echo-mark' as it gets echoed by
1009the remote shell.")
1010
fb7933a3
KG
1011(defcustom tramp-rsh-end-of-line "\n"
1012 "*String used for end of line in rsh connections.
1013I don't think this ever needs to be changed, so please tell me about it
16674e4f 1014if you need to change this.
90f8dc03
KG
1015Also see the method parameter `tramp-password-end-of-line' and the normal
1016variable `tramp-default-password-end-of-line'."
16674e4f
KG
1017 :group 'tramp
1018 :type 'string)
1019
90f8dc03
KG
1020(defcustom tramp-default-password-end-of-line
1021 tramp-rsh-end-of-line
16674e4f 1022 "*String used for end of line after sending a password.
90f8dc03
KG
1023This variable provides the default value for the method parameter
1024`tramp-password-end-of-line', see `tramp-methods' for more details.
1025
16674e4f
KG
1026It seems that people using plink under Windows need to send
1027\"\\r\\n\" (carriage-return, then newline) after a password, but just
1028\"\\n\" after all other lines. This variable can be used for the
1029password, see `tramp-rsh-end-of-line' for the other cases.
1030
1031The default value is to use the same value as `tramp-rsh-end-of-line'."
fb7933a3
KG
1032 :group 'tramp
1033 :type 'string)
1034
00d6fd04
MA
1035;; "getconf PATH" yields:
1036;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
1037;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
0664ff72 1038;; GNU/Linux (Debian, Suse): /bin:/usr/bin
00d6fd04 1039;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
fb7933a3 1040(defcustom tramp-remote-path
00d6fd04
MA
1041 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
1042 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
fb7933a3
KG
1043 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
1044 "*List of directories to search for executables on remote host.
00d6fd04
MA
1045For every remote host, this variable will be set buffer local,
1046keeping the list of existing directories on that host.
fb7933a3
KG
1047
1048You can use `~' in this list, but when searching for a shell which groks
00d6fd04
MA
1049tilde expansion, all directory names starting with `~' will be ignored.
1050
1051`Default Directories' represent the list of directories given by
1052the command \"getconf PATH\". It is recommended to use this
1053entry on top of this list, because these are the default
70c11b0b
MA
1054directories for POSIX compatible commands.
1055
1056`Private Directories' are the settings of the $PATH environment,
1057as given in your `~/.profile'."
00d6fd04
MA
1058 :group 'tramp
1059 :type '(repeat (choice
1060 (const :tag "Default Directories" tramp-default-remote-path)
70c11b0b 1061 (const :tag "Private Directories" tramp-own-remote-path)
00d6fd04
MA
1062 (string :tag "Directory"))))
1063
00d6fd04 1064(defcustom tramp-remote-process-environment
a0a5183a 1065 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
b533bc97 1066 ,(format "TERM=%s" tramp-terminal-type)
97c696d5
MA
1067 "EMACS=t" ;; Deprecated.
1068 ,(format "INSIDE_EMACS=%s,tramp:%s" emacs-version tramp-version)
00d6fd04
MA
1069 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
1070 "autocorrect=" "correct=")
1071
1072 "*List of environment variables to be set on the remote host.
1073
1074Each element should be a string of the form ENVVARNAME=VALUE. An
1075entry ENVVARNAME= diables the corresponding environment variable,
1076which might have been set in the init files like ~/.profile.
1077
1078Special handling is applied to the PATH environment, which should
1079not be set here. Instead of, it should be set via `tramp-remote-path'."
fb7933a3
KG
1080 :group 'tramp
1081 :type '(repeat string))
1082
1083(defcustom tramp-login-prompt-regexp
bc103d00 1084 ".*ogin\\( .*\\)?: *"
fb7933a3 1085 "*Regexp matching login-like prompts.
bc103d00
MA
1086The regexp should match at end of buffer.
1087
1088Sometimes the prompt is reported to look like \"login as:\"."
fb7933a3
KG
1089 :group 'tramp
1090 :type 'regexp)
1091
821e6e36 1092(defcustom tramp-shell-prompt-pattern
aa485f7c
MA
1093 ;; Allow a prompt to start right after a ^M since it indeed would be
1094 ;; displayed at the beginning of the line (and Zsh uses it).
dab816a9 1095 "\\(?:^\\|\r\\)[^#$%>\n]*#?[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
821e6e36
KG
1096 "Regexp to match prompts from remote shell.
1097Normally, Tramp expects you to configure `shell-prompt-pattern'
1098correctly, but sometimes it happens that you are connecting to a
1099remote host which sends a different kind of shell prompt. Therefore,
1100Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
1101and also things matched by this variable. The default value of this
b25a52cc 1102variable is similar to the default value of `shell-prompt-pattern',
dab816a9
MA
1103which should work well in many cases.
1104
1105This regexp must match both `tramp-initial-end-of-output' and
1106`tramp-end-of-output'."
821e6e36
KG
1107 :group 'tramp
1108 :type 'regexp)
1109
fb7933a3 1110(defcustom tramp-password-prompt-regexp
00d6fd04 1111 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
fb7933a3 1112 "*Regexp matching password-like prompts.
ac474af1 1113The regexp should match at end of buffer.
fb7933a3
KG
1114
1115The `sudo' program appears to insert a `^@' character into the prompt."
1116 :group 'tramp
1117 :type 'regexp)
1118
1119(defcustom tramp-wrong-passwd-regexp
b1d06e75
KG
1120 (concat "^.*"
1121 ;; These strings should be on the last line
a4aeb9a4 1122 (regexp-opt '("Permission denied"
b1d06e75
KG
1123 "Login incorrect"
1124 "Login Incorrect"
1125 "Connection refused"
27e813fe 1126 "Connection closed"
7e5686f0 1127 "Timeout, server not responding."
b1d06e75
KG
1128 "Sorry, try again."
1129 "Name or service not known"
00d6fd04 1130 "Host key verification failed."
70c11b0b 1131 "No supported authentication methods left to try!") t)
b1d06e75
KG
1132 ".*"
1133 "\\|"
1134 "^.*\\("
1135 ;; Here comes a list of regexes, separated by \\|
1136 "Received signal [0-9]+"
1137 "\\).*")
fb7933a3 1138 "*Regexp matching a `login failed' message.
ac474af1
KG
1139The regexp should match at end of buffer."
1140 :group 'tramp
1141 :type 'regexp)
1142
1143(defcustom tramp-yesno-prompt-regexp
3cdaec13
KG
1144 (concat
1145 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
1146 "\\s-*")
1147 "Regular expression matching all yes/no queries which need to be confirmed.
ac474af1 1148The confirmation should be done with yes or no.
3cdaec13
KG
1149The regexp should match at end of buffer.
1150See also `tramp-yn-prompt-regexp'."
fb7933a3
KG
1151 :group 'tramp
1152 :type 'regexp)
1153
3cdaec13 1154(defcustom tramp-yn-prompt-regexp
658052a2
MA
1155 (concat
1156 (regexp-opt '("Store key in cache? (y/n)"
1157 "Update cached key? (y/n, Return cancels connection)") t)
1158 "\\s-*")
3cdaec13
KG
1159 "Regular expression matching all y/n queries which need to be confirmed.
1160The confirmation should be done with y or n.
1161The regexp should match at end of buffer.
1162See also `tramp-yesno-prompt-regexp'."
1163 :group 'tramp
1164 :type 'regexp)
487f4fb7
KG
1165
1166(defcustom tramp-terminal-prompt-regexp
1167 (concat "\\("
1168 "TERM = (.*)"
1169 "\\|"
1170 "Terminal type\\? \\[.*\\]"
1171 "\\)\\s-*")
1172 "Regular expression matching all terminal setting prompts.
1173The regexp should match at end of buffer.
1174The answer will be provided by `tramp-action-terminal', which see."
1175 :group 'tramp
1176 :type 'regexp)
3cdaec13 1177
01917a18
MA
1178(defcustom tramp-operation-not-permitted-regexp
1179 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1180 (regexp-opt '("Operation not permitted") t))
1181 "Regular expression matching keep-date problems in (s)cp operations.
1182Copying has been performed successfully already, so this message can
1183be ignored safely."
1184 :group 'tramp
1185 :type 'regexp)
1186
6b2633cc
LH
1187(defcustom tramp-copy-failed-regexp
1188 (concat "\\(.+: "
1189 (regexp-opt '("Permission denied"
1190 "not a regular file"
1191 "is a directory"
1192 "No such file or directory") t)
1193 "\\)\\s-*")
1194 "Regular expression matching copy problems in (s)cp operations."
1195 :group 'tramp
1196 :type 'regexp)
1197
19a87064 1198(defcustom tramp-process-alive-regexp
38c65fca 1199 ""
19a87064 1200 "Regular expression indicating a process has finished.
38c65fca
KG
1201In fact this expression is empty by intention, it will be used only to
1202check regularly the status of the associated process.
07dfe738 1203The answer will be provided by `tramp-action-process-alive',
00d6fd04 1204`tramp-action-out-of-band', which see."
38c65fca
KG
1205 :group 'tramp
1206 :type 'regexp)
1207
fb7933a3
KG
1208(defcustom tramp-temp-name-prefix "tramp."
1209 "*Prefix to use for temporary files.
1210If this is a relative file name (such as \"tramp.\"), it is considered
1211relative to the directory name returned by the function
9e6ab520 1212`tramp-compat-temporary-file-directory' (which see). It may also be an
fb7933a3
KG
1213absolute file name; don't forget to include a prefix for the filename
1214part, though."
1215 :group 'tramp
1216 :type 'string)
1217
2296b54d
MA
1218(defconst tramp-temp-buffer-name " *tramp temp*"
1219 "Buffer name for a temporary buffer.
1220It shall be used in combination with `generate-new-buffer-name'.")
1221
b88f2d0a
MA
1222(defvar tramp-temp-buffer-file-name nil
1223 "File name of a persistent local temporary file.
1224Useful for \"rsync\" like methods.")
1225(make-variable-buffer-local 'tramp-temp-buffer-file-name)
1226
4007ba5b 1227(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
c62c9d08
KG
1228 "*Alist specifying extra arguments to pass to the remote shell.
1229Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1230matching the shell file name and ARGS is a string specifying the
1231arguments.
1232
1233This variable is only used when Tramp needs to start up another shell
1234for tilde expansion. The extra arguments should typically prevent the
1235shell from reading its init file."
1236 :group 'tramp
90f8dc03
KG
1237 ;; This might be the wrong way to test whether the widget type
1238 ;; `alist' is available. Who knows the right way to test it?
1239 :type (if (get 'alist 'widget-type)
1240 '(alist :key-type string :value-type string)
1241 '(repeat (cons string string))))
c62c9d08 1242
00d6fd04
MA
1243;; XEmacs is distributed with few Lisp packages. Further packages are
1244;; installed using EFS. If we use a unified filename format, then
1245;; Tramp is required in addition to EFS. (But why can't Tramp just
1246;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1247;; just like before.) Another reason for using a separate filename
1248;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1249;; Tramp only knows how to deal with `file-name-handler-alist', not
1250;; the other places.
1251
1252;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1253;;;###autoload
1254(defcustom tramp-syntax
1255 (if (featurep 'xemacs) 'sep 'ftp)
1256 "Tramp filename syntax to be used.
1257
1258It can have the following values:
1259
1260 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1261 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1262 'url -- URL-like syntax."
16674e4f 1263 :group 'tramp
00d6fd04
MA
1264 :type (if (featurep 'xemacs)
1265 '(choice (const :tag "EFS" ftp)
1266 (const :tag "XEmacs" sep)
1267 (const :tag "URL" url))
1268 '(choice (const :tag "Ange-FTP" ftp)
1269 (const :tag "URL" url))))
1270
1271(defconst tramp-prefix-format
1272 (cond ((equal tramp-syntax 'ftp) "/")
1273 ((equal tramp-syntax 'sep) "/[")
1274 ((equal tramp-syntax 'url) "/")
1275 (t (error "Wrong `tramp-syntax' defined")))
a4aeb9a4 1276 "*String matching the very beginning of Tramp file names.
00d6fd04 1277Used in `tramp-make-tramp-file-name'.")
16674e4f 1278
00d6fd04 1279(defconst tramp-prefix-regexp
16674e4f 1280 (concat "^" (regexp-quote tramp-prefix-format))
a4aeb9a4 1281 "*Regexp matching the very beginning of Tramp file names.
00d6fd04 1282Should always start with \"^\". Derived from `tramp-prefix-format'.")
16674e4f 1283
00d6fd04 1284(defconst tramp-method-regexp
16674e4f 1285 "[a-zA-Z_0-9-]+"
00d6fd04 1286 "*Regexp matching methods identifiers.")
16674e4f 1287
00d6fd04
MA
1288(defconst tramp-postfix-method-format
1289 (cond ((equal tramp-syntax 'ftp) ":")
1290 ((equal tramp-syntax 'sep) "/")
1291 ((equal tramp-syntax 'url) "://")
1292 (t (error "Wrong `tramp-syntax' defined")))
16674e4f 1293 "*String matching delimeter between method and user or host names.
00d6fd04 1294Used in `tramp-make-tramp-file-name'.")
16674e4f 1295
00d6fd04
MA
1296(defconst tramp-postfix-method-regexp
1297 (regexp-quote tramp-postfix-method-format)
16674e4f 1298 "*Regexp matching delimeter between method and user or host names.
00d6fd04 1299Derived from `tramp-postfix-method-format'.")
16674e4f 1300
00d6fd04
MA
1301(defconst tramp-user-regexp
1302 "[^:/ \t]+"
1303 "*Regexp matching user names.")
16674e4f 1304
b96e6899
MA
1305(defconst tramp-prefix-domain-format "%"
1306 "*String matching delimeter between user and domain names.")
1307
1308(defconst tramp-prefix-domain-regexp
1309 (regexp-quote tramp-prefix-domain-format)
1310 "*Regexp matching delimeter between user and domain names.
1311Derived from `tramp-prefix-domain-format'.")
1312
1313(defconst tramp-domain-regexp
70c11b0b 1314 "[-a-zA-Z0-9_.]+"
b96e6899
MA
1315 "*Regexp matching domain names.")
1316
1317(defconst tramp-user-with-domain-regexp
1318 (concat "\\(" tramp-user-regexp "\\)"
1319 tramp-prefix-domain-regexp
1320 "\\(" tramp-domain-regexp "\\)")
1321 "*Regexp matching user names with domain names.")
1322
00d6fd04 1323(defconst tramp-postfix-user-format
16674e4f
KG
1324 "@"
1325 "*String matching delimeter between user and host names.
00d6fd04 1326Used in `tramp-make-tramp-file-name'.")
16674e4f 1327
00d6fd04 1328(defconst tramp-postfix-user-regexp
16674e4f
KG
1329 (regexp-quote tramp-postfix-user-format)
1330 "*Regexp matching delimeter between user and host names.
00d6fd04
MA
1331Derived from `tramp-postfix-user-format'.")
1332
1333(defconst tramp-host-regexp
1334 "[a-zA-Z0-9_.-]+"
1335 "*Regexp matching host names.")
1336
b96e6899
MA
1337(defconst tramp-prefix-ipv6-format
1338 (cond ((equal tramp-syntax 'ftp) "[")
1339 ((equal tramp-syntax 'sep) "")
1340 ((equal tramp-syntax 'url) "[")
1341 (t (error "Wrong `tramp-syntax' defined")))
1342 "*String matching left hand side of IPv6 addresses.
1343Used in `tramp-make-tramp-file-name'.")
1344
1345(defconst tramp-prefix-ipv6-regexp
1346 (regexp-quote tramp-prefix-ipv6-format)
1347 "*Regexp matching left hand side of IPv6 addresses.
1348Derived from `tramp-prefix-ipv6-format'.")
1349
e0b6e3b9
MA
1350;; The following regexp is a bit sloppy. But it shall serve our
1351;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1352;; "::ffff:192.168.0.1".
b96e6899 1353(defconst tramp-ipv6-regexp
e0b6e3b9 1354 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
b96e6899
MA
1355 "*Regexp matching IPv6 addresses.")
1356
1357(defconst tramp-postfix-ipv6-format
1358 (cond ((equal tramp-syntax 'ftp) "]")
1359 ((equal tramp-syntax 'sep) "")
1360 ((equal tramp-syntax 'url) "]")
1361 (t (error "Wrong `tramp-syntax' defined")))
1362 "*String matching right hand side of IPv6 addresses.
1363Used in `tramp-make-tramp-file-name'.")
1364
1365(defconst tramp-postfix-ipv6-regexp
1366 (regexp-quote tramp-postfix-ipv6-format)
1367 "*Regexp matching right hand side of IPv6 addresses.
1368Derived from `tramp-postfix-ipv6-format'.")
1369
00d6fd04
MA
1370(defconst tramp-prefix-port-format
1371 (cond ((equal tramp-syntax 'ftp) "#")
1372 ((equal tramp-syntax 'sep) "#")
1373 ((equal tramp-syntax 'url) ":")
1374 (t (error "Wrong `tramp-syntax' defined")))
1375 "*String matching delimeter between host names and port numbers.")
1376
1377(defconst tramp-prefix-port-regexp
1378 (regexp-quote tramp-prefix-port-format)
1379 "*Regexp matching delimeter between host names and port numbers.
1380Derived from `tramp-prefix-port-format'.")
1381
1382(defconst tramp-port-regexp
1383 "[0-9]+"
1384 "*Regexp matching port numbers.")
1385
1386(defconst tramp-host-with-port-regexp
1387 (concat "\\(" tramp-host-regexp "\\)"
1388 tramp-prefix-port-regexp
1389 "\\(" tramp-port-regexp "\\)")
1390 "*Regexp matching host names with port numbers.")
1391
1392(defconst tramp-postfix-host-format
1393 (cond ((equal tramp-syntax 'ftp) ":")
1394 ((equal tramp-syntax 'sep) "]")
1395 ((equal tramp-syntax 'url) "")
1396 (t (error "Wrong `tramp-syntax' defined")))
7432277c 1397 "*String matching delimeter between host names and localnames.
00d6fd04 1398Used in `tramp-make-tramp-file-name'.")
16674e4f 1399
00d6fd04 1400(defconst tramp-postfix-host-regexp
16674e4f 1401 (regexp-quote tramp-postfix-host-format)
7432277c 1402 "*Regexp matching delimeter between host names and localnames.
00d6fd04 1403Derived from `tramp-postfix-host-format'.")
16674e4f 1404
00d6fd04 1405(defconst tramp-localname-regexp
16674e4f 1406 ".*$"
00d6fd04 1407 "*Regexp matching localnames.")
16674e4f
KG
1408
1409;; File name format.
505edaeb 1410
00d6fd04 1411(defconst tramp-file-name-structure
16674e4f
KG
1412 (list
1413 (concat
1414 tramp-prefix-regexp
00d6fd04
MA
1415 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
1416 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
b96e6899
MA
1417 "\\(" "\\(" tramp-host-regexp
1418 "\\|"
1419 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1420 tramp-postfix-ipv6-regexp "\\)"
1421 "\\(" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"
00d6fd04
MA
1422 tramp-postfix-host-regexp
1423 "\\(" tramp-localname-regexp "\\)")
b96e6899 1424 2 4 5 8)
16674e4f 1425
fb7933a3 1426 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
a4aeb9a4 1427the Tramp file name structure.
fb7933a3 1428
a4aeb9a4 1429The first element REGEXP is a regular expression matching a Tramp file
fb7933a3
KG
1430name. The regex should contain parentheses around the method name,
1431the user name, the host name, and the file name parts.
1432
1433The second element METHOD is a number, saying which pair of
1434parentheses matches the method name. The third element USER is
1435similar, but for the user name. The fourth element HOST is similar,
1436but for the host name. The fifth element FILE is for the file name.
1437These numbers are passed directly to `match-string', which see. That
1438means the opening parentheses are counted to identify the pair.
1439
00d6fd04 1440See also `tramp-file-name-regexp'.")
fb7933a3
KG
1441
1442;;;###autoload
505edaeb 1443(defconst tramp-file-name-regexp-unified
b96e6899 1444 "\\`/\\([^[/:]+\\|[^/]+]\\):"
505edaeb
KG
1445 "Value for `tramp-file-name-regexp' for unified remoting.
1446Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
00d6fd04 1447Tramp. See `tramp-file-name-structure' for more explanations.")
505edaeb
KG
1448
1449;;;###autoload
1450(defconst tramp-file-name-regexp-separate
1451 "\\`/\\[.*\\]"
1452 "Value for `tramp-file-name-regexp' for separate remoting.
1453XEmacs uses a separate filename syntax for Tramp and EFS.
00d6fd04 1454See `tramp-file-name-structure' for more explanations.")
505edaeb
KG
1455
1456;;;###autoload
00d6fd04
MA
1457(defconst tramp-file-name-regexp-url
1458 "\\`/[^/:]+://"
1459 "Value for `tramp-file-name-regexp' for URL-like remoting.
1460See `tramp-file-name-structure' for more explanations.")
1461
1462;;;###autoload
1463(defconst tramp-file-name-regexp
1464 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
1465 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
1466 ((equal tramp-syntax 'url) tramp-file-name-regexp-url)
1467 (t (error "Wrong `tramp-syntax' defined")))
94be87e8 1468 "*Regular expression matching file names handled by Tramp.
a4aeb9a4 1469This regexp should match Tramp file names but no other file names.
b533bc97 1470When tramp.el is loaded, this regular expression is prepended to
fb7933a3 1471`file-name-handler-alist', and that is searched sequentially. Thus,
a4aeb9a4
MA
1472if the Tramp entry appears rather early in the `file-name-handler-alist'
1473and is a bit too general, then some files might be considered Tramp
00d6fd04 1474files which are not really Tramp files.
fb7933a3
KG
1475
1476Please note that the entry in `file-name-handler-alist' is made when
b533bc97 1477this file \(tramp.el\) is loaded. This means that this variable must be set
fb7933a3
KG
1478before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1479updated after changing this variable.
1480
00d6fd04 1481Also see `tramp-file-name-structure'.")
fb7933a3 1482
16674e4f 1483;;;###autoload
8a798e41 1484(defconst tramp-root-regexp
00d6fd04 1485 (if (memq system-type '(cygwin windows-nt))
aa485f7c
MA
1486 "\\`\\([a-zA-Z]:\\)?/"
1487 "\\`/")
8a798e41 1488 "Beginning of an incomplete Tramp file name.
aa485f7c 1489Usually, it is just \"\\\\`/\". On W32 systems, there might be a
57671b72 1490volume letter, which will be removed by `tramp-drop-volume-letter'.")
8a798e41
MA
1491
1492;;;###autoload
1493(defconst tramp-completion-file-name-regexp-unified
aa485f7c 1494 (concat tramp-root-regexp "[^/]*\\'")
16674e4f 1495 "Value for `tramp-completion-file-name-regexp' for unified remoting.
8a798e41
MA
1496GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1497See `tramp-file-name-structure' for more explanations.")
fb7933a3 1498
16674e4f
KG
1499;;;###autoload
1500(defconst tramp-completion-file-name-regexp-separate
aa485f7c 1501 (concat tramp-root-regexp "\\([[][^]]*\\)?\\'")
16674e4f
KG
1502 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1503XEmacs uses a separate filename syntax for Tramp and EFS.
00d6fd04 1504See `tramp-file-name-structure' for more explanations.")
fb7933a3 1505
16674e4f 1506;;;###autoload
00d6fd04 1507(defconst tramp-completion-file-name-regexp-url
aa485f7c 1508 (concat tramp-root-regexp "[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
00d6fd04
MA
1509 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1510See `tramp-file-name-structure' for more explanations.")
1511
1512;;;###autoload
1513(defconst tramp-completion-file-name-regexp
1514 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
1515 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
1516 ((equal tramp-syntax 'url) tramp-completion-file-name-regexp-url)
1517 (t (error "Wrong `tramp-syntax' defined")))
a4aeb9a4
MA
1518 "*Regular expression matching file names handled by Tramp completion.
1519This regexp should match partial Tramp file names only.
16674e4f
KG
1520
1521Please note that the entry in `file-name-handler-alist' is made when
1522this file (tramp.el) is loaded. This means that this variable must be set
1523before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1524updated after changing this variable.
1525
00d6fd04 1526Also see `tramp-file-name-structure'.")
fb7933a3 1527
00d6fd04
MA
1528(defconst tramp-actions-before-shell
1529 '((tramp-login-prompt-regexp tramp-action-login)
1530 (tramp-password-prompt-regexp tramp-action-password)
1531 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
ac474af1 1532 (shell-prompt-pattern tramp-action-succeed)
821e6e36 1533 (tramp-shell-prompt-pattern tramp-action-succeed)
3cdaec13 1534 (tramp-yesno-prompt-regexp tramp-action-yesno)
487f4fb7 1535 (tramp-yn-prompt-regexp tramp-action-yn)
19a87064
MA
1536 (tramp-terminal-prompt-regexp tramp-action-terminal)
1537 (tramp-process-alive-regexp tramp-action-process-alive))
ac474af1
KG
1538 "List of pattern/action pairs.
1539Whenever a pattern matches, the corresponding action is performed.
1540Each item looks like (PATTERN ACTION).
1541
1542The PATTERN should be a symbol, a variable. The value of this
1543variable gives the regular expression to search for. Note that the
1544regexp must match at the end of the buffer, \"\\'\" is implicitly
1545appended to it.
1546
1547The ACTION should also be a symbol, but a function. When the
00d6fd04 1548corresponding PATTERN matches, the ACTION function is called.")
ac474af1 1549
00d6fd04 1550(defconst tramp-actions-copy-out-of-band
38c65fca
KG
1551 '((tramp-password-prompt-regexp tramp-action-password)
1552 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
00d6fd04 1553 (tramp-copy-failed-regexp tramp-action-permission-denied)
19a87064 1554 (tramp-process-alive-regexp tramp-action-out-of-band))
38c65fca
KG
1555 "List of pattern/action pairs.
1556This list is used for copying/renaming with out-of-band methods.
90f8dc03 1557
00d6fd04
MA
1558See `tramp-actions-before-shell' for more info.")
1559
1560;; Chunked sending kludge. We set this to 500 for black-listed constellations
7432277c 1561;; known to have a bug in `process-send-string'; some ssh connections appear
7177e2a3
MA
1562;; to drop bytes when data is sent too quickly. There is also a connection
1563;; buffer local variable, which is computed depending on remote host properties
1564;; when `tramp-chunksize' is zero or nil.
7432277c
KG
1565(defcustom tramp-chunksize
1566 (when (and (not (featurep 'xemacs))
1567 (memq system-type '(hpux)))
1568 500)
55880756
MA
1569;; Parentheses in docstring starting at beginning of line are escaped.
1570;; Fontification is messed up when
1571;; `open-paren-in-column-0-is-defun-start' set to t.
7432277c
KG
1572 "*If non-nil, chunksize for sending input to local process.
1573It is necessary only on systems which have a buggy `process-send-string'
1574implementation. The necessity, whether this variable must be set, can be
1575checked via the following code:
1576
1577 (with-temp-buffer
11948172
MA
1578 (let* ((user \"xxx\") (host \"yyy\")
1579 (init 0) (step 50)
1580 (sent init) (received init))
1581 (while (= sent received)
1582 (setq sent (+ sent step))
1583 (erase-buffer)
1584 (let ((proc (start-process (buffer-name) (current-buffer)
1585 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1586 (when (memq (process-status proc) '(run open))
1587 (process-send-string proc (make-string sent ?\\ ))
1588 (process-send-eof proc)
1589 (process-send-eof proc))
1590 (while (not (progn (goto-char (point-min))
1591 (re-search-forward \"\\\\w+\" (point-max) t)))
1592 (accept-process-output proc 1))
1593 (when (memq (process-status proc) '(run open))
1594 (setq received (string-to-number (match-string 0)))
1595 (delete-process proc)
1596 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1597 (sit-for 0))))
1598 (if (> sent (+ init step))
1599 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1600 (- sent step))
1601 (message \"Test does not work\")
1602 (display-buffer (current-buffer))
1603 (sit-for 30))))
1604
1605In the Emacs normally running Tramp, evaluate the above code
55880756 1606\(replace \"xxx\" and \"yyy\" by the remote user and host name,
b533bc97 1607respectively\). You can do this, for example, by pasting it into
11948172
MA
1608the `*scratch*' buffer and then hitting C-j with the cursor after the
1609last closing parenthesis. Note that it works only if you have configured
b533bc97 1610\"ssh\" to run without password query, see ssh-agent\(1\).
11948172
MA
1611
1612You will see the number of bytes sent successfully to the remote host.
1613If that number exceeds 1000, you can stop the execution by hitting
1614C-g, because your Emacs is likely clean.
1615
11948172 1616When it is necessary to set `tramp-chunksize', you might consider to
b533bc97
MA
1617use an out-of-the-band method \(like \"scp\"\) instead of an internal one
1618\(like \"ssh\"\), because setting `tramp-chunksize' to non-nil decreases
11948172 1619performance.
c951aecb 1620
00d6fd04
MA
1621If your Emacs is buggy, the code stops and gives you an indication
1622about the value `tramp-chunksize' should be set. Maybe you could just
1623experiment a bit, e.g. changing the values of `init' and `step'
1624in the third line of the code.
1625
7432277c
KG
1626Please raise a bug report via \"M-x tramp-bug\" if your system needs
1627this variable to be set as well."
1628 :group 'tramp
b1a2b924 1629 :type '(choice (const nil) integer))
7432277c 1630
5ec2cc41
KG
1631;; Logging in to a remote host normally requires obtaining a pty. But
1632;; Emacs on MacOS X has process-connection-type set to nil by default,
1633;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1634;; for an override of the system default.
1635(defcustom tramp-process-connection-type t
1636 "Overrides `process-connection-type' for connections from Tramp.
1637Tramp binds process-connection-type to the value given here before
1638opening a connection to a remote host."
1639 :group 'tramp
1640 :type '(choice (const nil) (const t) (const pty)))
1641
b50dd0d2
MA
1642(defcustom tramp-completion-reread-directory-timeout 10
1643 "Defines seconds since last remote command before rereading a directory.
1644A remote directory might have changed its contents. In order to
1645make it visible during file name completion in the minibuffer,
1646Tramp flushes its cache and rereads the directory contents when
1647more than `tramp-completion-reread-directory-timeout' seconds
1648have been gone since last remote command execution. A value of 0
1649would require an immediate reread during filename completion, nil
1650means to use always cached values for the directory contents."
1651 :group 'tramp
1652 :type '(choice (const nil) integer))
1653
fb7933a3
KG
1654;;; Internal Variables:
1655
fb7933a3 1656(defvar tramp-current-method nil
00d6fd04 1657 "Connection method for this *tramp* buffer.")
fb7933a3
KG
1658
1659(defvar tramp-current-user nil
00d6fd04 1660 "Remote login name for this *tramp* buffer.")
fb7933a3
KG
1661
1662(defvar tramp-current-host nil
00d6fd04
MA
1663 "Remote host for this *tramp* buffer.")
1664
1665(defconst tramp-uudecode
1666 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
fabf2143 1667cat /tmp/tramp.$$
00d6fd04 1668rm -f /tmp/tramp.$$"
fabf2143 1669 "Shell function to implement `uudecode' to standard output.
c08e6004
MA
1670Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1671for this or `uudecode -p', but some systems don't, and for them
1672we have this shell function.")
fabf2143 1673
293c24f9
MA
1674(defconst tramp-perl-file-truename
1675 "%s -e '
1676use File::Spec;
1677use Cwd \"realpath\";
1678
1679sub recursive {
1680 my ($volume, @dirs) = @_;
1681 my $real = realpath(File::Spec->catpath(
1682 $volume, File::Spec->catdir(@dirs), \"\"));
1683 if ($real) {
1684 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1685 return ($vol, File::Spec->splitdir($dir));
1686 }
1687 else {
1688 my $last = pop(@dirs);
1689 ($volume, @dirs) = recursive($volume, @dirs);
1690 push(@dirs, $last);
1691 return ($volume, @dirs);
1692 }
1693}
1694
1695$result = realpath($ARGV[0]);
1696if (!$result) {
1697 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1698 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1699
1700 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1701}
1702
1703if ($ARGV[0] =~ /\\/$/) {
1704 $result = $result . \"/\";
1705}
1706
1707print \"\\\"$result\\\"\\n\";
1708' \"$1\" 2>/dev/null"
1709 "Perl script to produce output suitable for use with `file-truename'
1710on the remote file system.
1711Escape sequence %s is replaced with name of Perl binary.
1712This string is passed to `format', so percent characters need to be doubled.")
1713
1714(defconst tramp-perl-file-name-all-completions
1715 "%s -e 'sub case {
1716 my $str = shift;
1717 if ($ARGV[2]) {
1718 return lc($str);
1719 }
1720 else {
1721 return $str;
1722 }
1723}
1724opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1725@files = readdir(d); closedir(d);
1726foreach $f (@files) {
1727 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1728 if (-d \"$ARGV[0]/$f\") {
1729 print \"$f/\\n\";
1730 }
1731 else {
1732 print \"$f\\n\";
1733 }
1734 }
1735}
1736print \"ok\\n\"
1737' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1738 "Perl script to produce output suitable for use with
1739`file-name-all-completions' on the remote file system. Escape
1740sequence %s is replaced with name of Perl binary. This string is
1741passed to `format', so percent characters need to be doubled.")
1742
fabf2143
KG
1743;; Perl script to implement `file-attributes' in a Lisp `read'able
1744;; output. If you are hacking on this, note that you get *no* output
1745;; unless this spits out a complete line, including the '\n' at the
1746;; end.
8daea7fc 1747;; The device number is returned as "-1", because there will be a virtual
b946a456 1748;; device number set in `tramp-handle-file-attributes'.
00d6fd04
MA
1749(defconst tramp-perl-file-attributes
1750 "%s -e '
c82c5727 1751@stat = lstat($ARGV[0]);
680db9ac
MA
1752if (!@stat) {
1753 print \"nil\\n\";
1754 exit 0;
1755}
c82c5727
LH
1756if (($stat[2] & 0170000) == 0120000)
1757{
1758 $type = readlink($ARGV[0]);
1759 $type = \"\\\"$type\\\"\";
1760}
1761elsif (($stat[2] & 0170000) == 040000)
1762{
1763 $type = \"t\";
1764}
1765else
1766{
1767 $type = \"nil\"
1768};
1769$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1770$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1771printf(
d4443a0d 1772 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
c82c5727
LH
1773 $type,
1774 $stat[3],
1775 $uid,
1776 $gid,
1777 $stat[8] >> 16 & 0xffff,
1778 $stat[8] & 0xffff,
1779 $stat[9] >> 16 & 0xffff,
1780 $stat[9] & 0xffff,
1781 $stat[10] >> 16 & 0xffff,
1782 $stat[10] & 0xffff,
1783 $stat[7],
1784 $stat[2],
1785 $stat[1] >> 16 & 0xffff,
1786 $stat[1] & 0xffff
00d6fd04 1787);' \"$1\" \"$2\" \"$3\" 2>/dev/null"
fb7933a3 1788 "Perl script to produce output suitable for use with `file-attributes'
00d6fd04
MA
1789on the remote file system.
1790Escape sequence %s is replaced with name of Perl binary.
1791This string is passed to `format', so percent characters need to be doubled.")
fb7933a3 1792
00d6fd04
MA
1793(defconst tramp-perl-directory-files-and-attributes
1794 "%s -e '
8cb0a559
LH
1795chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1796opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
c82c5727
LH
1797@list = readdir(DIR);
1798closedir(DIR);
1799$n = scalar(@list);
1800printf(\"(\\n\");
1801for($i = 0; $i < $n; $i++)
1802{
1803 $filename = $list[$i];
1804 @stat = lstat($filename);
1805 if (($stat[2] & 0170000) == 0120000)
1806 {
1807 $type = readlink($filename);
1808 $type = \"\\\"$type\\\"\";
1809 }
1810 elsif (($stat[2] & 0170000) == 040000)
1811 {
1812 $type = \"t\";
1813 }
1814 else
1815 {
1816 $type = \"nil\"
1817 };
1818 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1819 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1820 printf(
b946a456 1821 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
c82c5727
LH
1822 $filename,
1823 $type,
1824 $stat[3],
1825 $uid,
1826 $gid,
1827 $stat[8] >> 16 & 0xffff,
1828 $stat[8] & 0xffff,
1829 $stat[9] >> 16 & 0xffff,
1830 $stat[9] & 0xffff,
1831 $stat[10] >> 16 & 0xffff,
1832 $stat[10] & 0xffff,
1833 $stat[7],
1834 $stat[2],
1835 $stat[1] >> 16 & 0xffff,
1836 $stat[1] & 0xffff,
1837 $stat[0] >> 16 & 0xffff,
1838 $stat[0] & 0xffff);
1839}
00d6fd04 1840printf(\")\\n\");' \"$1\" \"$2\" \"$3\" 2>/dev/null"
c82c5727 1841 "Perl script implementing `directory-files-attributes' as Lisp `read'able
00d6fd04
MA
1842output.
1843Escape sequence %s is replaced with name of Perl binary.
1844This string is passed to `format', so percent characters need to be doubled.")
c82c5727 1845
ac474af1
KG
1846;; ;; These two use uu encoding.
1847;; (defvar tramp-perl-encode "%s -e'\
1848;; print qq(begin 644 xxx\n);
1849;; my $s = q();
1850;; my $res = q();
1851;; while (read(STDIN, $s, 45)) {
1852;; print pack(q(u), $s);
1853;; }
1854;; print qq(`\n);
1855;; print qq(end\n);
1856;; '"
1857;; "Perl program to use for encoding a file.
1858;; Escape sequence %s is replaced with name of Perl binary.")
1859
1860;; (defvar tramp-perl-decode "%s -ne '
1861;; print unpack q(u), $_;
1862;; '"
1863;; "Perl program to use for decoding a file.
1864;; Escape sequence %s is replaced with name of Perl binary.")
1865
1866;; These two use base64 encoding.
00d6fd04
MA
1867(defconst tramp-perl-encode-with-module
1868 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
ac474af1 1869 "Perl program to use for encoding a file.
b1d06e75 1870Escape sequence %s is replaced with name of Perl binary.
89509ea0 1871This string is passed to `format', so percent characters need to be doubled.
b1d06e75
KG
1872This implementation requires the MIME::Base64 Perl module to be installed
1873on the remote host.")
1874
00d6fd04
MA
1875(defconst tramp-perl-decode-with-module
1876 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
b1d06e75
KG
1877 "Perl program to use for decoding a file.
1878Escape sequence %s is replaced with name of Perl binary.
89509ea0 1879This string is passed to `format', so percent characters need to be doubled.
b1d06e75
KG
1880This implementation requires the MIME::Base64 Perl module to be installed
1881on the remote host.")
1882
00d6fd04 1883(defconst tramp-perl-encode
b1d06e75
KG
1884 "%s -e '
1885# This script contributed by Juanma Barranquero <lektu@terra.es>.
114f9c96 1886# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
cbd12ed7 1887# Free Software Foundation, Inc.
b1d06e75
KG
1888use strict;
1889
fa32e96a 1890my %%trans = do {
b1d06e75
KG
1891 my $i = 0;
1892 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1893 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1894};
1895
36541701 1896binmode(\\*STDIN);
b1d06e75
KG
1897
1898# We read in chunks of 54 bytes, to generate output lines
1899# of 72 chars (plus end of line)
36541701 1900$/ = \\54;
b1d06e75
KG
1901
1902while (my $data = <STDIN>) {
1903 my $pad = q();
1904
1905 # Only for the last chunk, and only if did not fill the last three-byte packet
1906 if (eof) {
fa32e96a 1907 my $mod = length($data) %% 3;
b1d06e75
KG
1908 $pad = q(=) x (3 - $mod) if $mod;
1909 }
1910
1911 # Not the fastest method, but it is simple: unpack to binary string, split
1912 # by groups of 6 bits and convert back from binary to byte; then map into
1913 # the translation table
1914 print
1915 join q(),
1916 map($trans{$_},
1917 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1918 $pad,
36541701 1919 qq(\\n);
00d6fd04 1920}' 2>/dev/null"
b1d06e75 1921 "Perl program to use for encoding a file.
fa32e96a 1922Escape sequence %s is replaced with name of Perl binary.
ccf29586 1923This string is passed to `format', so percent characters need to be doubled.")
ac474af1 1924
00d6fd04 1925(defconst tramp-perl-decode
b1d06e75
KG
1926 "%s -e '
1927# This script contributed by Juanma Barranquero <lektu@terra.es>.
114f9c96 1928# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
cbd12ed7 1929# Free Software Foundation, Inc.
b1d06e75
KG
1930use strict;
1931
fa32e96a 1932my %%trans = do {
b1d06e75 1933 my $i = 0;
16674e4f 1934 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
b1d06e75
KG
1935 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1936};
1937
fa32e96a 1938my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
b1d06e75 1939
36541701 1940binmode(\\*STDOUT);
b1d06e75
KG
1941
1942# We are going to accumulate into $pending to accept any line length
1943# (we do not check they are <= 76 chars as the RFC says)
1944my $pending = q();
1945
1946while (my $data = <STDIN>) {
1947 chomp $data;
1948
1949 # If we find one or two =, we have reached the end and
1950 # any following data is to be discarded
1951 my $finished = $data =~ s/(==?).*/$1/;
1952 $pending .= $data;
1953
1954 my $len = length($pending);
16674e4f 1955 my $chunk = substr($pending, 0, $len & ~3);
414da5ab 1956 $pending = substr($pending, $len & ~3 + 1);
b1d06e75
KG
1957
1958 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1959 # split in 8-bit chunks and convert back to char.
1960 print join q(),
1961 map $bytes{$_},
1962 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1963
1964 last if $finished;
00d6fd04 1965}' 2>/dev/null"
ac474af1 1966 "Perl program to use for decoding a file.
fa32e96a 1967Escape sequence %s is replaced with name of Perl binary.
ccf29586 1968This string is passed to `format', so percent characters need to be doubled.")
fb7933a3 1969
946a5aeb
MA
1970(defconst tramp-vc-registered-read-file-names
1971 "echo \"(\"
1972for file in \"$@\"; do
1973 if %s $file; then
1974 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1975 else
1976 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1977 fi
1978 if %s $file; then
1979 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1980 else
1981 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1982 fi
1983done
1984echo \")\""
1985 "Script to check existence of VC related files.
1986It must be send formatted with two strings; the tests for file
1987existence, and file readability.")
1988
9ce8462a
MA
1989(defconst tramp-file-mode-type-map
1990 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1991 (1 . "p") ; fifo
1992 (2 . "c") ; character device
1993 (3 . "m") ; multiplexed character device (v7)
1994 (4 . "d") ; directory
1995 (5 . "?") ; Named special file (XENIX)
1996 (6 . "b") ; block device
1997 (7 . "?") ; multiplexed block device (v7)
1998 (8 . "-") ; regular file
1999 (9 . "n") ; network special file (HP-UX)
2000 (10 . "l") ; symlink
2001 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
2002 (12 . "s") ; socket
2003 (13 . "D") ; door special (Solaris)
2004 (14 . "w")) ; whiteout (BSD)
fb7933a3
KG
2005 "A list of file types returned from the `stat' system call.
2006This is used to map a mode number to a permission string.")
2007
fb7933a3 2008;; New handlers should be added here. The following operations can be
c0fc6170
MA
2009;; handled using the normal primitives: file-name-sans-versions,
2010;; get-file-buffer.
fb7933a3 2011(defconst tramp-file-name-handler-alist
00d6fd04 2012 '((load . tramp-handle-load)
fb7933a3 2013 (make-symbolic-link . tramp-handle-make-symbolic-link)
c0fc6170 2014 (file-name-as-directory . tramp-handle-file-name-as-directory)
fb7933a3
KG
2015 (file-name-directory . tramp-handle-file-name-directory)
2016 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
2017 (file-truename . tramp-handle-file-truename)
2018 (file-exists-p . tramp-handle-file-exists-p)
2019 (file-directory-p . tramp-handle-file-directory-p)
2020 (file-executable-p . tramp-handle-file-executable-p)
fb7933a3
KG
2021 (file-readable-p . tramp-handle-file-readable-p)
2022 (file-regular-p . tramp-handle-file-regular-p)
2023 (file-symlink-p . tramp-handle-file-symlink-p)
2024 (file-writable-p . tramp-handle-file-writable-p)
2025 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
2026 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
2027 (file-attributes . tramp-handle-file-attributes)
2028 (file-modes . tramp-handle-file-modes)
fb7933a3 2029 (directory-files . tramp-handle-directory-files)
c82c5727 2030 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
fb7933a3
KG
2031 (file-name-all-completions . tramp-handle-file-name-all-completions)
2032 (file-name-completion . tramp-handle-file-name-completion)
2033 (add-name-to-file . tramp-handle-add-name-to-file)
2034 (copy-file . tramp-handle-copy-file)
263c02ef 2035 (copy-directory . tramp-handle-copy-directory)
fb7933a3
KG
2036 (rename-file . tramp-handle-rename-file)
2037 (set-file-modes . tramp-handle-set-file-modes)
ce3f516f 2038 (set-file-times . tramp-handle-set-file-times)
fb7933a3
KG
2039 (make-directory . tramp-handle-make-directory)
2040 (delete-directory . tramp-handle-delete-directory)
2041 (delete-file . tramp-handle-delete-file)
2042 (directory-file-name . tramp-handle-directory-file-name)
00d6fd04
MA
2043 ;; `executable-find' is not official yet.
2044 (executable-find . tramp-handle-executable-find)
2045 (start-file-process . tramp-handle-start-file-process)
0457dd55 2046 (process-file . tramp-handle-process-file)
00d6fd04 2047 (shell-command . tramp-handle-shell-command)
fb7933a3
KG
2048 (insert-directory . tramp-handle-insert-directory)
2049 (expand-file-name . tramp-handle-expand-file-name)
00d6fd04 2050 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
fb7933a3 2051 (file-local-copy . tramp-handle-file-local-copy)
19a87064 2052 (file-remote-p . tramp-handle-file-remote-p)
fb7933a3 2053 (insert-file-contents . tramp-handle-insert-file-contents)
94be87e8
MA
2054 (insert-file-contents-literally
2055 . tramp-handle-insert-file-contents-literally)
fb7933a3 2056 (write-region . tramp-handle-write-region)
38c65fca 2057 (find-backup-file-name . tramp-handle-find-backup-file-name)
c1105d05 2058 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
fb7933a3 2059 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
5ec2cc41 2060 (dired-compress-file . tramp-handle-dired-compress-file)
fb7933a3
KG
2061 (dired-recursive-delete-directory
2062 . tramp-handle-dired-recursive-delete-directory)
70c11b0b 2063 (dired-uncache . tramp-handle-dired-uncache)
fb7933a3 2064 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
49096407 2065 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
632c5478
MA
2066 (file-selinux-context . tramp-handle-file-selinux-context)
2067 (set-file-selinux-context . tramp-handle-set-file-selinux-context)
49096407 2068 (vc-registered . tramp-handle-vc-registered))
c1105d05 2069 "Alist of handler functions.
fb7933a3
KG
2070Operations not mentioned here will be handled by the normal Emacs functions.")
2071
a4aeb9a4 2072;; Handlers for partial Tramp file names. For Emacs just
41c8e348 2073;; `file-name-all-completions' is needed.
a01b1e22 2074;;;###autoload
16674e4f 2075(defconst tramp-completion-file-name-handler-alist
a01b1e22 2076 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
41c8e348 2077 (file-name-completion . tramp-completion-handle-file-name-completion))
16674e4f
KG
2078 "Alist of completion handler functions.
2079Used for file names matching `tramp-file-name-regexp'. Operations not
2080mentioned here will be handled by `tramp-file-name-handler-alist' or the
2081normal Emacs functions.")
2082
4007ba5b 2083;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
ea9d1443
KG
2084(defvar tramp-foreign-file-name-handler-alist
2085 ;; (identity . tramp-sh-file-name-handler) should always be the last
b88f2d0a 2086 ;; entry, because `identity' always matches.
ea9d1443 2087 '((identity . tramp-sh-file-name-handler))
4007ba5b
KG
2088 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2089If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2090calling HANDLER.")
2091
0664ff72 2092;;; Internal functions which must come first:
fb7933a3 2093
00d6fd04
MA
2094(defsubst tramp-debug-message (vec fmt-string &rest args)
2095 "Append message to debug buffer.
2096Message is formatted with FMT-STRING as control string and the remaining
2097ARGS to actually emit the message (if applicable)."
2098 (when (get-buffer (tramp-buffer-name vec))
2099 (with-current-buffer (tramp-get-debug-buffer vec)
2100 (goto-char (point-max))
70c11b0b
MA
2101 ;; Headline.
2102 (when (bobp)
2103 (insert
2104 (format
2105 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
2106 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
2107 emacs-version tramp-version)))
00d6fd04
MA
2108 (unless (bolp)
2109 (insert "\n"))
70c11b0b 2110 ;; Timestamp.
736ac90f
MA
2111 (let ((now (current-time)))
2112 (insert (format-time-string "%T." now))
2113 (insert (format "%06d " (nth 2 now))))
70c11b0b 2114 ;; Calling function.
00d6fd04
MA
2115 (let ((btn 1) btf fn)
2116 (while (not fn)
2117 (setq btf (nth 1 (backtrace-frame btn)))
2118 (if (not btf)
2119 (setq fn "")
2120 (when (symbolp btf)
2121 (setq fn (symbol-name btf))
2122 (unless (and (string-match "^tramp" fn)
2123 (not (string-match
b6827fff 2124 "^tramp\\(-debug\\)?\\(-message\\|-error\\|-compat-funcall\\)$"
00d6fd04
MA
2125 fn)))
2126 (setq fn nil)))
2127 (setq btn (1+ btn))))
2128 ;; The following code inserts filename and line number.
2129 ;; Should be deactivated by default, because it is time
2130 ;; consuming.
2131; (let ((ffn (find-function-noselect (intern fn))))
2132; (insert
2133; (format
2134; "%s:%d: "
2135; (file-name-nondirectory (buffer-file-name (car ffn)))
2136; (with-current-buffer (car ffn)
2137; (1+ (count-lines (point-min) (cdr ffn)))))))
2138 (insert (format "%s " fn)))
70c11b0b 2139 ;; The message.
00d6fd04
MA
2140 (insert (apply 'format fmt-string args)))))
2141
946a5aeb
MA
2142(defvar tramp-message-show-message t
2143 "Show Tramp message in the minibuffer.
2144This variable is used to disable messages from `tramp-error'.
2145The messages are visible anyway, because an error is raised.")
2146
00d6fd04 2147(defsubst tramp-message (vec-or-proc level fmt-string &rest args)
fb7933a3 2148 "Emit a message depending on verbosity level.
a4aeb9a4 2149VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
00d6fd04
MA
2150vector or a process. LEVEL says to be quiet if `tramp-verbose' is
2151less than LEVEL. The message is emitted only if `tramp-verbose' is
2152greater than or equal to LEVEL.
2153
2154The message is also logged into the debug buffer when `tramp-verbose'
2155is greater than or equal 4.
2156
2157Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2158control string and the remaining ARGS to actually emit the message (if
2159applicable)."
2160 (condition-case nil
2161 (when (<= level tramp-verbose)
2162 ;; Match data must be preserved!
2163 (save-match-data
2164 ;; Display only when there is a minimum level.
946a5aeb 2165 (when (and tramp-message-show-message (<= level 3))
00d6fd04
MA
2166 (apply 'message
2167 (concat
2168 (cond
2169 ((= level 0) "")
2170 ((= level 1) "")
2171 ((= level 2) "Warning: ")
2172 (t "Tramp: "))
2173 fmt-string)
2174 args))
2175 ;; Log only when there is a minimum level.
2176 (when (>= tramp-verbose 4)
2177 (when (and vec-or-proc
2178 (processp vec-or-proc)
2179 (buffer-name (process-buffer vec-or-proc)))
2180 (with-current-buffer (process-buffer vec-or-proc)
2181 ;; Translate proc to vec.
2182 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
2183 (when (and vec-or-proc (vectorp vec-or-proc))
2184 (apply 'tramp-debug-message
2185 vec-or-proc
2186 (concat (format "(%d) # " level) fmt-string)
2187 args)))))
2188 ;; Suppress all errors.
2189 (error nil)))
2190
2191(defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
2192 "Emit an error.
2193VEC-OR-PROC identifies the connection to use, SIGNAL is the
2194signal identifier to be raised, remaining args passed to
2195`tramp-message'. Finally, signal SIGNAL is raised."
946a5aeb
MA
2196 (let (tramp-message-show-message)
2197 (tramp-message
2198 vec-or-proc 1 "%s"
2199 (error-message-string
2200 (list signal
2201 (get signal 'error-message)
2202 (apply 'format fmt-string args))))
2203 (signal signal (list (apply 'format fmt-string args)))))
00d6fd04
MA
2204
2205(defsubst tramp-error-with-buffer
2206 (buffer vec-or-proc signal fmt-string &rest args)
2207 "Emit an error, and show BUFFER.
2208If BUFFER is nil, show the connection buffer. Wait for 30\", or until
2209an input event arrives. The other arguments are passed to `tramp-error'."
2210 (save-window-excursion
2211 (unwind-protect
2212 (apply 'tramp-error vec-or-proc signal fmt-string args)
4874f5e6
MA
2213 (when (and vec-or-proc
2214 (not (zerop tramp-verbose))
2215 (not (tramp-completion-mode-p)))
00d6fd04
MA
2216 (let ((enable-recursive-minibuffers t))
2217 (pop-to-buffer
2218 (or (and (bufferp buffer) buffer)
2219 (and (processp vec-or-proc) (process-buffer vec-or-proc))
2220 (tramp-get-buffer vec-or-proc)))
2221 (sit-for 30))))))
fb7933a3 2222
c62c9d08
KG
2223(defmacro with-parsed-tramp-file-name (filename var &rest body)
2224 "Parse a Tramp filename and make components available in the body.
2225
2226First arg FILENAME is evaluated and dissected into its components.
2227Second arg VAR is a symbol. It is used as a variable name to hold
2228the filename structure. It is also used as a prefix for the variables
2229holding the components. For example, if VAR is the symbol `foo', then
00d6fd04
MA
2230`foo' will be bound to the whole structure, `foo-method' will be bound to
2231the method component, and so on for `foo-user', `foo-host', `foo-localname'.
c62c9d08
KG
2232
2233Remaining args are Lisp expressions to be evaluated (inside an implicit
2234`progn').
2235
00d6fd04
MA
2236If VAR is nil, then we bind `v' to the structure and `method', `user',
2237`host', `localname' to the components."
c62c9d08 2238 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
c62c9d08
KG
2239 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2240 (tramp-file-name-method ,(or var 'v)))
2241 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2242 (tramp-file-name-user ,(or var 'v)))
2243 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2244 (tramp-file-name-host ,(or var 'v)))
7432277c
KG
2245 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2246 (tramp-file-name-localname ,(or var 'v))))
c62c9d08
KG
2247 ,@body))
2248
2249(put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
00d6fd04 2250(put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
9e6ab520 2251(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
c62c9d08 2252
00d6fd04
MA
2253(defmacro with-file-property (vec file property &rest body)
2254 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2255FILE must be a local file name on a connection identified via VEC."
2256 `(if (file-name-absolute-p ,file)
2257 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
2258 (when (eq value 'undef)
2259 ;; We cannot pass @body as parameter to
2260 ;; `tramp-set-file-property' because it mangles our
2261 ;; debug messages.
2262 (setq value (progn ,@body))
2263 (tramp-set-file-property ,vec ,file ,property value))
2264 value)
2265 ,@body))
9ce8462a 2266
00d6fd04
MA
2267(put 'with-file-property 'lisp-indent-function 3)
2268(put 'with-file-property 'edebug-form-spec t)
9e6ab520 2269(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>"))
00d6fd04
MA
2270
2271(defmacro with-connection-property (key property &rest body)
a94d821f 2272 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
00d6fd04
MA
2273 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
2274 (when (eq value 'undef)
2275 ;; We cannot pass ,@body as parameter to
2276 ;; `tramp-set-connection-property' because it mangles our debug
2277 ;; messages.
2278 (setq value (progn ,@body))
2279 (tramp-set-connection-property ,key ,property value))
2280 value))
9ce8462a 2281
00d6fd04
MA
2282(put 'with-connection-property 'lisp-indent-function 2)
2283(put 'with-connection-property 'edebug-form-spec t)
9e6ab520 2284(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>"))
00d6fd04 2285
9e021389
MA
2286(defun tramp-progress-reporter-update (reporter &optional value)
2287 (let* ((parameters (cdr reporter))
2288 (message (aref parameters 3)))
2289 (when (string-match message (or (current-message) ""))
2290 (funcall 'progress-reporter-update reporter value))))
2291
a94d821f 2292(defmacro with-progress-reporter (vec level message &rest body)
b6827fff
MA
2293 "Executes BODY, spinning a progress reporter with MESSAGE.
2294If LEVEL does not fit for visible messages, or if this is a
2295nested call of the macro, there are only traces without a visible
2296progress reporter."
a94d821f
MA
2297 `(let (pr tm)
2298 (tramp-message ,vec ,level "%s..." ,message)
2299 ;; We start a pulsing progress reporter after 3 seconds. Feature
2300 ;; introduced in Emacs 24.1.
3b30ccda
MA
2301 (when (and tramp-message-show-message
2302 ;; Display only when there is a minimum level.
2303 (<= ,level (min tramp-verbose 3)))
a94d821f 2304 (condition-case nil
0d5852cf 2305 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message)
9e021389
MA
2306 tm (when pr
2307 (run-at-time 3 0.1 'tramp-progress-reporter-update pr)))
a94d821f
MA
2308 (error nil)))
2309 (unwind-protect
b6827fff
MA
2310 ;; Execute the body. Unset `tramp-message-show-message' when
2311 ;; the timer object is created, in order to suppress
2312 ;; concurrent timers.
3b30ccda
MA
2313 (let ((tramp-message-show-message
2314 (and tramp-message-show-message (not tm))))
2315 ,@body)
a94d821f 2316 ;; Stop progress reporter.
0d5852cf 2317 (if tm (tramp-compat-funcall 'cancel-timer tm))
a94d821f
MA
2318 (tramp-message ,vec ,level "%s...done" ,message))))
2319
2320(put 'with-progress-reporter 'lisp-indent-function 3)
2321(put 'with-progress-reporter 'edebug-form-spec t)
2322(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-progress-reporter\\>"))
2323
2324(eval-and-compile ;; Silence compiler.
628c97b2
GM
2325 (if (memq system-type '(cygwin windows-nt))
2326 (defun tramp-drop-volume-letter (name)
2327 "Cut off unnecessary drive letter from file NAME.
2328The function `tramp-handle-expand-file-name' calls `expand-file-name'
2329locally on a remote file name. When the local system is a W32 system
2330but the remote system is Unix, this introduces a superfluous drive
2331letter into the file name. This function removes it."
2332 (save-match-data
2333 (if (string-match tramp-root-regexp name)
2334 (replace-match "/" nil t name)
2335 name)))
2336
2337 (defalias 'tramp-drop-volume-letter 'identity)))
2338
9c13938d 2339(defsubst tramp-make-tramp-temp-file (vec)
a6e96327 2340 "Create a temporary file on the remote host identified by VEC.
9c13938d
MA
2341Return the local name of the temporary file."
2342 (let ((prefix
2343 (tramp-make-tramp-file-name
2344 (tramp-file-name-method vec)
2345 (tramp-file-name-user vec)
2346 (tramp-file-name-host vec)
113e2a84
MA
2347 (tramp-drop-volume-letter
2348 (expand-file-name
2349 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
9c13938d
MA
2350 result)
2351 (while (not result)
2352 ;; `make-temp-file' would be the natural choice for
2353 ;; implementation. But it calls `write-region' internally,
2354 ;; which also needs a temporary file - we would end in an
2355 ;; infinite loop.
2356 (setq result (make-temp-name prefix))
2357 (if (file-exists-p result)
2358 (setq result nil)
2359 ;; This creates the file by side effect.
2360 (set-file-times result)
2361 (set-file-modes result (tramp-octal-to-decimal "0700"))))
2362
2363 ;; Return the local part.
2364 (with-parsed-tramp-file-name result nil localname)))
8a4438b6
MA
2365
2366
16674e4f
KG
2367;;; Config Manipulation Functions:
2368
2369(defun tramp-set-completion-function (method function-list)
2370 "Sets the list of completion functions for METHOD.
2371FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2372The FUNCTION is intended to parse FILE according its syntax.
2373It might be a predefined FUNCTION, or a user defined FUNCTION.
2374Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
8fc29035 2375`tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
8daea7fc
KG
2376and `tramp-parse-netrc'.
2377
16674e4f
KG
2378Example:
2379
2380 (tramp-set-completion-function
2381 \"ssh\"
8daea7fc
KG
2382 '((tramp-parse-sconfig \"/etc/ssh_config\")
2383 (tramp-parse-sconfig \"~/.ssh/config\")))"
16674e4f 2384
5ec2cc41
KG
2385 (let ((r function-list)
2386 (v function-list))
2387 (setq tramp-completion-function-alist
2388 (delete (assoc method tramp-completion-function-alist)
2389 tramp-completion-function-alist))
2390
2391 (while v
00d6fd04 2392 ;; Remove double entries.
5ec2cc41
KG
2393 (when (member (car v) (cdr v))
2394 (setcdr v (delete (car v) (cdr v))))
00d6fd04 2395 ;; Check for function and file or registry key.
5ec2cc41 2396 (unless (and (functionp (nth 0 (car v)))
00d6fd04
MA
2397 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
2398 ;; Windows registry.
2399 (and (memq system-type '(cygwin windows-nt))
a4aeb9a4
MA
2400 (zerop
2401 (tramp-local-call-process
2402 "reg" nil nil nil "query" (nth 1 (car v)))))
00d6fd04
MA
2403 ;; Configuration file.
2404 (file-exists-p (nth 1 (car v)))))
5ec2cc41
KG
2405 (setq r (delete (car v) r)))
2406 (setq v (cdr v)))
2407
2408 (when r
4007ba5b 2409 (add-to-list 'tramp-completion-function-alist
5ec2cc41 2410 (cons method r)))))
16674e4f
KG
2411
2412(defun tramp-get-completion-function (method)
00d6fd04 2413 "Returns a list of completion functions for METHOD.
16674e4f 2414For definition of that list see `tramp-set-completion-function'."
00d6fd04
MA
2415 (cons
2416 ;; Hosts visited once shall be remembered.
2417 `(tramp-parse-connection-properties ,method)
2418 ;; The method related defaults.
2419 (cdr (assoc method tramp-completion-function-alist))))
16674e4f 2420
d037d501 2421
0664ff72 2422;;; Fontification of `read-file-name':
d037d501 2423
0664ff72 2424;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
d037d501
MA
2425(defvar tramp-rfn-eshadow-overlay)
2426(make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
2427
2428(defun tramp-rfn-eshadow-setup-minibuffer ()
2429 "Set up a minibuffer for `file-name-shadow-mode'.
2430Adds another overlay hiding filename parts according to Tramp's
2431special handling of `substitute-in-file-name'."
9ce8462a 2432 (when (symbol-value 'minibuffer-completing-file-name)
d037d501 2433 (setq tramp-rfn-eshadow-overlay
0d5852cf
MA
2434 (tramp-compat-funcall
2435 'make-overlay
2436 (tramp-compat-funcall 'minibuffer-prompt-end)
2437 (tramp-compat-funcall 'minibuffer-prompt-end)))
d037d501 2438 ;; Copy rfn-eshadow-overlay properties.
0d5852cf
MA
2439 (let ((props (tramp-compat-funcall
2440 'overlay-properties (symbol-value 'rfn-eshadow-overlay))))
d037d501 2441 (while props
0d5852cf
MA
2442 (tramp-compat-funcall
2443 'overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))))))
d037d501
MA
2444
2445(when (boundp 'rfn-eshadow-setup-minibuffer-hook)
2446 (add-hook 'rfn-eshadow-setup-minibuffer-hook
48846dc5
MA
2447 'tramp-rfn-eshadow-setup-minibuffer)
2448 (add-hook 'tramp-unload-hook
aa485f7c
MA
2449 (lambda ()
2450 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2451 'tramp-rfn-eshadow-setup-minibuffer))))
d037d501 2452
adcbca53
MA
2453(defconst tramp-rfn-eshadow-update-overlay-regexp
2454 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
2455
d037d501
MA
2456(defun tramp-rfn-eshadow-update-overlay ()
2457 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2458This is intended to be used as a minibuffer `post-command-hook' for
2459`file-name-shadow-mode'; the minibuffer should have already
2460been set up by `rfn-eshadow-setup-minibuffer'."
2461 ;; In remote files name, there is a shadowing just for the local part.
0d5852cf
MA
2462 (let ((end (or (tramp-compat-funcall
2463 'overlay-end (symbol-value 'rfn-eshadow-overlay))
2464 (tramp-compat-funcall 'minibuffer-prompt-end))))
2465 (when
2466 (file-remote-p
2467 (tramp-compat-funcall 'buffer-substring-no-properties end (point-max)))
bd316474
KY
2468 (save-excursion
2469 (save-restriction
2470 (narrow-to-region
adcbca53
MA
2471 (1+ (or (string-match
2472 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
2473 end))
2474 (point-max))
bd316474
KY
2475 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
2476 (rfn-eshadow-update-overlay-hook nil))
0d5852cf
MA
2477 (tramp-compat-funcall
2478 'move-overlay rfn-eshadow-overlay (point-max) (point-max))
2479 (tramp-compat-funcall 'rfn-eshadow-update-overlay)))))))
d037d501
MA
2480
2481(when (boundp 'rfn-eshadow-update-overlay-hook)
2482 (add-hook 'rfn-eshadow-update-overlay-hook
b88f2d0a
MA
2483 'tramp-rfn-eshadow-update-overlay)
2484 (add-hook 'tramp-unload-hook
2485 (lambda ()
2486 (remove-hook 'rfn-eshadow-update-overlay-hook
2487 'tramp-rfn-eshadow-update-overlay))))
d037d501
MA
2488
2489
605a20a9
MA
2490;;; Integration of eshell.el:
2491
2492(eval-when-compile
2493 (defvar eshell-path-env))
2494
2495;; eshell.el keeps the path in `eshell-path-env'. We must change it
2496;; when `default-directory' points to another host.
2497(defun tramp-eshell-directory-change ()
2498 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2499 (setq eshell-path-env
2500 (if (file-remote-p default-directory)
2501 (with-parsed-tramp-file-name default-directory nil
2502 (mapconcat
2503 'identity
2504 (tramp-get-remote-path v)
2505 ":"))
2506 (getenv "PATH"))))
2507
2508(eval-after-load "esh-util"
2509 '(progn
2510 (tramp-eshell-directory-change)
2511 (add-hook 'eshell-directory-change-hook
2512 'tramp-eshell-directory-change)
2513 (add-hook 'tramp-unload-hook
2514 (lambda ()
2515 (remove-hook 'eshell-directory-change-hook
2516 'tramp-eshell-directory-change)))))
2517
2518
fb7933a3
KG
2519;;; File Name Handler Functions:
2520
fb7933a3
KG
2521(defun tramp-handle-make-symbolic-link
2522 (filename linkname &optional ok-if-already-exists)
00d6fd04 2523 "Like `make-symbolic-link' for Tramp files.
cebb4ec6 2524If LINKNAME is a non-Tramp file, it is used verbatim as the target of
7432277c 2525the symlink. If LINKNAME is a Tramp file, only the localname component is
cebb4ec6
KG
2526used as the target of the symlink.
2527
7432277c
KG
2528If LINKNAME is a Tramp file and the localname component is relative, then
2529it is expanded first, before the localname component is taken. Note that
cebb4ec6
KG
2530this can give surprising results if the user/host for the source and
2531target of the symlink differ."
c62c9d08 2532 (with-parsed-tramp-file-name linkname l
00d6fd04 2533 (let ((ln (tramp-get-remote-ln l))
87bdd2c7
MA
2534 (cwd (tramp-run-real-handler
2535 'file-name-directory (list l-localname))))
c62c9d08 2536 (unless ln
00d6fd04
MA
2537 (tramp-error
2538 l 'file-error
2539 "Making a symbolic link. ln(1) does not exist on the remote host."))
c62c9d08
KG
2540
2541 ;; Do the 'confirm if exists' thing.
cebb4ec6 2542 (when (file-exists-p linkname)
c62c9d08
KG
2543 ;; What to do?
2544 (if (or (null ok-if-already-exists) ; not allowed to exist
2545 (and (numberp ok-if-already-exists)
2546 (not (yes-or-no-p
2547 (format
2548 "File %s already exists; make it a link anyway? "
7432277c 2549 l-localname)))))
00d6fd04
MA
2550 (tramp-error
2551 l 'file-already-exists "File %s already exists" l-localname)
eba082a2 2552 (delete-file linkname)))
cebb4ec6 2553
7432277c 2554 ;; If FILENAME is a Tramp name, use just the localname component.
cebb4ec6 2555 (when (tramp-tramp-file-p filename)
1834b39f
MA
2556 (setq filename
2557 (tramp-file-name-localname
2558 (tramp-dissect-file-name (expand-file-name filename)))))
bf247b6e 2559
c62c9d08
KG
2560 ;; Right, they are on the same host, regardless of user, method, etc.
2561 ;; We now make the link on the remote machine. This will occur as the user
2562 ;; that FILENAME belongs to.
2563 (zerop
2564 (tramp-send-command-and-check
b593f105
MA
2565 l
2566 (format
2567 "cd %s && %s -sf %s %s"
2568 (tramp-shell-quote-argument cwd)
2569 ln
2570 (tramp-shell-quote-argument filename)
2571 (tramp-shell-quote-argument l-localname))
2572 t)))))
fb7933a3 2573
fb7933a3 2574(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
00d6fd04
MA
2575 "Like `load' for Tramp files."
2576 (with-parsed-tramp-file-name (expand-file-name file) nil
c62c9d08
KG
2577 (unless nosuffix
2578 (cond ((file-exists-p (concat file ".elc"))
2579 (setq file (concat file ".elc")))
2580 ((file-exists-p (concat file ".el"))
2581 (setq file (concat file ".el")))))
2582 (when must-suffix
2583 ;; The first condition is always true for absolute file names.
2584 ;; Included for safety's sake.
2585 (unless (or (file-name-directory file)
2586 (string-match "\\.elc?\\'" file))
00d6fd04
MA
2587 (tramp-error
2588 v 'file-error
2589 "File `%s' does not include a `.el' or `.elc' suffix" file)))
c62c9d08
KG
2590 (unless noerror
2591 (when (not (file-exists-p file))
00d6fd04 2592 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
c62c9d08
KG
2593 (if (not (file-exists-p file))
2594 nil
3b30ccda
MA
2595 (let ((tramp-message-show-message (not nomessage)))
2596 (with-progress-reporter v 0 (format "Loading %s" file)
2597 (let ((local-copy (file-local-copy file)))
2598 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2599 (unwind-protect
2600 (load local-copy noerror t t)
eba082a2 2601 (delete-file local-copy)))))
c62c9d08 2602 t)))
fb7933a3 2603
a4aeb9a4 2604;; Localname manipulation functions that grok Tramp localnames...
c0fc6170
MA
2605(defun tramp-handle-file-name-as-directory (file)
2606 "Like `file-name-as-directory' but aware of Tramp files."
2607 ;; `file-name-as-directory' would be sufficient except localname is
2608 ;; the empty string.
2609 (let ((v (tramp-dissect-file-name file t)))
2610 ;; Run the command on the localname portion only.
2611 (tramp-make-tramp-file-name
2612 (tramp-file-name-method v)
2613 (tramp-file-name-user v)
2614 (tramp-file-name-host v)
2615 (tramp-run-real-handler
2616 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2617
fb7933a3 2618(defun tramp-handle-file-name-directory (file)
00d6fd04 2619 "Like `file-name-directory' but aware of Tramp files."
9ce8462a
MA
2620 ;; Everything except the last filename thing is the directory. We
2621 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2622 ;; the remote file name parts. This is a problem when we are in
2623 ;; file name completion.
2624 (let ((v (tramp-dissect-file-name file t)))
a01b1e22
MA
2625 ;; Run the command on the localname portion only.
2626 (tramp-make-tramp-file-name
9ce8462a
MA
2627 (tramp-file-name-method v)
2628 (tramp-file-name-user v)
2629 (tramp-file-name-host v)
87bdd2c7
MA
2630 (tramp-run-real-handler
2631 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
fb7933a3
KG
2632
2633(defun tramp-handle-file-name-nondirectory (file)
00d6fd04 2634 "Like `file-name-nondirectory' but aware of Tramp files."
c62c9d08 2635 (with-parsed-tramp-file-name file nil
87bdd2c7 2636 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
fb7933a3
KG
2637
2638(defun tramp-handle-file-truename (filename &optional counter prev-dirs)
00d6fd04 2639 "Like `file-truename' for Tramp files."
48ddd622 2640 (with-parsed-tramp-file-name (expand-file-name filename) nil
00d6fd04 2641 (with-file-property v localname "file-truename"
293c24f9 2642 (let ((result nil)) ; result steps in reverse order
00d6fd04 2643 (tramp-message v 4 "Finding true name for `%s'" filename)
293c24f9
MA
2644 (cond
2645 ;; Use GNU readlink --canonicalize-missing where available.
2646 ((tramp-get-remote-readlink v)
2647 (setq result
2648 (tramp-send-command-and-read
2649 v
2650 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2651 (tramp-get-remote-readlink v)
2652 (tramp-shell-quote-argument localname)))))
2653
2654 ;; Use Perl implementation.
2655 ((and (tramp-get-remote-perl v)
2656 (tramp-get-connection-property v "perl-file-spec" nil)
2657 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2658 (tramp-maybe-send-script
2659 v tramp-perl-file-truename "tramp_perl_file_truename")
2660 (setq result
2661 (tramp-send-command-and-read
2662 v
2663 (format "tramp_perl_file_truename %s"
2664 (tramp-shell-quote-argument localname)))))
2665
2666 ;; Do it yourself. We bind `directory-sep-char' here for
2667 ;; XEmacs on Windows, which would otherwise use backslash.
2668 (t (let* ((directory-sep-char ?/)
2669 (steps (tramp-compat-split-string localname "/"))
2670 (localnamedir (tramp-run-real-handler
2671 'file-name-as-directory (list localname)))
2672 (is-dir (string= localname localnamedir))
2673 (thisstep nil)
2674 (numchase 0)
2675 ;; Don't make the following value larger than
2676 ;; necessary. People expect an error message in a
2677 ;; timely fashion when something is wrong;
2678 ;; otherwise they might think that Emacs is hung.
2679 ;; Of course, correctness has to come first.
2680 (numchase-limit 20)
2681 symlink-target)
2682 (while (and steps (< numchase numchase-limit))
2683 (setq thisstep (pop steps))
2684 (tramp-message
2685 v 5 "Check %s"
2686 (mapconcat 'identity
2687 (append '("") (reverse result) (list thisstep))
2688 "/"))
2689 (setq symlink-target
2690 (nth 0 (file-attributes
2691 (tramp-make-tramp-file-name
2692 method user host
2693 (mapconcat 'identity
2694 (append '("")
2695 (reverse result)
2696 (list thisstep))
2697 "/")))))
2698 (cond ((string= "." thisstep)
2699 (tramp-message v 5 "Ignoring step `.'"))
2700 ((string= ".." thisstep)
2701 (tramp-message v 5 "Processing step `..'")
2702 (pop result))
2703 ((stringp symlink-target)
2704 ;; It's a symlink, follow it.
2705 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2706 (setq numchase (1+ numchase))
2707 (when (file-name-absolute-p symlink-target)
2708 (setq result nil))
2709 ;; If the symlink was absolute, we'll get a string like
2710 ;; "/user@host:/some/target"; extract the
2711 ;; "/some/target" part from it.
2712 (when (tramp-tramp-file-p symlink-target)
2713 (unless (tramp-equal-remote filename symlink-target)
2714 (tramp-error
2715 v 'file-error
2716 "Symlink target `%s' on wrong host" symlink-target))
2717 (setq symlink-target localname))
2718 (setq steps
2719 (append (tramp-compat-split-string
2720 symlink-target "/")
2721 steps)))
2722 (t
2723 ;; It's a file.
2724 (setq result (cons thisstep result)))))
2725 (when (>= numchase numchase-limit)
2726 (tramp-error
2727 v 'file-error
2728 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2729 (setq result (reverse result))
2730 ;; Combine list to form string.
2731 (setq result
2732 (if result
2733 (mapconcat 'identity (cons "" result) "/")
00d6fd04 2734 "/"))
293c24f9
MA
2735 (when (and is-dir (or (string= "" result)
2736 (not (string= (substring result -1) "/"))))
2737 (setq result (concat result "/"))))))
2738
2739 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2740 (tramp-make-tramp-file-name method user host result)))))
fb7933a3
KG
2741
2742;; Basic functions.
2743
2744(defun tramp-handle-file-exists-p (filename)
00d6fd04 2745 "Like `file-exists-p' for Tramp files."
c62c9d08 2746 (with-parsed-tramp-file-name filename nil
00d6fd04 2747 (with-file-property v localname "file-exists-p"
293c24f9
MA
2748 (or (not (null (tramp-get-file-property
2749 v localname "file-attributes-integer" nil)))
2750 (not (null (tramp-get-file-property
2751 v localname "file-attributes-string" nil)))
2752 (zerop (tramp-send-command-and-check
2753 v
2754 (format
2755 "%s %s"
2756 (tramp-get-file-exists-command v)
2757 (tramp-shell-quote-argument localname))))))))
fb7933a3 2758
00d6fd04
MA
2759;; Inodes don't exist for some file systems. Therefore we must
2760;; generate virtual ones. Used in `find-buffer-visiting'. The method
2761;; applied might be not so efficient (Ange-FTP uses hashes). But
2762;; performance isn't the major issue given that file transfer will
2763;; take time.
2764(defvar tramp-inodes nil
2765 "Keeps virtual inodes numbers.")
2766
8daea7fc
KG
2767;; Devices must distinguish physical file systems. The device numbers
2768;; provided by "lstat" aren't unique, because we operate on different hosts.
2769;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2770;; EFS use device number "-1". In order to be different, we use device number
b946a456 2771;; (-1 . x), whereby "x" is unique for a given (method user host).
8daea7fc
KG
2772(defvar tramp-devices nil
2773 "Keeps virtual device numbers.")
2774
fb7933a3
KG
2775;; CCC: This should check for an error condition and signal failure
2776;; when something goes wrong.
2777;; Daniel Pittman <daniel@danann.net>
c951aecb 2778(defun tramp-handle-file-attributes (filename &optional id-format)
00d6fd04
MA
2779 "Like `file-attributes' for Tramp files."
2780 (unless id-format (setq id-format 'integer))
aa485f7c
MA
2781 ;; Don't modify `last-coding-system-used' by accident.
2782 (let ((last-coding-system-used last-coding-system-used))
2783 (with-parsed-tramp-file-name (expand-file-name filename) nil
2784 (with-file-property v localname (format "file-attributes-%s" id-format)
7f49fe46
MA
2785 (save-excursion
2786 (tramp-convert-file-attributes
2787 v
2788 (cond
2789 ((tramp-get-remote-stat v)
2790 (tramp-do-file-attributes-with-stat v localname id-format))
2791 ((tramp-get-remote-perl v)
2792 (tramp-do-file-attributes-with-perl v localname id-format))
2793 (t
2794 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2795
2796(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
00d6fd04 2797 "Implement `file-attributes' for Tramp files using the ls(1) command."
fb7933a3
KG
2798 (let (symlinkp dirp
2799 res-inode res-filemodes res-numlinks
2800 res-uid res-gid res-size res-symlink-target)
00d6fd04 2801 (tramp-message vec 5 "file attributes with ls: %s" localname)
fb7933a3 2802 (tramp-send-command
00d6fd04 2803 vec
680db9ac
MA
2804 (format "(%s %s || %s -h %s) && %s %s %s"
2805 (tramp-get-file-exists-command vec)
2806 (tramp-shell-quote-argument localname)
2807 (tramp-get-test-command vec)
2808 (tramp-shell-quote-argument localname)
00d6fd04 2809 (tramp-get-ls-command vec)
c82c5727 2810 (if (eq id-format 'integer) "-ildn" "-ild")
7432277c 2811 (tramp-shell-quote-argument localname)))
fb7933a3 2812 ;; parse `ls -l' output ...
00d6fd04 2813 (with-current-buffer (tramp-get-buffer vec)
680db9ac
MA
2814 (when (> (buffer-size) 0)
2815 (goto-char (point-min))
2816 ;; ... inode
2817 (setq res-inode
2818 (condition-case err
2819 (read (current-buffer))
2820 (invalid-read-syntax
2821 (when (and (equal (cadr err)
2822 "Integer constant overflow in reader")
2823 (string-match
2824 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2825 (car (cddr err))))
2826 (let* ((big (read (substring (car (cddr err)) 0
2827 (match-beginning 1))))
2828 (small (read (match-string 1 (car (cddr err)))))
2829 (twiddle (/ small 65536)))
2830 (cons (+ big twiddle)
2831 (- small (* twiddle 65536))))))))
2832 ;; ... file mode flags
2833 (setq res-filemodes (symbol-name (read (current-buffer))))
2834 ;; ... number links
2835 (setq res-numlinks (read (current-buffer)))
2836 ;; ... uid and gid
2837 (setq res-uid (read (current-buffer)))
2838 (setq res-gid (read (current-buffer)))
2839 (if (eq id-format 'integer)
2840 (progn
2841 (unless (numberp res-uid) (setq res-uid -1))
2842 (unless (numberp res-gid) (setq res-gid -1)))
2843 (progn
2844 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
2845 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
2846 ;; ... size
2847 (setq res-size (read (current-buffer)))
2848 ;; From the file modes, figure out other stuff.
2849 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2850 (setq dirp (eq ?d (aref res-filemodes 0)))
2851 ;; if symlink, find out file name pointed to
2852 (when symlinkp
2853 (search-forward "-> ")
2854 (setq res-symlink-target
2855 (buffer-substring (point) (tramp-compat-line-end-position))))
2856 ;; return data gathered
2857 (list
2858 ;; 0. t for directory, string (name linked to) for symbolic
2859 ;; link, or nil.
2860 (or dirp res-symlink-target)
2861 ;; 1. Number of links to file.
2862 res-numlinks
2863 ;; 2. File uid.
2864 res-uid
2865 ;; 3. File gid.
2866 res-gid
2867 ;; 4. Last access time, as a list of two integers. First
2868 ;; integer has high-order 16 bits of time, second has low 16
2869 ;; bits.
2870 ;; 5. Last modification time, likewise.
2871 ;; 6. Last status change time, likewise.
2872 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2873 ;; 7. Size in bytes (-1, if number is out of range).
2874 res-size
2875 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2876 res-filemodes
2877 ;; 9. t if file's gid would change if file were deleted and
2878 ;; recreated. Will be set in `tramp-convert-file-attributes'
2879 t
2880 ;; 10. inode number.
2881 res-inode
2882 ;; 11. Device number. Will be replaced by a virtual device number.
2883 -1
2884 )))))
fb7933a3 2885
7f49fe46 2886(defun tramp-do-file-attributes-with-perl
00d6fd04
MA
2887 (vec localname &optional id-format)
2888 "Implement `file-attributes' for Tramp files using a Perl script."
2889 (tramp-message vec 5 "file attributes with perl: %s" localname)
2890 (tramp-maybe-send-script
2891 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
2892 (tramp-send-command-and-read
2893 vec
2894 (format "tramp_perl_file_attributes %s %s"
2895 (tramp-shell-quote-argument localname) id-format)))
2896
7f49fe46 2897(defun tramp-do-file-attributes-with-stat
00d6fd04
MA
2898 (vec localname &optional id-format)
2899 "Implement `file-attributes' for Tramp files using stat(1) command."
2900 (tramp-message vec 5 "file attributes with stat: %s" localname)
2901 (tramp-send-command-and-read
2902 vec
2903 (format
94ecf5da
MA
2904 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
2905 ;; parse correctly the sequence "((". Therefore, we add a space.
2906 "( (%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)"
680db9ac
MA
2907 (tramp-get-file-exists-command vec)
2908 (tramp-shell-quote-argument localname)
2909 (tramp-get-test-command vec)
2910 (tramp-shell-quote-argument localname)
00d6fd04
MA
2911 (tramp-get-remote-stat vec)
2912 (if (eq id-format 'integer) "%u" "\"%U\"")
2913 (if (eq id-format 'integer) "%g" "\"%G\"")
2914 (tramp-shell-quote-argument localname))))
8daea7fc 2915
fb7933a3 2916(defun tramp-handle-set-visited-file-modtime (&optional time-list)
00d6fd04 2917 "Like `set-visited-file-modtime' for Tramp files."
fb7933a3
KG
2918 (unless (buffer-file-name)
2919 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2920 (buffer-name)))
48ddd622
MA
2921 (if time-list
2922 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
11948172
MA
2923 (let ((f (buffer-file-name))
2924 coding-system-used)
48ddd622
MA
2925 (with-parsed-tramp-file-name f nil
2926 (let* ((attr (file-attributes f))
2927 ;; '(-1 65535) means file doesn't exists yet.
2928 (modtime (or (nth 5 attr) '(-1 65535))))
11948172
MA
2929 (when (boundp 'last-coding-system-used)
2930 (setq coding-system-used (symbol-value 'last-coding-system-used)))
48ddd622 2931 ;; We use '(0 0) as a don't-know value. See also
7f49fe46 2932 ;; `tramp-do-file-attributes-with-ls'.
48ddd622
MA
2933 (if (not (equal modtime '(0 0)))
2934 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
00d6fd04 2935 (progn
48ddd622 2936 (tramp-send-command
00d6fd04 2937 v
48ddd622 2938 (format "%s -ild %s"
00d6fd04 2939 (tramp-get-ls-command v)
48ddd622 2940 (tramp-shell-quote-argument localname)))
48ddd622
MA
2941 (setq attr (buffer-substring (point)
2942 (progn (end-of-line) (point)))))
00d6fd04
MA
2943 (tramp-set-file-property
2944 v localname "visited-file-modtime-ild" attr))
11948172
MA
2945 (when (boundp 'last-coding-system-used)
2946 (set 'last-coding-system-used coding-system-used))
d2a2c17f 2947 nil)))))
fb7933a3 2948
c62c9d08
KG
2949;; This function makes the same assumption as
2950;; `tramp-handle-set-visited-file-modtime'.
2951(defun tramp-handle-verify-visited-file-modtime (buf)
00d6fd04 2952 "Like `verify-visited-file-modtime' for Tramp files.
c08e6004
MA
2953At the time `verify-visited-file-modtime' calls this function, we
2954already know that the buffer is visiting a file and that
2955`visited-file-modtime' does not return 0. Do not call this
2956function directly, unless those two cases are already taken care
2957of."
c62c9d08 2958 (with-current-buffer buf
94ecf5da
MA
2959 (let ((f (buffer-file-name)))
2960 ;; There is no file visiting the buffer, or the buffer has no
2961 ;; recorded last modification time, or there is no established
2962 ;; connection.
2963 (if (or (not f)
2964 (eq (visited-file-modtime) 0)
2965 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
2966 t
b15d0c4c 2967 (with-parsed-tramp-file-name f nil
bce04fee 2968 (tramp-flush-file-property v localname)
b15d0c4c
MA
2969 (let* ((attr (file-attributes f))
2970 (modtime (nth 5 attr))
2971 (mt (visited-file-modtime)))
bf247b6e 2972
70c11b0b
MA
2973 (cond
2974 ;; File exists, and has a known modtime.
b15d0c4c
MA
2975 ((and attr (not (equal modtime '(0 0))))
2976 (< (abs (tramp-time-diff
2977 modtime
2978 ;; For compatibility, deal with both the old
70c11b0b
MA
2979 ;; (HIGH . LOW) and the new (HIGH LOW) return
2980 ;; values of `visited-file-modtime'.
b15d0c4c
MA
2981 (if (atom (cdr mt))
2982 (list (car mt) (cdr mt))
2983 mt)))
2984 2))
70c11b0b 2985 ;; Modtime has the don't know value.
b15d0c4c 2986 (attr
00d6fd04
MA
2987 (tramp-send-command
2988 v
2989 (format "%s -ild %s"
2990 (tramp-get-ls-command v)
2991 (tramp-shell-quote-argument localname)))
2992 (with-current-buffer (tramp-get-buffer v)
b15d0c4c
MA
2993 (setq attr (buffer-substring
2994 (point) (progn (end-of-line) (point)))))
00d6fd04
MA
2995 (equal
2996 attr
2997 (tramp-get-file-property
2998 v localname "visited-file-modtime-ild" "")))
70c11b0b
MA
2999 ;; If file does not exist, say it is not modified if and
3000 ;; only if that agrees with the buffer's record.
b15d0c4c 3001 (t (equal mt '(-1 65535))))))))))
c62c9d08 3002
fb7933a3 3003(defun tramp-handle-set-file-modes (filename mode)
00d6fd04 3004 "Like `set-file-modes' for Tramp files."
c62c9d08 3005 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3006 (tramp-flush-file-property v localname)
3007 (unless (zerop (tramp-send-command-and-check
3008 v
3009 (format "chmod %s %s"
3010 (tramp-decimal-to-octal mode)
3011 (tramp-shell-quote-argument localname))))
3012 ;; FIXME: extract the proper text from chmod's stderr.
3013 (tramp-error
3014 v 'file-error "Error while changing file's mode %s" filename))))
fb7933a3 3015
ce3f516f
MA
3016(defun tramp-handle-set-file-times (filename &optional time)
3017 "Like `set-file-times' for Tramp files."
3018 (zerop
9e6ab520 3019 (if (file-remote-p filename)
ce3f516f 3020 (with-parsed-tramp-file-name filename nil
8d60099b 3021 (tramp-flush-file-property v localname)
ce3f516f
MA
3022 (let ((time (if (or (null time) (equal time '(0 0)))
3023 (current-time)
3024 time))
94ecf5da
MA
3025 ;; With GNU Emacs, `format-time-string' has an optional
3026 ;; parameter UNIVERSAL. This is preferred, because we
3027 ;; could handle the case when the remote host is
3028 ;; located in a different time zone as the local host.
3029 (utc (not (featurep 'xemacs))))
ce3f516f
MA
3030 (tramp-send-command-and-check
3031 v (format "%s touch -t %s %s"
3032 (if utc "TZ=UTC; export TZ;" "")
3033 (if utc
3034 (format-time-string "%Y%m%d%H%M.%S" time t)
3035 (format-time-string "%Y%m%d%H%M.%S" time))
3036 (tramp-shell-quote-argument localname)))))
8d60099b 3037
ce3f516f
MA
3038 ;; We handle also the local part, because in older Emacsen,
3039 ;; without `set-file-times', this function is an alias for this.
3040 ;; We are local, so we don't need the UTC settings.
a4aeb9a4 3041 (tramp-local-call-process
ce3f516f
MA
3042 "touch" nil nil nil "-t"
3043 (format-time-string "%Y%m%d%H%M.%S" time)
3044 (tramp-shell-quote-argument filename)))))
3045
8d60099b
MA
3046(defun tramp-set-file-uid-gid (filename &optional uid gid)
3047 "Set the ownership for FILENAME.
3048If UID and GID are provided, these values are used; otherwise uid
3049and gid of the corresponding user is taken. Both parameters must be integers."
70c11b0b
MA
3050 ;; Modern Unices allow chown only for root. So we might need
3051 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3052 ;; working with su(do)? when it is needed, so it shall succeed in
3053 ;; the majority of cases.
aa485f7c
MA
3054 ;; Don't modify `last-coding-system-used' by accident.
3055 (let ((last-coding-system-used last-coding-system-used))
3056 (if (file-remote-p filename)
3057 (with-parsed-tramp-file-name filename nil
3058 (if (and (zerop (user-uid)) (tramp-local-host-p v))
3059 ;; If we are root on the local host, we can do it directly.
3060 (tramp-set-file-uid-gid localname uid gid)
3061 (let ((uid (or (and (integerp uid) uid)
3062 (tramp-get-remote-uid v 'integer)))
3063 (gid (or (and (integerp gid) gid)
3064 (tramp-get-remote-gid v 'integer))))
3065 (tramp-send-command
3066 v (format
3067 "chown %d:%d %s" uid gid
3068 (tramp-shell-quote-argument localname))))))
3069
3070 ;; We handle also the local part, because there doesn't exist
3071 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3072 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
3073 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
3074 (tramp-local-call-process
3075 "chown" nil nil nil
3076 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
8d60099b 3077
7ba94701
MA
3078(defun tramp-remote-selinux-p (vec)
3079 "Check, whether SELINUX is enabled on the remote host."
3080 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
3081 (let ((result (tramp-find-executable
3082 vec "getenforce" (tramp-get-remote-path vec) t t)))
3083 (and result
3084 (string-equal
3085 (tramp-send-command-and-read
3086 vec (format "echo \\\"`%S`\\\"" result))
3087 "Enforcing")))))
3088
632c5478
MA
3089(defun tramp-handle-file-selinux-context (filename)
3090 "Like `file-selinux-context' for Tramp files."
3091 (with-parsed-tramp-file-name filename nil
3092 (with-file-property v localname "file-selinux-context"
3093 (let ((context '(nil nil nil nil))
3094 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3095 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
7ba94701
MA
3096 (when (and (tramp-remote-selinux-p v)
3097 (zerop (tramp-send-command-and-check
3098 v (format
3099 "%s -d -Z %s"
3100 (tramp-get-ls-command v)
3101 (tramp-shell-quote-argument localname)))))
632c5478
MA
3102 (with-current-buffer (tramp-get-connection-buffer v)
3103 (goto-char (point-min))
3104 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
3105 (setq context (list (match-string 1) (match-string 2)
3106 (match-string 3) (match-string 4))))))
3107 ;; Return the context.
3108 context))))
3109
3110(defun tramp-handle-set-file-selinux-context (filename context)
3111 "Like `set-file-selinux-context' for Tramp files."
3112 (with-parsed-tramp-file-name filename nil
3113 (if (and (consp context)
7ba94701 3114 (tramp-remote-selinux-p v)
632c5478
MA
3115 (zerop (tramp-send-command-and-check
3116 v (format "chcon %s %s %s %s %s"
3117 (if (stringp (nth 0 context))
3118 (format "--user=%s" (nth 0 context)) "")
3119 (if (stringp (nth 1 context))
3120 (format "--role=%s" (nth 1 context)) "")
3121 (if (stringp (nth 2 context))
3122 (format "--type=%s" (nth 2 context)) "")
3123 (if (stringp (nth 3 context))
3124 (format "--range=%s" (nth 3 context)) "")
3125 (tramp-shell-quote-argument localname)))))
3126 (tramp-set-file-property v localname "file-selinux-context" context)
3127 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
3128 ;; We always return nil.
3129 nil)
3130
fb7933a3
KG
3131;; Simple functions using the `test' command.
3132
3133(defun tramp-handle-file-executable-p (filename)
00d6fd04 3134 "Like `file-executable-p' for Tramp files."
c62c9d08 3135 (with-parsed-tramp-file-name filename nil
00d6fd04 3136 (with-file-property v localname "file-executable-p"
293c24f9
MA
3137 ;; Examine `file-attributes' cache to see if request can be
3138 ;; satisfied without remote operation.
3139 (or (tramp-check-cached-permissions v ?x)
3140 (zerop (tramp-run-test "-x" filename))))))
fb7933a3
KG
3141
3142(defun tramp-handle-file-readable-p (filename)
00d6fd04 3143 "Like `file-readable-p' for Tramp files."
c62c9d08 3144 (with-parsed-tramp-file-name filename nil
00d6fd04 3145 (with-file-property v localname "file-readable-p"
293c24f9
MA
3146 ;; Examine `file-attributes' cache to see if request can be
3147 ;; satisfied without remote operation.
3148 (or (tramp-check-cached-permissions v ?r)
3149 (zerop (tramp-run-test "-r" filename))))))
fb7933a3
KG
3150
3151;; When the remote shell is started, it looks for a shell which groks
3152;; tilde expansion. Here, we assume that all shells which grok tilde
3153;; expansion will also provide a `test' command which groks `-nt' (for
3154;; newer than). If this breaks, tell me about it and I'll try to do
3155;; something smarter about it.
3156(defun tramp-handle-file-newer-than-file-p (file1 file2)
00d6fd04 3157 "Like `file-newer-than-file-p' for Tramp files."
fb7933a3
KG
3158 (cond ((not (file-exists-p file1))
3159 nil)
3160 ((not (file-exists-p file2))
3161 t)
91879624 3162 ;; We are sure both files exist at this point.
fb7933a3
KG
3163 (t
3164 (save-excursion
91879624
KG
3165 ;; We try to get the mtime of both files. If they are not
3166 ;; equal to the "dont-know" value, then we subtract the times
3167 ;; and obtain the result.
3168 (let ((fa1 (file-attributes file1))
3169 (fa2 (file-attributes file2)))
3170 (if (and (not (equal (nth 5 fa1) '(0 0)))
3171 (not (equal (nth 5 fa2) '(0 0))))
01917a18 3172 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
91879624
KG
3173 ;; If one of them is the dont-know value, then we can
3174 ;; still try to run a shell command on the remote host.
3175 ;; However, this only works if both files are Tramp
3176 ;; files and both have the same method, same user, same
3177 ;; host.
00d6fd04
MA
3178 (unless (tramp-equal-remote file1 file2)
3179 (with-parsed-tramp-file-name
3180 (if (tramp-tramp-file-p file1) file1 file2) nil
3181 (tramp-error
3182 v 'file-error
3183 "Files %s and %s must have same method, user, host"
3184 file1 file2)))
3185 (with-parsed-tramp-file-name file1 nil
3186 (zerop (tramp-run-test2
3187 (tramp-get-test-nt-command v) file1 file2)))))))))
fb7933a3
KG
3188
3189;; Functions implemented using the basic functions above.
3190
3191(defun tramp-handle-file-modes (filename)
00d6fd04 3192 "Like `file-modes' for Tramp files."
5da24108
MA
3193 (let ((truename (or (file-truename filename) filename)))
3194 (when (file-exists-p truename)
3195 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
fb7933a3 3196
b86c1cd8
MA
3197(defun tramp-default-file-modes (filename)
3198 "Return file modes of FILENAME as integer.
3199If the file modes of FILENAME cannot be determined, return the
974647ac
MA
3200value of `default-file-modes', without execute permissions."
3201 (or (file-modes filename)
3202 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
b86c1cd8 3203
fb7933a3 3204(defun tramp-handle-file-directory-p (filename)
00d6fd04 3205 "Like `file-directory-p' for Tramp files."
fb7933a3
KG
3206 ;; Care must be taken that this function returns `t' for symlinks
3207 ;; pointing to directories. Surely the most obvious implementation
3208 ;; would be `test -d', but that returns false for such symlinks.
3209 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3210 ;; I now think he's right. So we could be using `test -d', couldn't
3211 ;; we?
3212 ;;
3213 ;; Alternatives: `cd %s', `test -d %s'
c62c9d08 3214 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3215 (with-file-property v localname "file-directory-p"
3216 (zerop (tramp-run-test "-d" filename)))))
fb7933a3
KG
3217
3218(defun tramp-handle-file-regular-p (filename)
00d6fd04
MA
3219 "Like `file-regular-p' for Tramp files."
3220 (and (file-exists-p filename)
3221 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
fb7933a3
KG
3222
3223(defun tramp-handle-file-symlink-p (filename)
00d6fd04 3224 "Like `file-symlink-p' for Tramp files."
c62c9d08 3225 (with-parsed-tramp-file-name filename nil
c951aecb 3226 (let ((x (car (file-attributes filename))))
b25a52cc
KG
3227 (when (stringp x)
3228 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3229 ;; might do weird things.
3230 (if (file-name-absolute-p x)
00d6fd04 3231 (tramp-make-tramp-file-name method user host x)
b25a52cc 3232 x)))))
fb7933a3
KG
3233
3234(defun tramp-handle-file-writable-p (filename)
00d6fd04 3235 "Like `file-writable-p' for Tramp files."
c62c9d08 3236 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3237 (with-file-property v localname "file-writable-p"
3238 (if (file-exists-p filename)
293c24f9
MA
3239 ;; Examine `file-attributes' cache to see if request can be
3240 ;; satisfied without remote operation.
3241 (or (tramp-check-cached-permissions v ?w)
3242 (zerop (tramp-run-test "-w" filename)))
00d6fd04
MA
3243 ;; If file doesn't exist, check if directory is writable.
3244 (and (zerop (tramp-run-test
3245 "-d" (file-name-directory filename)))
3246 (zerop (tramp-run-test
3247 "-w" (file-name-directory filename))))))))
fb7933a3
KG
3248
3249(defun tramp-handle-file-ownership-preserved-p (filename)
00d6fd04 3250 "Like `file-ownership-preserved-p' for Tramp files."
c62c9d08 3251 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3252 (with-file-property v localname "file-ownership-preserved-p"
3253 (let ((attributes (file-attributes filename)))
3254 ;; Return t if the file doesn't exist, since it's true that no
3255 ;; information would be lost by an (attempted) delete and create.
3256 (or (null attributes)
3257 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
fb7933a3
KG
3258
3259;; Other file name ops.
3260
fb7933a3 3261(defun tramp-handle-directory-file-name (directory)
00d6fd04 3262 "Like `directory-file-name' for Tramp files."
7432277c
KG
3263 ;; If localname component of filename is "/", leave it unchanged.
3264 ;; Otherwise, remove any trailing slash from localname component.
8daea7fc
KG
3265 ;; Method, host, etc, are unchanged. Does it make sense to try
3266 ;; to avoid parsing the filename?
c62c9d08 3267 (with-parsed-tramp-file-name directory nil
7432277c
KG
3268 (if (and (not (zerop (length localname)))
3269 (eq (aref localname (1- (length localname))) ?/)
3270 (not (string= localname "/")))
8daea7fc
KG
3271 (substring directory 0 -1)
3272 directory)))
fb7933a3
KG
3273
3274;; Directory listings.
3275
00d6fd04
MA
3276(defun tramp-handle-directory-files
3277 (directory &optional full match nosort files-only)
3278 "Like `directory-files' for Tramp files."
3279 ;; FILES-ONLY is valid for XEmacs only.
3280 (when (file-directory-p directory)
73a37a69 3281 (setq directory (file-name-as-directory (expand-file-name directory)))
00d6fd04
MA
3282 (let ((temp (nreverse (file-name-all-completions "" directory)))
3283 result item)
3284
3285 (while temp
3286 (setq item (directory-file-name (pop temp)))
3287 (when (and (or (null match) (string-match match item))
3288 (or (null files-only)
73a37a69 3289 ;; Files only.
00d6fd04 3290 (and (equal files-only t) (file-regular-p item))
73a37a69 3291 ;; Directories only.
00d6fd04 3292 (file-directory-p item)))
73a37a69 3293 (push (if full (concat directory item) item)
00d6fd04 3294 result)))
73a37a69 3295 (if nosort result (sort result 'string<)))))
c62c9d08 3296
c82c5727
LH
3297(defun tramp-handle-directory-files-and-attributes
3298 (directory &optional full match nosort id-format)
00d6fd04
MA
3299 "Like `directory-files-and-attributes' for Tramp files."
3300 (unless id-format (setq id-format 'integer))
3301 (when (file-directory-p directory)
3302 (setq directory (expand-file-name directory))
3303 (let* ((temp
b533bc97 3304 (copy-tree
00d6fd04
MA
3305 (with-parsed-tramp-file-name directory nil
3306 (with-file-property
3307 v localname
3308 (format "directory-files-and-attributes-%s" id-format)
3309 (save-excursion
3310 (mapcar
aa485f7c
MA
3311 (lambda (x)
3312 (cons (car x)
3313 (tramp-convert-file-attributes v (cdr x))))
7f49fe46
MA
3314 (cond
3315 ((tramp-get-remote-stat v)
3316 (tramp-do-directory-files-and-attributes-with-stat
3317 v localname id-format))
3318 ((tramp-get-remote-perl v)
3319 (tramp-do-directory-files-and-attributes-with-perl
3320 v localname id-format)))))))))
00d6fd04
MA
3321 result item)
3322
3323 (while temp
3324 (setq item (pop temp))
3325 (when (or (null match) (string-match match (car item)))
3326 (when full
3327 (setcar item (expand-file-name (car item) directory)))
3328 (push item result)))
3329
3330 (if nosort
3331 result
3332 (sort result (lambda (x y) (string< (car x) (car y))))))))
3333
7f49fe46 3334(defun tramp-do-directory-files-and-attributes-with-perl
00d6fd04
MA
3335 (vec localname &optional id-format)
3336 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3337 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3338 (tramp-maybe-send-script
3339 vec tramp-perl-directory-files-and-attributes
3340 "tramp_perl_directory_files_and_attributes")
3341 (let ((object
3342 (tramp-send-command-and-read
3343 vec
3344 (format "tramp_perl_directory_files_and_attributes %s %s"
3345 (tramp-shell-quote-argument localname) id-format))))
3346 (when (stringp object) (tramp-error vec 'file-error object))
3347 object))
3348
7f49fe46 3349(defun tramp-do-directory-files-and-attributes-with-stat
00d6fd04
MA
3350 (vec localname &optional id-format)
3351 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3352 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3353 (tramp-send-command-and-read
3354 vec
3355 (format
3356 (concat
70c11b0b
MA
3357 ;; We must care about filenames with spaces, or starting with
3358 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3359 ;; but it does not work on all remote systems. Therefore, we
3360 ;; quote the filenames via sed.
3361 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
d4443a0d 3362 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
00d6fd04
MA
3363 "echo \")\"")
3364 (tramp-shell-quote-argument localname)
3365 (tramp-get-ls-command vec)
3366 (tramp-get-remote-stat vec)
3367 (if (eq id-format 'integer) "%u" "\"%U\"")
3368 (if (eq id-format 'integer) "%g" "\"%G\""))))
c82c5727 3369
c62c9d08 3370;; This function should return "foo/" for directories and "bar" for
00d6fd04 3371;; files.
c62c9d08 3372(defun tramp-handle-file-name-all-completions (filename directory)
00d6fd04
MA
3373 "Like `file-name-all-completions' for Tramp files."
3374 (unless (save-match-data (string-match "/" filename))
9c13938d 3375 (with-parsed-tramp-file-name (expand-file-name directory) nil
b50dd0d2 3376
00d6fd04
MA
3377 (all-completions
3378 filename
3379 (mapcar
3380 'list
293c24f9
MA
3381 (or
3382 ;; Try cache first
3383 (and
3384 ;; Ignore if expired
3385 (or (not (integerp tramp-completion-reread-directory-timeout))
3386 (<= (tramp-time-diff
3387 (current-time)
3388 (tramp-get-file-property
3389 v localname "last-completion" '(0 0 0)))
3390 tramp-completion-reread-directory-timeout))
3391
3392 ;; Try cache entries for filename, filename with last
3393 ;; character removed, filename with last two characters
3394 ;; removed, ..., and finally the empty string - all
3395 ;; concatenated to the local directory name
3396
3397 ;; This is inefficient for very long filenames, pity
3398 ;; `reduce' is not available...
3399 (car
3400 (apply
3401 'append
3402 (mapcar
3403 (lambda (x)
3404 (let ((cache-hit
3405 (tramp-get-file-property
3406 v
3407 (concat localname (substring filename 0 x))
3408 "file-name-all-completions"
3409 nil)))
3410 (when cache-hit (list cache-hit))))
3411 (tramp-compat-number-sequence (length filename) 0 -1)))))
3412
3413 ;; Cache expired or no matching cache entry found so we need
3414 ;; to perform a remote operation
3415 (let (result)
3416 ;; Get a list of directories and files, including reliably
3417 ;; tagging the directories with a trailing '/'. Because I
3418 ;; rock. --daniel@danann.net
3419
3420 ;; Changed to perform `cd' in the same remote op and only
3421 ;; get entries starting with `filename'. Capture any `cd'
3422 ;; error messages. Ensure any `cd' and `echo' aliases are
3423 ;; ignored.
3424 (tramp-send-command
3425 v
3426 (if (tramp-get-remote-perl v)
3427 (progn
3428 (tramp-maybe-send-script
3429 v tramp-perl-file-name-all-completions
3430 "tramp_perl_file_name_all_completions")
3431 (format "tramp_perl_file_name_all_completions %s %s %d"
3432 (tramp-shell-quote-argument localname)
3433 (tramp-shell-quote-argument filename)
3434 (if (symbol-value
b533bc97
MA
3435 ;; `read-file-name-completion-ignore-case'
3436 ;; is introduced with Emacs 22.1.
3437 (if (boundp
3438 'read-file-name-completion-ignore-case)
3439 'read-file-name-completion-ignore-case
3440 'completion-ignore-case))
293c24f9
MA
3441 1 0)))
3442
3443 (format (concat
3444 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3445 ;; `ls' with wildcard might fail with `Argument
3446 ;; list too long' error in some corner cases; if
3447 ;; `ls' fails after `cd' succeeded, chances are
3448 ;; that's the case, so let's retry without
3449 ;; wildcard. This will return "too many" entries
3450 ;; but that isn't harmful.
3451 " || %s -a 2>/dev/null)"
3452 " | while read f; do"
3453 " if %s -d \"$f\" 2>/dev/null;"
3454 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3455 " && \\echo ok) || \\echo fail")
3456 (tramp-shell-quote-argument localname)
3457 (tramp-get-ls-command v)
3458 ;; When `filename' is empty, just `ls' without
3459 ;; filename argument is more efficient than `ls *'
3460 ;; for very large directories and might avoid the
3461 ;; `Argument list too long' error.
3462 ;;
3463 ;; With and only with wildcard, we need to add
3464 ;; `-d' to prevent `ls' from descending into
3465 ;; sub-directories.
3466 (if (zerop (length filename))
3467 "."
3468 (concat (tramp-shell-quote-argument filename) "* -d"))
3469 (tramp-get-ls-command v)
3470 (tramp-get-test-command v))))
3471
3472 ;; Now grab the output.
3473 (with-current-buffer (tramp-get-buffer v)
3474 (goto-char (point-max))
3475
3476 ;; Check result code, found in last line of output
3477 (forward-line -1)
3478 (if (looking-at "^fail$")
3479 (progn
3480 ;; Grab error message from line before last line
3481 ;; (it was put there by `cd 2>&1')
3482 (forward-line -1)
3483 (tramp-error
3484 v 'file-error
3485 "tramp-handle-file-name-all-completions: %s"
3486 (buffer-substring
3487 (point) (tramp-compat-line-end-position))))
3488 ;; For peace of mind, if buffer doesn't end in `fail'
3489 ;; then it should end in `ok'. If neither are in the
3490 ;; buffer something went seriously wrong on the remote
3491 ;; side.
3492 (unless (looking-at "^ok$")
3493 (tramp-error
3494 v 'file-error
3495 "\
3496tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3497 (tramp-shell-quote-argument localname) (buffer-string))))
3498
3499 (while (zerop (forward-line -1))
3500 (push (buffer-substring
3501 (point) (tramp-compat-line-end-position))
3502 result)))
3503
3504 ;; Because the remote op went through OK we know the
3505 ;; directory we `cd'-ed to exists
3506 (tramp-set-file-property
3507 v localname "file-exists-p" t)
3508
3509 ;; Because the remote op went through OK we know every
3510 ;; file listed by `ls' exists.
3511 (mapc (lambda (entry)
3512 (tramp-set-file-property
3513 v (concat localname entry) "file-exists-p" t))
3514 result)
3515
3516 (tramp-set-file-property
3517 v localname "last-completion" (current-time))
3518
3519 ;; Store result in the cache
3520 (tramp-set-file-property
3521 v (concat localname filename)
3522 "file-name-all-completions"
3523 result))))))))
fb7933a3 3524
e1e17cae
MA
3525(defun tramp-handle-file-name-completion
3526 (filename directory &optional predicate)
00d6fd04 3527 "Like `file-name-completion' for Tramp files."
fb7933a3
KG
3528 (unless (tramp-tramp-file-p directory)
3529 (error
3530 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3531 directory))
83e20b5c
MA
3532 (try-completion
3533 filename
3534 (mapcar 'list (file-name-all-completions filename directory))
3535 (when predicate
3536 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
fb7933a3
KG
3537
3538;; cp, mv and ln
3539
3540(defun tramp-handle-add-name-to-file
3541 (filename newname &optional ok-if-already-exists)
00d6fd04
MA
3542 "Like `add-name-to-file' for Tramp files."
3543 (unless (tramp-equal-remote filename newname)
3544 (with-parsed-tramp-file-name
3545 (if (tramp-tramp-file-p filename) filename newname) nil
3546 (tramp-error
3547 v 'file-error
3548 "add-name-to-file: %s"
3549 "only implemented for same method, same user, same host")))
c62c9d08
KG
3550 (with-parsed-tramp-file-name filename v1
3551 (with-parsed-tramp-file-name newname v2
00d6fd04 3552 (let ((ln (when v1 (tramp-get-remote-ln v1))))
c62c9d08
KG
3553 (when (and (not ok-if-already-exists)
3554 (file-exists-p newname)
3555 (not (numberp ok-if-already-exists))
3556 (y-or-n-p
3557 (format
3558 "File %s already exists; make it a new name anyway? "
3559 newname)))
00d6fd04
MA
3560 (tramp-error
3561 v2 'file-error
3562 "add-name-to-file: file %s already exists" newname))
aac0b0f2 3563 (tramp-flush-file-property v2 (file-name-directory v2-localname))
00d6fd04 3564 (tramp-flush-file-property v2 v2-localname)
c62c9d08 3565 (tramp-barf-unless-okay
00d6fd04 3566 v1
7432277c
KG
3567 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3568 (tramp-shell-quote-argument v2-localname))
c62c9d08
KG
3569 "error with add-name-to-file, see buffer `%s' for details"
3570 (buffer-name))))))
fb7933a3
KG
3571
3572(defun tramp-handle-copy-file
25f14cdb
MA
3573 (filename newname &optional ok-if-already-exists keep-date
3574 preserve-uid-gid preserve-selinux-context)
00d6fd04 3575 "Like `copy-file' for Tramp files."
fb7933a3
KG
3576 (setq filename (expand-file-name filename))
3577 (setq newname (expand-file-name newname))
9e6ab520 3578 (cond
a4aeb9a4 3579 ;; At least one file a Tramp file?
9e6ab520
MA
3580 ((or (tramp-tramp-file-p filename)
3581 (tramp-tramp-file-p newname))
3582 (tramp-do-copy-or-rename-file
632c5478
MA
3583 'copy filename newname ok-if-already-exists keep-date
3584 preserve-uid-gid preserve-selinux-context))
9e6ab520 3585 ;; Compat section.
632c5478
MA
3586 (preserve-selinux-context
3587 (tramp-run-real-handler
3588 'copy-file
3589 (list filename newname ok-if-already-exists keep-date
3590 preserve-uid-gid preserve-selinux-context)))
9e6ab520 3591 (preserve-uid-gid
fb7933a3 3592 (tramp-run-real-handler
8d60099b 3593 'copy-file
9e6ab520
MA
3594 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3595 (t
3596 (tramp-run-real-handler
3597 'copy-file (list filename newname ok-if-already-exists keep-date)))))
fb7933a3 3598
263c02ef
MA
3599(defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3600 "Like `copy-directory' for Tramp files."
3601 (let ((t1 (tramp-tramp-file-p dirname))
3602 (t2 (tramp-tramp-file-p newname)))
3603 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3604 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3605 ;; When DIRNAME and NEWNAME are remote, they must have
3606 ;; the same method.
3607 (or (null t1) (null t2)
b000a6e2
MA
3608 (string-equal
3609 (tramp-file-name-method (tramp-dissect-file-name dirname))
3610 (tramp-file-name-method (tramp-dissect-file-name newname)))))
263c02ef
MA
3611 ;; scp or rsync DTRT.
3612 (progn
3613 (setq dirname (directory-file-name (expand-file-name dirname))
3614 newname (directory-file-name (expand-file-name newname)))
3615 (if (and (file-directory-p newname)
3616 (not (string-equal (file-name-nondirectory dirname)
3617 (file-name-nondirectory newname))))
3618 (setq newname
3619 (expand-file-name
3620 (file-name-nondirectory dirname) newname)))
3621 (if (not (file-directory-p (file-name-directory newname)))
3622 (make-directory (file-name-directory newname) parents))
3623 (tramp-do-copy-or-rename-file-out-of-band
3624 'copy dirname newname keep-date))
3625 ;; We must do it file-wise.
3626 (tramp-run-real-handler
aac0b0f2
MA
3627 'copy-directory (list dirname newname keep-date parents)))
3628
3629 ;; When newname did exist, we have wrong cached values.
3630 (when t2
3631 (with-parsed-tramp-file-name newname nil
3632 (tramp-flush-file-property v (file-name-directory localname))
3633 (tramp-flush-file-property v localname))))))
263c02ef 3634
fb7933a3
KG
3635(defun tramp-handle-rename-file
3636 (filename newname &optional ok-if-already-exists)
00d6fd04 3637 "Like `rename-file' for Tramp files."
fb7933a3 3638 ;; Check if both files are local -- invoke normal rename-file.
a4aeb9a4 3639 ;; Otherwise, use Tramp from local system.
fb7933a3
KG
3640 (setq filename (expand-file-name filename))
3641 (setq newname (expand-file-name newname))
a4aeb9a4 3642 ;; At least one file a Tramp file?
fb7933a3
KG
3643 (if (or (tramp-tramp-file-p filename)
3644 (tramp-tramp-file-p newname))
3645 (tramp-do-copy-or-rename-file
8d60099b 3646 'rename filename newname ok-if-already-exists t t)
00d6fd04
MA
3647 (tramp-run-real-handler
3648 'rename-file (list filename newname ok-if-already-exists))))
fb7933a3
KG
3649
3650(defun tramp-do-copy-or-rename-file
632c5478
MA
3651 (op filename newname &optional ok-if-already-exists keep-date
3652 preserve-uid-gid preserve-selinux-context)
fb7933a3
KG
3653 "Copy or rename a remote file.
3654OP must be `copy' or `rename' and indicates the operation to perform.
3655FILENAME specifies the file to copy or rename, NEWNAME is the name of
3656the new file (for copy) or the new name of the file (for rename).
3657OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3658KEEP-DATE means to make sure that NEWNAME has the same timestamp
8d60099b
MA
3659as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3660the uid and gid if both files are on the same host.
632c5478 3661PRESERVE-SELINUX-CONTEXT activates selinux commands.
fb7933a3
KG
3662
3663This function is invoked by `tramp-handle-copy-file' and
3664`tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3665and `rename'. FILENAME and NEWNAME must be absolute file names."
3666 (unless (memq op '(copy rename))
3667 (error "Unknown operation `%s', must be `copy' or `rename'" op))
90dc758d 3668 (let ((t1 (tramp-tramp-file-p filename))
b6bbb65e 3669 (t2 (tramp-tramp-file-p newname))
632c5478
MA
3670 (context (and preserve-selinux-context
3671 (apply 'file-selinux-context (list filename))))
b6bbb65e 3672 pr tm)
5ec2cc41 3673
655bded0
MA
3674 (with-parsed-tramp-file-name (if t1 filename newname) nil
3675 (when (and (not ok-if-already-exists) (file-exists-p newname))
da1975d7 3676 (tramp-error
655bded0 3677 v 'file-already-exists "File %s already exists" newname))
5ec2cc41 3678
a94d821f 3679 (with-progress-reporter
655bded0
MA
3680 v 0 (format "%s %s to %s"
3681 (if (eq op 'copy) "Copying" "Renaming")
3682 filename newname)
905fb90e 3683
655bded0
MA
3684 (cond
3685 ;; Both are Tramp files.
3686 ((and t1 t2)
3687 (with-parsed-tramp-file-name filename v1
3688 (with-parsed-tramp-file-name newname v2
3689 (cond
3690 ;; Shortcut: if method, host, user are the same for
3691 ;; both files, we invoke `cp' or `mv' on the remote
3692 ;; host directly.
3693 ((tramp-equal-remote filename newname)
3694 (tramp-do-copy-or-rename-file-directly
3695 op filename newname
3696 ok-if-already-exists keep-date preserve-uid-gid))
3697
3698 ;; Try out-of-band operation.
3699 ((tramp-method-out-of-band-p
3700 v1 (nth 7 (file-attributes filename)))
3701 (tramp-do-copy-or-rename-file-out-of-band
3702 op filename newname keep-date))
3703
3704 ;; No shortcut was possible. So we copy the file
3705 ;; first. If the operation was `rename', we go back
3706 ;; and delete the original file (if the copy was
3707 ;; successful). The approach is simple-minded: we
3708 ;; create a new buffer, insert the contents of the
3709 ;; source file into it, then write out the buffer to
3710 ;; the target file. The advantage is that it doesn't
3711 ;; matter which filename handlers are used for the
3712 ;; source and target file.
3713 (t
3714 (tramp-do-copy-or-rename-file-via-buffer
3715 op filename newname keep-date))))))
3716
3717 ;; One file is a Tramp file, the other one is local.
3718 ((or t1 t2)
3719 (cond
3720 ;; Fast track on local machine.
3721 ((tramp-local-host-p v)
3722 (tramp-do-copy-or-rename-file-directly
3723 op filename newname
3724 ok-if-already-exists keep-date preserve-uid-gid))
3725
3726 ;; If the Tramp file has an out-of-band method, the
3727 ;; corresponding copy-program can be invoked.
3728 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3729 (tramp-do-copy-or-rename-file-out-of-band
3730 op filename newname keep-date))
3731
3732 ;; Use the inline method via a Tramp buffer.
3733 (t (tramp-do-copy-or-rename-file-via-buffer
3734 op filename newname keep-date))))
3735
3736 (t
3737 ;; One of them must be a Tramp file.
3738 (error "Tramp implementation says this cannot happen")))
3739
3740 ;; Handle `preserve-selinux-context'.
3741 (when context (apply 'set-file-selinux-context (list newname context)))
3742
3743 ;; In case of `rename', we must flush the cache of the source file.
3744 (when (and t1 (eq op 'rename))
3745 (with-parsed-tramp-file-name filename v1
3746 (tramp-flush-file-property v1 (file-name-directory localname))
3747 (tramp-flush-file-property v1 localname)))
3748
3749 ;; When newname did exist, we have wrong cached values.
3750 (when t2
3751 (with-parsed-tramp-file-name newname v2
3752 (tramp-flush-file-property v2 (file-name-directory localname))
3753 (tramp-flush-file-property v2 localname)))))))
7432277c 3754
38c65fca 3755(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
90dc758d
KG
3756 "Use an Emacs buffer to copy or rename a file.
3757First arg OP is either `copy' or `rename' and indicates the operation.
3758FILENAME is the source file, NEWNAME the target file.
3759KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
8a798e41
MA
3760 (with-temp-buffer
3761 ;; We must disable multibyte, because binary data shall not be
3762 ;; converted.
3763 (set-buffer-multibyte nil)
3764 (let ((coding-system-for-read 'binary)
3765 (jka-compr-inhibit t))
3766 (insert-file-contents-literally filename))
3767 ;; We don't want the target file to be compressed, so we let-bind
3768 ;; `jka-compr-inhibit' to t.
3769 (let ((coding-system-for-write 'binary)
3770 (jka-compr-inhibit t))
3771 (write-region (point-min) (point-max) newname)))
3772 ;; KEEP-DATE handling.
3773 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3774 ;; Set the mode.
b86c1cd8 3775 (set-file-modes newname (tramp-default-file-modes filename))
8a798e41 3776 ;; If the operation was `rename', delete the original file.
eba082a2 3777 (unless (eq op 'copy) (delete-file filename)))
fb7933a3
KG
3778
3779(defun tramp-do-copy-or-rename-file-directly
8d60099b 3780 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
fb7933a3
KG
3781 "Invokes `cp' or `mv' on the remote system.
3782OP must be one of `copy' or `rename', indicating `cp' or `mv',
8d60099b
MA
3783respectively. FILENAME specifies the file to copy or rename,
3784NEWNAME is the name of the new file (for copy) or the new name of
3785the file (for rename). Both files must reside on the same host.
3786KEEP-DATE means to make sure that NEWNAME has the same timestamp
3787as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3788the uid and gid from FILENAME."
8a4438b6 3789 (let ((t1 (tramp-tramp-file-p filename))
4f4126e6
MA
3790 (t2 (tramp-tramp-file-p newname))
3791 (file-times (nth 5 (file-attributes filename)))
3792 (file-modes (tramp-default-file-modes filename)))
8a4438b6
MA
3793 (with-parsed-tramp-file-name (if t1 filename newname) nil
3794 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3795 ((eq op 'copy) "cp -f")
3796 ((eq op 'rename) "mv -f")
3797 (t (tramp-error
3798 v 'file-error
3799 "Unknown operation `%s', must be `copy' or `rename'"
3800 op))))
3801 (localname1
b533bc97
MA
3802 (if t1
3803 (tramp-file-name-handler 'file-remote-p filename 'localname)
3804 filename))
8a4438b6 3805 (localname2
b533bc97
MA
3806 (if t2
3807 (tramp-file-name-handler 'file-remote-p newname 'localname)
3808 newname))
293c24f9
MA
3809 (prefix (file-remote-p (if t1 filename newname)))
3810 cmd-result)
8d60099b 3811
8d60099b 3812 (cond
8a4438b6
MA
3813 ;; Both files are on a remote host, with same user.
3814 ((and t1 t2)
293c24f9
MA
3815 (setq cmd-result
3816 (tramp-send-command-and-check
3817 v
3818 (format "%s %s %s" cmd
3819 (tramp-shell-quote-argument localname1)
3820 (tramp-shell-quote-argument localname2))))
8a4438b6
MA
3821 (with-current-buffer (tramp-get-buffer v)
3822 (goto-char (point-min))
3823 (unless
3824 (or
3825 (and keep-date
3826 ;; Mask cp -f error.
3827 (re-search-forward
3828 tramp-operation-not-permitted-regexp nil t))
293c24f9 3829 (zerop cmd-result))
8a4438b6
MA
3830 (tramp-error-with-buffer
3831 nil v 'file-error
3832 "Copying directly failed, see buffer `%s' for details."
3833 (buffer-name)))))
3834
3835 ;; We are on the local host.
3836 ((or t1 t2)
8d60099b 3837 (cond
8a4438b6 3838 ;; We can do it directly.
87bdd2c7
MA
3839 ((let (file-name-handler-alist)
3840 (and (file-readable-p localname1)
3841 (file-writable-p (file-name-directory localname2))
3842 (or (file-directory-p localname2)
3843 (file-writable-p localname2))))
8d60099b 3844 (if (eq op 'copy)
9e6ab520
MA
3845 (tramp-compat-copy-file
3846 localname1 localname2 ok-if-already-exists
3847 keep-date preserve-uid-gid)
87bdd2c7
MA
3848 (tramp-run-real-handler
3849 'rename-file (list localname1 localname2 ok-if-already-exists))))
8a4438b6
MA
3850
3851 ;; We can do it directly with `tramp-send-command'
946a5aeb
MA
3852 ((and (file-readable-p (concat prefix localname1))
3853 (file-writable-p
3854 (file-name-directory (concat prefix localname2)))
3855 (or (file-directory-p (concat prefix localname2))
3856 (file-writable-p (concat prefix localname2))))
8a4438b6
MA
3857 (tramp-do-copy-or-rename-file-directly
3858 op (concat prefix localname1) (concat prefix localname2)
3859 ok-if-already-exists keep-date t)
3860 ;; We must change the ownership to the local user.
8d60099b 3861 (tramp-set-file-uid-gid
8a4438b6
MA
3862 (concat prefix localname2)
3863 (tramp-get-local-uid 'integer)
3864 (tramp-get-local-gid 'integer)))
8d60099b 3865
8a4438b6
MA
3866 ;; We need a temporary file in between.
3867 (t
2c418c5b
MA
3868 ;; Create the temporary file.
3869 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
917b89a6 3870 (unwind-protect
2c418c5b
MA
3871 (progn
3872 (cond
3873 (t1
917b89a6
MA
3874 (or
3875 (zerop
3876 (tramp-send-command-and-check
3877 v (format
3878 "%s %s %s" cmd
3879 (tramp-shell-quote-argument localname1)
3880 (tramp-shell-quote-argument tmpfile))))
3881 (tramp-error-with-buffer
3882 nil v 'file-error
3883 "Copying directly failed, see buffer `%s' for details."
3884 (tramp-get-buffer v)))
2c418c5b 3885 ;; We must change the ownership as remote user.
917b89a6
MA
3886 ;; Since this does not work reliable, we also
3887 ;; give read permissions.
3888 (set-file-modes
3889 (concat prefix tmpfile) (tramp-octal-to-decimal "0777"))
2c418c5b
MA
3890 (tramp-set-file-uid-gid
3891 (concat prefix tmpfile)
3892 (tramp-get-local-uid 'integer)
3893 (tramp-get-local-gid 'integer)))
3894 (t2
3895 (if (eq op 'copy)
3896 (tramp-compat-copy-file
5ab38c3c 3897 localname1 tmpfile t
2c418c5b
MA
3898 keep-date preserve-uid-gid)
3899 (tramp-run-real-handler
3900 'rename-file
5ab38c3c 3901 (list localname1 tmpfile t)))
2c418c5b 3902 ;; We must change the ownership as local user.
917b89a6
MA
3903 ;; Since this does not work reliable, we also
3904 ;; give read permissions.
3905 (set-file-modes tmpfile (tramp-octal-to-decimal "0777"))
2c418c5b
MA
3906 (tramp-set-file-uid-gid
3907 tmpfile
3908 (tramp-get-remote-uid v 'integer)
3909 (tramp-get-remote-gid v 'integer))))
3910
3911 ;; Move the temporary file to its destination.
3912 (cond
3913 (t2
917b89a6
MA
3914 (or
3915 (zerop
3916 (tramp-send-command-and-check
3917 v (format
3918 "cp -f -p %s %s"
3919 (tramp-shell-quote-argument tmpfile)
3920 (tramp-shell-quote-argument localname2))))
3921 (tramp-error-with-buffer
3922 nil v 'file-error
3923 "Copying directly failed, see buffer `%s' for details."
3924 (tramp-get-buffer v))))
2c418c5b 3925 (t1
ce2cc728
MA
3926 (tramp-run-real-handler
3927 'rename-file
2c418c5b
MA
3928 (list tmpfile localname2 ok-if-already-exists)))))
3929
917b89a6
MA
3930 ;; Save exit.
3931 (condition-case nil
eba082a2 3932 (delete-file tmpfile)
917b89a6 3933 (error)))))))))
8d60099b
MA
3934
3935 ;; Set the time and mode. Mask possible errors.
8d60099b 3936 (condition-case nil
1f107aed 3937 (when keep-date
4f4126e6
MA
3938 (set-file-times newname file-times)
3939 (set-file-modes newname file-modes))
8d60099b
MA
3940 (error)))))
3941
5ec2cc41 3942(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
7432277c 3943 "Invoke rcp program to copy.
905fb90e 3944The method used must be an out-of-band method."
38c65fca 3945 (let ((t1 (tramp-tramp-file-p filename))
5ec2cc41 3946 (t2 (tramp-tramp-file-p newname))
946a5aeb 3947 copy-program copy-args copy-env copy-keep-date port spec
00d6fd04
MA
3948 source target)
3949
3950 (with-parsed-tramp-file-name (if t1 filename newname) nil
905fb90e 3951 (if (and t1 t2)
00d6fd04 3952
905fb90e
MA
3953 ;; Both are Tramp files. We shall optimize it, when the
3954 ;; methods for filename and newname are the same.
9cf3544e
MA
3955 (let* ((dir-flag (file-directory-p filename))
3956 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
3957 (if dir-flag
3958 (setq tmpfile
3959 (expand-file-name
3960 (file-name-nondirectory newname) tmpfile)))
905fb90e
MA
3961 (unwind-protect
3962 (progn
3963 (tramp-do-copy-or-rename-file-out-of-band
3964 op filename tmpfile keep-date)
3965 (tramp-do-copy-or-rename-file-out-of-band
3966 'rename tmpfile newname keep-date))
3967 ;; Save exit.
3968 (condition-case nil
9cf3544e 3969 (if dir-flag
b533bc97 3970 (tramp-compat-delete-directory
9cf3544e 3971 (expand-file-name ".." tmpfile) 'recursive)
eba082a2 3972 (delete-file tmpfile))
905fb90e
MA
3973 (error))))
3974
3975 ;; Expand hops. Might be necessary for gateway methods.
3976 (setq v (car (tramp-compute-multi-hops v)))
3977 (aset v 3 localname)
3978
3979 ;; Check which ones of source and target are Tramp files.
3980 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
263c02ef
MA
3981 target (funcall
3982 (if (and (file-directory-p filename)
3983 (string-equal
3984 (file-name-nondirectory filename)
3985 (file-name-nondirectory newname)))
3986 'file-name-directory
3987 'identity)
3988 (if t2 (tramp-make-copy-program-file-name v) newname)))
905fb90e
MA
3989
3990 ;; Check for port number. Until now, there's no need for handling
3991 ;; like method, user, host.
3992 (setq host (tramp-file-name-real-host v)
3993 port (tramp-file-name-port v)
3994 port (or (and port (number-to-string port)) ""))
3995
3996 ;; Compose copy command.
b533bc97
MA
3997 (setq spec (format-spec-make
3998 ?h host ?u user ?p port
3999 ?t (tramp-get-connection-property
4000 (tramp-get-connection-process v) "temp-file" "")
4001 ?k (if keep-date " " ""))
905fb90e
MA
4002 copy-program (tramp-get-method-parameter
4003 method 'tramp-copy-program)
4004 copy-keep-date (tramp-get-method-parameter
4005 method 'tramp-copy-keep-date)
4006 copy-args
4007 (delq
4008 nil
4009 (mapcar
aa485f7c
MA
4010 (lambda (x)
4011 (setq
4012 x
4013 ;; " " is indication for keep-date argument.
4014 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
4015 (unless (member "" x) (mapconcat 'identity x " ")))
946a5aeb
MA
4016 (tramp-get-method-parameter method 'tramp-copy-args)))
4017 copy-env
4018 (delq
4019 nil
4020 (mapcar
aa485f7c
MA
4021 (lambda (x)
4022 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4023 (unless (member "" x) (mapconcat 'identity x " ")))
946a5aeb 4024 (tramp-get-method-parameter method 'tramp-copy-env))))
905fb90e
MA
4025
4026 ;; Check for program.
4027 (when (and (fboundp 'executable-find)
4028 (not (let ((default-directory
4029 (tramp-compat-temporary-file-directory)))
4030 (executable-find copy-program))))
4031 (tramp-error
4032 v 'file-error "Cannot find copy program: %s" copy-program))
00d6fd04 4033
4265deab
MA
4034 ;; Set variables for computing the prompt for reading
4035 ;; password.
4036 (setq tramp-current-method (tramp-file-name-method v)
4037 tramp-current-user (tramp-file-name-user v)
4038 tramp-current-host (tramp-file-name-host v))
4039
905fb90e
MA
4040 (unwind-protect
4041 (with-temp-buffer
4042 ;; The default directory must be remote.
4043 (let ((default-directory
946a5aeb
MA
4044 (file-name-directory (if t1 filename newname)))
4045 (process-environment (copy-sequence process-environment)))
905fb90e
MA
4046 ;; Set the transfer process properties.
4047 (tramp-set-connection-property
4048 v "process-name" (buffer-name (current-buffer)))
4049 (tramp-set-connection-property
4050 v "process-buffer" (current-buffer))
946a5aeb
MA
4051 (while copy-env
4052 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
4053 (setenv (pop copy-env) (pop copy-env)))
905fb90e
MA
4054
4055 ;; Use an asynchronous process. By this, password can
4056 ;; be handled. The default directory must be local, in
4057 ;; order to apply the correct `copy-program'. We don't
4058 ;; set a timeout, because the copying of large files can
4059 ;; last longer than 60 secs.
4060 (let ((p (let ((default-directory
4061 (tramp-compat-temporary-file-directory)))
4062 (apply 'start-process
4063 (tramp-get-connection-property
4064 v "process-name" nil)
4065 (tramp-get-connection-property
4066 v "process-buffer" nil)
4067 copy-program
4068 (append copy-args (list source target))))))
4069 (tramp-message
4070 v 6 "%s" (mapconcat 'identity (process-command p) " "))
4071 (tramp-set-process-query-on-exit-flag p nil)
4072 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
00d6fd04 4073
905fb90e
MA
4074 ;; Reset the transfer process properties.
4075 (tramp-set-connection-property v "process-name" nil)
4076 (tramp-set-connection-property v "process-buffer" nil))
00d6fd04 4077
905fb90e
MA
4078 ;; Handle KEEP-DATE argument.
4079 (when (and keep-date (not copy-keep-date))
4080 (set-file-times newname (nth 5 (file-attributes filename))))
01917a18 4081
905fb90e
MA
4082 ;; Set the mode.
4083 (unless (and keep-date copy-keep-date)
459a5f4b
MA
4084 (ignore-errors
4085 (set-file-modes newname (tramp-default-file-modes filename)))))
5ec2cc41 4086
905fb90e
MA
4087 ;; If the operation was `rename', delete the original file.
4088 (unless (eq op 'copy)
aac0b0f2 4089 (if (file-regular-p filename)
eba082a2 4090 (delete-file filename)
b533bc97 4091 (tramp-compat-delete-directory filename 'recursive))))))
7432277c 4092
fb7933a3 4093(defun tramp-handle-make-directory (dir &optional parents)
00d6fd04 4094 "Like `make-directory' for Tramp files."
ac474af1 4095 (setq dir (expand-file-name dir))
c62c9d08 4096 (with-parsed-tramp-file-name dir nil
c15cdf02 4097 (tramp-flush-directory-property v (file-name-directory localname))
b1d06e75
KG
4098 (save-excursion
4099 (tramp-barf-unless-okay
00d6fd04 4100 v
9c13938d 4101 (format "%s %s"
b1d06e75 4102 (if parents "mkdir -p" "mkdir")
7432277c 4103 (tramp-shell-quote-argument localname))
b1d06e75 4104 "Couldn't make directory %s" dir))))
fb7933a3 4105
c15cdf02 4106(defun tramp-handle-delete-directory (directory &optional recursive)
00d6fd04 4107 "Like `delete-directory' for Tramp files."
ac474af1 4108 (setq directory (expand-file-name directory))
c62c9d08 4109 (with-parsed-tramp-file-name directory nil
aac0b0f2 4110 (tramp-flush-file-property v (file-name-directory localname))
00d6fd04
MA
4111 (tramp-flush-directory-property v localname)
4112 (unless (zerop (tramp-send-command-and-check
4113 v
c15cdf02
MA
4114 (format
4115 "%s %s"
4116 (if recursive "rm -rf" "rmdir")
4117 (tramp-shell-quote-argument localname))))
00d6fd04 4118 (tramp-error v 'file-error "Couldn't delete %s" directory))))
fb7933a3 4119
f1a5d776 4120(defun tramp-handle-delete-file (filename &optional trash)
00d6fd04 4121 "Like `delete-file' for Tramp files."
ac474af1 4122 (setq filename (expand-file-name filename))
c62c9d08 4123 (with-parsed-tramp-file-name filename nil
aac0b0f2 4124 (tramp-flush-file-property v (file-name-directory localname))
00d6fd04 4125 (tramp-flush-file-property v localname)
b6827fff
MA
4126 (unless
4127 (zerop
4128 (tramp-send-command-and-check
4129 v (format "%s %s"
4130 (or (and trash (tramp-get-remote-trash v)) "rm -f")
4131 (tramp-shell-quote-argument localname))))
00d6fd04 4132 (tramp-error v 'file-error "Couldn't delete %s" filename))))
fb7933a3
KG
4133
4134;; Dired.
4135
4136;; CCC: This does not seem to be enough. Something dies when
a4aeb9a4 4137;; we try and delete two directories under Tramp :/
fb7933a3
KG
4138(defun tramp-handle-dired-recursive-delete-directory (filename)
4139 "Recursively delete the directory given.
00d6fd04 4140This is like `dired-recursive-delete-directory' for Tramp files."
c62c9d08 4141 (with-parsed-tramp-file-name filename nil
00d6fd04 4142 ;; Run a shell command 'rm -r <localname>'
260821d3 4143 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
00d6fd04
MA
4144 (unless (file-exists-p filename)
4145 (tramp-error v 'file-error "No such directory: %s" filename))
fb7933a3 4146 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
00d6fd04
MA
4147 (tramp-send-command
4148 v
9c13938d 4149 (format "rm -rf %s" (tramp-shell-quote-argument localname))
00d6fd04
MA
4150 ;; Don't read the output, do it explicitely.
4151 nil t)
fb7933a3
KG
4152 ;; Wait for the remote system to return to us...
4153 ;; This might take a while, allow it plenty of time.
00d6fd04 4154 (tramp-wait-for-output (tramp-get-connection-process v) 120)
fb7933a3 4155 ;; Make sure that it worked...
aac0b0f2 4156 (tramp-flush-file-property v (file-name-directory localname))
c15cdf02 4157 (tramp-flush-directory-property v localname)
07dfe738 4158 (and (file-exists-p filename)
00d6fd04
MA
4159 (tramp-error
4160 v 'file-error "Failed to recursively delete %s" filename))))
bf247b6e 4161
5ec2cc41 4162(defun tramp-handle-dired-compress-file (file &rest ok-flag)
00d6fd04 4163 "Like `dired-compress-file' for Tramp files."
5ec2cc41
KG
4164 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4165 ;; Code stolen mainly from dired-aux.el.
4166 (with-parsed-tramp-file-name file nil
00d6fd04 4167 (tramp-flush-file-property v localname)
5ec2cc41
KG
4168 (save-excursion
4169 (let ((suffixes
4170 (if (not (featurep 'xemacs))
4171 ;; Emacs case
4172 (symbol-value 'dired-compress-file-suffixes)
4173 ;; XEmacs has `dired-compression-method-alist', which is
4174 ;; transformed into `dired-compress-file-suffixes' structure.
4175 (mapcar
aa485f7c
MA
4176 (lambda (x)
4177 (list (concat (regexp-quote (nth 1 x)) "\\'")
4178 nil
4179 (mapconcat 'identity (nth 3 x) " ")))
5ec2cc41
KG
4180 (symbol-value 'dired-compression-method-alist))))
4181 suffix)
4182 ;; See if any suffix rule matches this file name.
4183 (while suffixes
4184 (let (case-fold-search)
4185 (if (string-match (car (car suffixes)) localname)
4186 (setq suffix (car suffixes) suffixes nil))
4187 (setq suffixes (cdr suffixes))))
4188
4189 (cond ((file-symlink-p file)
4190 nil)
4191 ((and suffix (nth 2 suffix))
4192 ;; We found an uncompression rule.
3b30ccda 4193 (with-progress-reporter v 0 (format "Uncompressing %s" file)
a94d821f
MA
4194 (when (zerop
4195 (tramp-send-command-and-check
4196 v (concat (nth 2 suffix) " "
4197 (tramp-shell-quote-argument localname))))
0d5852cf
MA
4198 ;; `dired-remove-file' is not defined in XEmacs.
4199 (tramp-compat-funcall 'dired-remove-file file)
a94d821f
MA
4200 (string-match (car suffix) file)
4201 (concat (substring file 0 (match-beginning 0))))))
5ec2cc41
KG
4202 (t
4203 ;; We don't recognize the file as compressed, so compress it.
4204 ;; Try gzip.
3b30ccda 4205 (with-progress-reporter v 0 (format "Compressing %s" file)
a94d821f
MA
4206 (when (zerop
4207 (tramp-send-command-and-check
4208 v (concat "gzip -f "
4209 (tramp-shell-quote-argument localname))))
0d5852cf
MA
4210 ;; `dired-remove-file' is not defined in XEmacs.
4211 (tramp-compat-funcall 'dired-remove-file file)
a94d821f
MA
4212 (cond ((file-exists-p (concat file ".gz"))
4213 (concat file ".gz"))
4214 ((file-exists-p (concat file ".z"))
4215 (concat file ".z"))
4216 (t nil))))))))))
fb7933a3 4217
d5b3979c 4218(defun tramp-handle-dired-uncache (dir &optional dir-p)
70c11b0b 4219 "Like `dired-uncache' for Tramp files."
d5b3979c
MA
4220 ;; DIR-P is valid for XEmacs only.
4221 (with-parsed-tramp-file-name
4222 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
70c11b0b
MA
4223 (tramp-flush-file-property v localname)))
4224
fb7933a3
KG
4225;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4226;; not sure at all that this is the right way to do it, but let's hope
4227;; it works for now, and wait for a guru to point out the Right Way to
4228;; achieve this.
4229;;(eval-when-compile
4230;; (unless (fboundp 'dired-insert-set-properties)
4231;; (fset 'dired-insert-set-properties 'ignore)))
4232;; Gerd suggests this:
4233(eval-when-compile (require 'dired))
4234;; Note that dired is required at run-time, too, when it is needed.
4235;; It is only needed on XEmacs for the function
4236;; `dired-insert-set-properties'.
4237
4238(defun tramp-handle-insert-directory
4239 (filename switches &optional wildcard full-directory-p)
00d6fd04
MA
4240 "Like `insert-directory' for Tramp files."
4241 (setq filename (expand-file-name filename))
4242 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
4243 (if (and (featurep 'ls-lisp)
4244 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
4245 (tramp-run-real-handler
4246 'insert-directory (list filename switches wildcard full-directory-p))
0c0b61f1
MA
4247 (when (stringp switches)
4248 (setq switches (split-string switches)))
4249 (when (and (member "--dired" switches)
8e754ea2 4250 (not (tramp-get-ls-command-with-dired v)))
0c0b61f1 4251 (setq switches (delete "--dired" switches)))
c82c5727 4252 (when wildcard
87bdd2c7
MA
4253 (setq wildcard (tramp-run-real-handler
4254 'file-name-nondirectory (list localname)))
4255 (setq localname (tramp-run-real-handler
4256 'file-name-directory (list localname))))
c82c5727 4257 (unless full-directory-p
0c0b61f1
MA
4258 (setq switches (add-to-list 'switches "-d" 'append)))
4259 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
c82c5727 4260 (when wildcard
0c0b61f1
MA
4261 (setq switches (concat switches " " wildcard)))
4262 (tramp-message
4263 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4264 switches filename (if wildcard "yes" "no")
4265 (if full-directory-p "yes" "no"))
00d6fd04
MA
4266 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4267 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4268 (if full-directory-p
4269 (tramp-send-command
4270 v
fe5facd3 4271 (format "%s %s %s 2>/dev/null"
00d6fd04
MA
4272 (tramp-get-ls-command v)
4273 switches
4274 (if wildcard
4275 localname
4276 (tramp-shell-quote-argument (concat localname ".")))))
4277 (tramp-barf-unless-okay
4278 v
4279 (format "cd %s" (tramp-shell-quote-argument
87bdd2c7
MA
4280 (tramp-run-real-handler
4281 'file-name-directory (list localname))))
00d6fd04 4282 "Couldn't `cd %s'"
87bdd2c7
MA
4283 (tramp-shell-quote-argument
4284 (tramp-run-real-handler 'file-name-directory (list localname))))
00d6fd04
MA
4285 (tramp-send-command
4286 v
4287 (format "%s %s %s"
4288 (tramp-get-ls-command v)
4289 switches
4290 (if (or wildcard
87bdd2c7
MA
4291 (zerop (length
4292 (tramp-run-real-handler
4293 'file-name-nondirectory (list localname)))))
00d6fd04
MA
4294 ""
4295 (tramp-shell-quote-argument
87bdd2c7
MA
4296 (tramp-run-real-handler
4297 'file-name-nondirectory (list localname)))))))
8e754ea2
MA
4298 (let ((beg (point)))
4299 ;; We cannot use `insert-buffer-substring' because the Tramp
4300 ;; buffer changes its contents before insertion due to calling
4301 ;; `expand-file' and alike.
4302 (insert
4303 (with-current-buffer (tramp-get-buffer v)
4304 (buffer-string)))
4305
4306 ;; Check for "--dired" output.
8e754ea2 4307 (forward-line -2)
7f4d4a97
MA
4308 (when (looking-at "//SUBDIRED//")
4309 (forward-line -1))
fa9ba953
MA
4310 (when (looking-at "//DIRED//\\s-+")
4311 (let ((databeg (match-end 0))
4312 (end (tramp-compat-line-end-position)))
8e754ea2 4313 ;; Now read the numeric positions of file names.
fa9ba953 4314 (goto-char databeg)
8e754ea2
MA
4315 (while (< (point) end)
4316 (let ((start (+ beg (read (current-buffer))))
4317 (end (+ beg (read (current-buffer)))))
7f49fe46 4318 (if (memq (char-after end) '(?\n ?\ ))
8e754ea2 4319 ;; End is followed by \n or by " -> ".
fe5facd3
MA
4320 (put-text-property start end 'dired-filename t))))))
4321 ;; Remove trailing lines.
4322 (goto-char (tramp-compat-line-beginning-position))
4323 (while (looking-at "//")
4324 (forward-line 1)
4325 (delete-region (match-beginning 0) (point)))
0c0b61f1
MA
4326
4327 ;; The inserted file could be from somewhere else.
4328 (when (and (not wildcard) (not full-directory-p))
4329 (goto-char (point-max))
e5c70c41
MA
4330 (when (file-symlink-p filename)
4331 (goto-char (search-backward "->" beg 'noerror)))
0c0b61f1
MA
4332 (search-backward
4333 (if (zerop (length (file-name-nondirectory filename)))
4334 "."
4335 (file-name-nondirectory filename))
4336 beg 'noerror)
4337 (replace-match (file-relative-name filename) t))
4338
fe5facd3 4339 (goto-char (point-max))))))
fb7933a3 4340
fb7933a3 4341(defun tramp-handle-unhandled-file-name-directory (filename)
00d6fd04 4342 "Like `unhandled-file-name-directory' for Tramp files."
8a798e41
MA
4343 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4344 ;; for backward compatibility.
ce3f516f 4345 (expand-file-name "~/"))
fb7933a3
KG
4346
4347;; Canonicalization of file names.
4348
fb7933a3 4349(defun tramp-handle-expand-file-name (name &optional dir)
00d6fd04 4350 "Like `expand-file-name' for Tramp files.
7432277c
KG
4351If the localname part of the given filename starts with \"/../\" then
4352the result will be a local, non-Tramp, filename."
fb7933a3
KG
4353 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4354 (setq dir (or dir default-directory "/"))
4355 ;; Unless NAME is absolute, concat DIR and NAME.
4356 (unless (file-name-absolute-p name)
4357 (setq name (concat (file-name-as-directory dir) name)))
00d6fd04 4358 ;; If NAME is not a Tramp file, run the real handler.
acd1f317 4359 (if (not (tramp-connectable-p name))
00d6fd04 4360 (tramp-run-real-handler 'expand-file-name (list name nil))
fb7933a3 4361 ;; Dissect NAME.
c62c9d08 4362 (with-parsed-tramp-file-name name nil
87bdd2c7 4363 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
7432277c 4364 (setq localname (concat "~/" localname)))
00d6fd04
MA
4365 ;; Tilde expansion if necessary. This needs a shell which
4366 ;; groks tilde expansion! The function `tramp-find-shell' is
4367 ;; supposed to find such a shell on the remote host. Please
4368 ;; tell me about it when this doesn't work on your system.
4369 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4370 (let ((uname (match-string 1 localname))
4371 (fname (match-string 2 localname)))
4372 ;; We cannot simply apply "~/", because under sudo "~/" is
4373 ;; expanded to the local user home directory but to the
4374 ;; root home directory. On the other hand, using always
4375 ;; the default user name for tilde expansion is not
4376 ;; appropriate either, because ssh and companions might
4377 ;; use a user name from the config file.
4378 (when (and (string-equal uname "~")
4379 (string-match "\\`su\\(do\\)?\\'" method))
4380 (setq uname (concat uname user)))
4381 (setq uname
b593f105
MA
4382 (with-connection-property v uname
4383 (tramp-send-command
4384 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
4385 (with-current-buffer (tramp-get-buffer v)
4386 (goto-char (point-min))
4387 (buffer-substring
4388 (point) (tramp-compat-line-end-position)))))
00d6fd04
MA
4389 (setq localname (concat uname fname))))
4390 ;; There might be a double slash, for example when "~/"
cb85dcd0 4391 ;; expands to "/". Remove this.
00d6fd04
MA
4392 (while (string-match "//" localname)
4393 (setq localname (replace-match "/" t t localname)))
4394 ;; No tilde characters in file name, do normal
a17632c1
MA
4395 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4396 ;; `directory-sep-char' here for XEmacs on Windows, which would
4397 ;; otherwise use backslash. `default-directory' is bound,
4398 ;; because on Windows there would be problems with UNC shares or
4399 ;; Cygwin mounts.
aff67808
MA
4400 (let ((directory-sep-char ?/)
4401 (default-directory (tramp-compat-temporary-file-directory)))
4402 (tramp-make-tramp-file-name
4403 method user host
4404 (tramp-drop-volume-letter
87bdd2c7
MA
4405 (tramp-run-real-handler
4406 'expand-file-name (list localname))))))))
00d6fd04 4407
c23c3394
MA
4408(defun tramp-replace-environment-variables (filename)
4409 "Replace environment variables in FILENAME.
4410Return the string with the replaced variables."
2e271195 4411 (save-match-data
ec5145d6 4412 (let ((idx (string-match "$\\(\\w+\\)" filename)))
2e271195 4413 ;; `$' is coded as `$$'.
ec5145d6
MA
4414 (when (and idx
4415 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
4416 (getenv (match-string 1 filename)))
2e271195
MA
4417 (setq filename
4418 (replace-match
4419 (substitute-in-file-name (match-string 0 filename))
4420 t nil filename)))
4421 filename)))
c23c3394 4422
00d6fd04
MA
4423(defun tramp-handle-substitute-in-file-name (filename)
4424 "Like `substitute-in-file-name' for Tramp files.
4425\"//\" and \"/~\" substitute only in the local filename part.
4426If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4427beginning of local filename are not substituted."
c23c3394
MA
4428 ;; First, we must replace environment variables.
4429 (setq filename (tramp-replace-environment-variables filename))
00d6fd04
MA
4430 (with-parsed-tramp-file-name filename nil
4431 (if (equal tramp-syntax 'url)
4432 ;; We need to check localname only. The other parts cannot contain
4433 ;; "//" or "/~".
4434 (if (and (> (length localname) 1)
4435 (or (string-match "//" localname)
4436 (string-match "/~" localname 1)))
4437 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4438 (tramp-make-tramp-file-name
4439 (when method (substitute-in-file-name method))
4440 (when user (substitute-in-file-name user))
4441 (when host (substitute-in-file-name host))
87bdd2c7
MA
4442 (when localname
4443 (tramp-run-real-handler
4444 'substitute-in-file-name (list localname)))))
00d6fd04
MA
4445 ;; Ignore in LOCALNAME everything before "//" or "/~".
4446 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4447 (setq filename
b08104a0
MA
4448 (concat (file-remote-p filename)
4449 (replace-match "\\1" nil nil localname)))
00d6fd04
MA
4450 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4451 (when (string-match "~$" filename)
4452 (setq filename (concat filename "/"))))
4453 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4454
4455;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4456;; which calls corresponding functions (see minibuf.el).
4457(when (fboundp 'minibuffer-electric-separator)
9e6ab520 4458 (mapc
aa485f7c
MA
4459 (lambda (x)
4460 (eval
4461 `(defadvice ,x
4462 (around ,(intern (format "tramp-advice-%s" x)) activate)
4463 "Invoke `substitute-in-file-name' for Tramp files."
4464 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
4465 (tramp-tramp-file-p (buffer-substring)))
4466 ;; We don't need to handle `last-input-event', because
4467 ;; due to the key map we know it must be ?/ or ?~.
4468 (let ((s (concat (buffer-substring (point-min) (point))
4469 (string last-command-char))))
4470 (delete-region (point-min) (point))
4471 (insert (substitute-in-file-name s))
4472 (setq ad-return-value last-command-char))
d7ec1df7
MA
4473 ad-do-it)))
4474 (eval
4475 `(add-hook
4476 'tramp-unload-hook
4477 (lambda ()
4478 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
4479 (ad-activate ',x)))))
00d6fd04
MA
4480
4481 '(minibuffer-electric-separator
4482 minibuffer-electric-tilde)))
4483
4484
0664ff72 4485;;; Remote commands:
fb7933a3 4486
00d6fd04
MA
4487(defun tramp-handle-executable-find (command)
4488 "Like `executable-find' for Tramp files."
4489 (with-parsed-tramp-file-name default-directory nil
f84638eb 4490 (tramp-find-executable v command (tramp-get-remote-path v) t)))
00d6fd04
MA
4491
4492;; We use BUFFER also as connection buffer during setup. Because of
4493;; this, its original contents must be saved, and restored once
4494;; connection has been setup.
4495(defun tramp-handle-start-file-process (name buffer program &rest args)
4496 "Like `start-file-process' for Tramp files."
4497 (with-parsed-tramp-file-name default-directory nil
9fb2cdc5
GM
4498 (unless (stringp program)
4499 (tramp-error
4500 v 'file-error "pty association is not supported for `%s'" name))
00d6fd04 4501 (unwind-protect
263c02ef
MA
4502 (let ((command (format "cd %s; exec %s"
4503 (tramp-shell-quote-argument localname)
4504 (mapconcat 'tramp-shell-quote-argument
4505 (cons program args) " ")))
4506 (name1 name)
38d63e6a 4507 (i 0))
2296b54d 4508 (unless buffer
6ce63faf 4509 ;; BUFFER can be nil. We use a temporary buffer.
2296b54d 4510 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
38d63e6a
MA
4511 (while (get-process name1)
4512 ;; NAME must be unique as process name.
4513 (setq i (1+ i)
4514 name1 (format "%s<%d>" name i)))
4515 (setq name name1)
00d6fd04
MA
4516 ;; Set the new process properties.
4517 (tramp-set-connection-property v "process-name" name)
2296b54d 4518 (tramp-set-connection-property v "process-buffer" buffer)
00d6fd04 4519 ;; Activate narrowing in order to save BUFFER contents.
11c71217
MA
4520 ;; Clear also the modification time; otherwise we might be
4521 ;; interrupted by `verify-visited-file-modtime'.
00d6fd04 4522 (with-current-buffer (tramp-get-connection-buffer v)
11c71217 4523 (clear-visited-file-modtime)
00d6fd04 4524 (narrow-to-region (point-max) (point-max)))
263c02ef 4525 ;; Send the command. `tramp-send-command' opens a new
00d6fd04 4526 ;; connection.
263c02ef 4527 (tramp-send-command v command nil t) ; nooutput
6ce63faf
MA
4528 ;; Set query flag for this process.
4529 (tramp-set-process-query-on-exit-flag
4530 (tramp-get-connection-process v) t)
00d6fd04
MA
4531 ;; Return process.
4532 (tramp-get-connection-process v))
4533 ;; Save exit.
ce3f516f 4534 (with-current-buffer (tramp-get-connection-buffer v)
6ce63faf
MA
4535 (if (string-match tramp-temp-buffer-name (buffer-name))
4536 (progn
4537 (set-process-buffer (tramp-get-connection-process v) nil)
4538 (kill-buffer (current-buffer)))
4539 (widen)
4540 (goto-char (point-max))))
00d6fd04
MA
4541 (tramp-set-connection-property v "process-name" nil)
4542 (tramp-set-connection-property v "process-buffer" nil))))
4543
4544(defun tramp-handle-process-file
4545 (program &optional infile destination display &rest args)
4546 "Like `process-file' for Tramp files."
4547 ;; The implementation is not complete yet.
4548 (when (and (numberp destination) (zerop destination))
4549 (error "Implementation does not handle immediate return"))
4550
4551 (with-parsed-tramp-file-name default-directory nil
a6e96327 4552 (let (command input tmpinput stderr tmpstderr outbuf ret)
00d6fd04
MA
4553 ;; Compute command.
4554 (setq command (mapconcat 'tramp-shell-quote-argument
4555 (cons program args) " "))
4556 ;; Determine input.
4557 (if (null infile)
4558 (setq input "/dev/null")
4559 (setq infile (expand-file-name infile))
4560 (if (tramp-equal-remote default-directory infile)
4561 ;; INFILE is on the same remote host.
4562 (setq input (with-parsed-tramp-file-name infile nil localname))
4563 ;; INFILE must be copied to remote host.
a6e96327
MA
4564 (setq input (tramp-make-tramp-temp-file v)
4565 tmpinput (tramp-make-tramp-file-name method user host input))
4566 (copy-file infile tmpinput t)))
00d6fd04
MA
4567 (when input (setq command (format "%s <%s" command input)))
4568
4569 ;; Determine output.
4570 (cond
bede3e9f 4571 ;; Just a buffer.
00d6fd04
MA
4572 ((bufferp destination)
4573 (setq outbuf destination))
bede3e9f 4574 ;; A buffer name.
00d6fd04
MA
4575 ((stringp destination)
4576 (setq outbuf (get-buffer-create destination)))
4577 ;; (REAL-DESTINATION ERROR-DESTINATION)
4578 ((consp destination)
bede3e9f 4579 ;; output.
00d6fd04
MA
4580 (cond
4581 ((bufferp (car destination))
4582 (setq outbuf (car destination)))
4583 ((stringp (car destination))
0664ff72
MA
4584 (setq outbuf (get-buffer-create (car destination))))
4585 ((car destination)
4586 (setq outbuf (current-buffer))))
bede3e9f 4587 ;; stderr.
00d6fd04
MA
4588 (cond
4589 ((stringp (cadr destination))
4590 (setcar (cdr destination) (expand-file-name (cadr destination)))
4591 (if (tramp-equal-remote default-directory (cadr destination))
4592 ;; stderr is on the same remote host.
4593 (setq stderr (with-parsed-tramp-file-name
4594 (cadr destination) nil localname))
4595 ;; stderr must be copied to remote host. The temporary
4596 ;; file must be deleted after execution.
a6e96327
MA
4597 (setq stderr (tramp-make-tramp-temp-file v)
4598 tmpstderr (tramp-make-tramp-file-name
4599 method user host stderr))))
bede3e9f 4600 ;; stderr to be discarded.
00d6fd04
MA
4601 ((null (cadr destination))
4602 (setq stderr "/dev/null"))))
4603 ;; 't
4604 (destination
4605 (setq outbuf (current-buffer))))
4606 (when stderr (setq command (format "%s 2>%s" command stderr)))
4607
00d6fd04
MA
4608 ;; Send the command. It might not return in time, so we protect it.
4609 (condition-case nil
4610 (unwind-protect
293c24f9
MA
4611 (setq ret
4612 (tramp-send-command-and-check
4613 v (format "\\cd %s; %s"
4614 (tramp-shell-quote-argument localname)
b593f105
MA
4615 command)
4616 nil t))
00d6fd04
MA
4617 ;; We should show the output anyway.
4618 (when outbuf
293c24f9
MA
4619 (with-current-buffer outbuf
4620 (insert
4621 (with-current-buffer (tramp-get-connection-buffer v)
4622 (buffer-string))))
00d6fd04 4623 (when display (display-buffer outbuf))))
260821d3
MA
4624 ;; When the user did interrupt, we should do it also. We use
4625 ;; return code -1 as marker.
4626 (quit
4627 (kill-buffer (tramp-get-connection-buffer v))
4628 (setq ret -1))
4629 ;; Handle errors.
00d6fd04
MA
4630 (error
4631 (kill-buffer (tramp-get-connection-buffer v))
4632 (setq ret 1)))
a6e96327 4633
a6e96327
MA
4634 ;; Provide error file.
4635 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
263c02ef 4636
260821d3
MA
4637 ;; Cleanup. We remove all file cache values for the connection,
4638 ;; because the remote process could have changed them.
eba082a2 4639 (when tmpinput (delete-file tmpinput))
946a5aeb
MA
4640
4641 ;; `process-file-side-effects' has been introduced with GNU
4642 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4643 ;; by `program'. If it doesn't exist, we assume its default
4644 ;; value 't'.
4645 (unless (and (boundp 'process-file-side-effects)
4646 (not (symbol-value 'process-file-side-effects)))
4647 (tramp-flush-directory-property v ""))
4648
00d6fd04 4649 ;; Return exit status.
260821d3
MA
4650 (if (equal ret -1)
4651 (keyboard-quit)
4652 ret))))
00d6fd04 4653
a4aeb9a4
MA
4654(defun tramp-local-call-process
4655 (program &optional infile destination display &rest args)
4656 "Calls `call-process' on the local host.
4657This is needed because for some Emacs flavors Tramp has
4658defadviced `call-process' to behave like `process-file'. The
4659Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4660 (let ((default-directory
4661 (if (file-remote-p default-directory)
4662 (tramp-compat-temporary-file-directory)
4663 default-directory)))
4664 (if (executable-find program)
4665 (apply 'call-process program infile destination display args)
4666 1)))
4667
00d6fd04
MA
4668(defun tramp-handle-call-process-region
4669 (start end program &optional delete buffer display &rest args)
4670 "Like `call-process-region' for Tramp files."
258800f8 4671 (let ((tmpfile (tramp-compat-make-temp-file "")))
00d6fd04
MA
4672 (write-region start end tmpfile)
4673 (when delete (delete-region start end))
4674 (unwind-protect
4675 (apply 'call-process program tmpfile buffer display args)
eba082a2 4676 (delete-file tmpfile))))
00d6fd04
MA
4677
4678(defun tramp-handle-shell-command
4679 (command &optional output-buffer error-buffer)
4680 "Like `shell-command' for Tramp files."
ce3f516f 4681 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
b8bfcf96
MA
4682 ;; We cannot use `shell-file-name' and `shell-command-switch',
4683 ;; they are variables of the local host.
4684 (args (list "/bin/sh" "-c" (substring command 0 asynchronous)))
5d2ebd96 4685 current-buffer-p
ce3f516f 4686 (output-buffer
27e813fe
MA
4687 (cond
4688 ((bufferp output-buffer) output-buffer)
4689 ((stringp output-buffer) (get-buffer-create output-buffer))
5d2ebd96
AS
4690 (output-buffer
4691 (setq current-buffer-p t)
4692 (current-buffer))
42bc9b6d 4693 (t (get-buffer-create
27e813fe
MA
4694 (if asynchronous
4695 "*Async Shell Command*"
4696 "*Shell Command Output*")))))
4697 (error-buffer
4698 (cond
4699 ((bufferp error-buffer) error-buffer)
4700 ((stringp error-buffer) (get-buffer-create error-buffer))))
ce3f516f 4701 (buffer
27e813fe 4702 (if (and (not asynchronous) error-buffer)
ce3f516f
MA
4703 (with-parsed-tramp-file-name default-directory nil
4704 (list output-buffer (tramp-make-tramp-temp-file v)))
42bc9b6d 4705 output-buffer))
cfb5c0db 4706 (p (get-buffer-process output-buffer)))
42bc9b6d
MA
4707
4708 ;; Check whether there is another process running. Tramp does not
4709 ;; support 2 (asynchronous) processes in parallel.
cfb5c0db 4710 (when p
42bc9b6d 4711 (if (yes-or-no-p "A command is running. Kill it? ")
699a11fb
GM
4712 (condition-case nil
4713 (kill-process p)
4714 (error nil))
42bc9b6d
MA
4715 (error "Shell command in progress")))
4716
f34db316
AS
4717 (if current-buffer-p
4718 (progn
4719 (barf-if-buffer-read-only)
4720 (push-mark nil t))
5d2ebd96
AS
4721 (with-current-buffer output-buffer
4722 (setq buffer-read-only nil)
4723 (erase-buffer)))
42bc9b6d 4724
5d2ebd96 4725 (if (and (not current-buffer-p) (integerp asynchronous))
42bc9b6d
MA
4726 (prog1
4727 ;; Run the process.
3412f35d 4728 (apply 'start-file-process "*Async Shell*" buffer args)
42bc9b6d 4729 ;; Display output.
cfb5c0db
MA
4730 (pop-to-buffer output-buffer)
4731 (setq mode-line-process '(":%s"))
4732 (require 'shell) (shell-mode))
42bc9b6d
MA
4733
4734 (prog1
4735 ;; Run the process.
4736 (apply 'process-file (car args) nil buffer nil (cdr args))
4737 ;; Insert error messages if they were separated.
4738 (when (listp buffer)
4739 (with-current-buffer error-buffer
4740 (insert-file-contents (cadr buffer)))
eba082a2 4741 (delete-file (cadr buffer)))
f34db316
AS
4742 (if current-buffer-p
4743 ;; This is like exchange-point-and-mark, but doesn't
4744 ;; activate the mark. It is cleaner to avoid activation,
4745 ;; even though the command loop would deactivate the mark
4746 ;; because we inserted text.
4747 (goto-char (prog1 (mark t)
4748 (set-marker (mark-marker) (point)
4749 (current-buffer))))
4750 ;; There's some output, display it.
4751 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4752 (if (functionp 'display-message-or-buffer)
0d5852cf 4753 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
f34db316 4754 (pop-to-buffer output-buffer))))))))
00d6fd04
MA
4755
4756;; File Editing.
4757
4758(defvar tramp-handle-file-local-copy-hook nil
4759 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4760
fb7933a3 4761(defun tramp-handle-file-local-copy (filename)
00d6fd04 4762 "Like `file-local-copy' for Tramp files."
0f205eee 4763
c62c9d08 4764 (with-parsed-tramp-file-name filename nil
2988341a
MA
4765 (unless (file-exists-p filename)
4766 (tramp-error
4767 v 'file-error
4768 "Cannot make local copy of non-existing file `%s'" filename))
4769
6a29a838
MA
4770 (let* ((size (nth 7 (file-attributes filename)))
4771 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
4772 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
4773 (tmpfile (tramp-compat-make-temp-file filename)))
5ec2cc41 4774
2988341a
MA
4775 (condition-case err
4776 (cond
4777 ;; `copy-file' handles direct copy and out-of-band methods.
4778 ((or (tramp-local-host-p v)
6a29a838 4779 (tramp-method-out-of-band-p v size))
2988341a
MA
4780 (copy-file filename tmpfile t t))
4781
4782 ;; Use inline encoding for file transfer.
4783 (rem-enc
4784 (save-excursion
3b30ccda 4785 (with-progress-reporter
655bded0 4786 v 3 (format "Encoding remote file %s" filename)
3b30ccda
MA
4787 (tramp-barf-unless-okay
4788 v (format rem-enc (tramp-shell-quote-argument localname))
4789 "Encoding remote file failed"))
2988341a 4790
6a29a838 4791 (if (functionp loc-dec)
2988341a
MA
4792 ;; If local decoding is a function, we call it. We
4793 ;; must disable multibyte, because
4794 ;; `uudecode-decode-region' doesn't handle it
4795 ;; correctly.
0f205eee
MA
4796 (with-temp-buffer
4797 (set-buffer-multibyte nil)
4798 (insert-buffer-substring (tramp-get-buffer v))
3b30ccda
MA
4799 (with-progress-reporter
4800 v 3 (format "Decoding remote file %s with function %s"
4801 filename loc-dec)
4802 (funcall loc-dec (point-min) (point-max))
4803 ;; Unset `file-name-handler-alist'. Otherwise,
4804 ;; epa-file gets confused.
4805 (let (file-name-handler-alist
4806 (coding-system-for-write 'binary))
4807 (write-region (point-min) (point-max) tmpfile))))
2988341a
MA
4808
4809 ;; If tramp-decoding-function is not defined for this
4810 ;; method, we invoke tramp-decoding-command instead.
4811 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
aa485f7c
MA
4812 ;; Unset `file-name-handler-alist'. Otherwise,
4813 ;; epa-file gets confused.
4814 (let (file-name-handler-alist
4815 (coding-system-for-write 'binary))
2988341a 4816 (write-region (point-min) (point-max) tmpfile2))
3b30ccda
MA
4817 (with-progress-reporter
4818 v 3 (format "Decoding remote file %s with command %s"
4819 filename loc-dec)
4820 (unwind-protect
4821 (tramp-call-local-coding-command
4822 loc-dec tmpfile2 tmpfile)
eba082a2 4823 (delete-file tmpfile2)))))
2988341a 4824
2988341a 4825 ;; Set proper permissions.
b86c1cd8 4826 (set-file-modes tmpfile (tramp-default-file-modes filename))
2988341a
MA
4827 ;; Set local user ownership.
4828 (tramp-set-file-uid-gid tmpfile)))
4829
4830 ;; Oops, I don't know what to do.
4831 (t (tramp-error
4832 v 'file-error "Wrong method specification for `%s'" method)))
4833
4834 ;; Error handling.
4835 ((error quit)
eba082a2 4836 (delete-file tmpfile)
2988341a 4837 (signal (car err) (cdr err))))
0f205eee 4838
00d6fd04 4839 (run-hooks 'tramp-handle-file-local-copy-hook)
94be87e8 4840 tmpfile)))
fb7933a3 4841
bce04fee 4842(defun tramp-handle-file-remote-p (filename &optional identification connected)
00d6fd04 4843 "Like `file-remote-p' for Tramp files."
d5b3979c
MA
4844 (let ((tramp-verbose 3))
4845 (when (tramp-tramp-file-p filename)
4846 (let* ((v (tramp-dissect-file-name filename))
4847 (p (tramp-get-connection-process v))
4848 (c (and p (processp p) (memq (process-status p) '(run open)))))
4849 ;; We expand the file name only, if there is already a connection.
4850 (with-parsed-tramp-file-name
4851 (if c (expand-file-name filename) filename) nil
4852 (and (or (not connected) c)
4853 (cond
4854 ((eq identification 'method) method)
4855 ((eq identification 'user) user)
4856 ((eq identification 'host) host)
4857 ((eq identification 'localname) localname)
4858 (t (tramp-make-tramp-file-name method user host "")))))))))
fb7933a3 4859
eb562962
MA
4860(defun tramp-find-file-name-coding-system-alist (filename tmpname)
4861 "Like `find-operation-coding-system' for Tramp filenames.
4862Tramp's `insert-file-contents' and `write-region' work over
4863temporary file names. If `file-coding-system-alist' contains an
4864expression, which matches more than the file name suffix, the
4865coding system might not be determined. This function repairs it."
4866 (let (result)
4867 (dolist (elt file-coding-system-alist result)
4868 (when (and (consp elt) (string-match (car elt) filename))
4869 ;; We found a matching entry in `file-coding-system-alist'.
4870 ;; So we add a similar entry, but with the temporary file name
4871 ;; as regexp.
4872 (add-to-list
4873 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
4874
fb7933a3
KG
4875(defun tramp-handle-insert-file-contents
4876 (filename &optional visit beg end replace)
00d6fd04 4877 "Like `insert-file-contents' for Tramp files."
fb7933a3
KG
4878 (barf-if-buffer-read-only)
4879 (setq filename (expand-file-name filename))
3b30ccda 4880 (let (result local-copy remote-copy)
2ac33804
MA
4881 (with-parsed-tramp-file-name filename nil
4882 (unwind-protect
70c11b0b
MA
4883 (if (not (file-exists-p filename))
4884 ;; We don't raise a Tramp error, because it might be
4885 ;; suppressed, like in `find-file-noselect-1'.
4886 (signal 'file-error
4887 (list "File not found on remote host" filename))
4888
4889 (if (and (tramp-local-host-p v)
4890 (let (file-name-handler-alist)
4891 (file-readable-p localname)))
4892 ;; Short track: if we are on the local host, we can
4893 ;; run directly.
4894 (setq result
4895 (tramp-run-real-handler
4896 'insert-file-contents
4897 (list localname visit beg end replace)))
4898
736ac90f
MA
4899 ;; When we shall insert only a part of the file, we copy
4900 ;; this part.
4901 (when (or beg end)
4902 (setq remote-copy (tramp-make-tramp-temp-file v))
4903 (tramp-send-command
4904 v
4905 (cond
4906 ((and beg end)
4907 (format "tail -c +%d %s | head -c +%d >%s"
4908 (1+ beg) (tramp-shell-quote-argument localname)
4909 (- end beg) remote-copy))
4910 (beg
4911 (format "tail -c +%d %s >%s"
4912 (1+ beg) (tramp-shell-quote-argument localname)
4913 remote-copy))
4914 (end
4915 (format "head -c +%d %s >%s"
4916 (1+ end) (tramp-shell-quote-argument localname)
4917 remote-copy)))))
4918
70c11b0b
MA
4919 ;; `insert-file-contents-literally' takes care to avoid
4920 ;; calling jka-compr. By let-binding
4921 ;; `inhibit-file-name-operation', we propagate that care
4922 ;; to the `file-local-copy' operation.
4923 (setq local-copy
4924 (let ((inhibit-file-name-operation
4925 (when (eq inhibit-file-name-operation
4926 'insert-file-contents)
4927 'file-local-copy)))
b88f2d0a
MA
4928 (cond
4929 ((stringp remote-copy)
4930 (file-local-copy
4931 (tramp-make-tramp-file-name
4932 method user host remote-copy)))
4933 ((stringp tramp-temp-buffer-file-name)
4934 (copy-file filename tramp-temp-buffer-file-name 'ok)
4935 tramp-temp-buffer-file-name)
4936 (t (file-local-copy filename)))))
4937
6e4f5731
MA
4938 ;; When the file is not readable for the owner, it
4939 ;; cannot be inserted, even it is redable for the group
4940 ;; or for everybody.
4941 (set-file-modes local-copy (tramp-octal-to-decimal "0600"))
4942
b88f2d0a
MA
4943 (when (and (null remote-copy)
4944 (tramp-get-method-parameter
4945 method 'tramp-copy-keep-tmpfile))
4946 ;; We keep the local file for performance reasons,
4947 ;; useful for "rsync".
b88f2d0a
MA
4948 (setq tramp-temp-buffer-file-name local-copy)
4949 (put 'tramp-temp-buffer-file-name 'permanent-local t))
4950
3b30ccda
MA
4951 (with-progress-reporter
4952 v 3 (format "Inserting local temp file `%s'" local-copy)
4953 ;; We must ensure that `file-coding-system-alist'
4954 ;; matches `local-copy'.
4955 (let ((file-coding-system-alist
4956 (tramp-find-file-name-coding-system-alist
4957 filename local-copy)))
4958 (setq result
4959 (insert-file-contents
4960 local-copy nil nil nil replace))))))
70c11b0b 4961
2ac33804
MA
4962 ;; Save exit.
4963 (progn
4964 (when visit
4965 (setq buffer-file-name filename)
4966 (setq buffer-read-only (not (file-writable-p filename)))
4967 (set-visited-file-modtime)
4968 (set-buffer-modified-p nil))
b88f2d0a
MA
4969 (when (and (stringp local-copy)
4970 (or remote-copy (null tramp-temp-buffer-file-name)))
eba082a2 4971 (delete-file local-copy))
2ac33804 4972 (when (stringp remote-copy)
eba082a2 4973 (delete-file
f1a5d776 4974 (tramp-make-tramp-file-name method user host remote-copy))))))
70c11b0b
MA
4975
4976 ;; Result.
4977 (list (expand-file-name filename)
4978 (cadr result))))
fb7933a3 4979
94be87e8
MA
4980;; This is needed for XEmacs only. Code stolen from files.el.
4981(defun tramp-handle-insert-file-contents-literally
4982 (filename &optional visit beg end replace)
4983 "Like `insert-file-contents-literally' for Tramp files."
4984 (let ((format-alist nil)
4985 (after-insert-file-functions nil)
4986 (coding-system-for-read 'no-conversion)
4987 (coding-system-for-write 'no-conversion)
4988 (find-buffer-file-type-function
4989 (if (fboundp 'find-buffer-file-type)
4990 (symbol-function 'find-buffer-file-type)
4991 nil))
4992 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
4993 (inhibit-file-name-operation 'insert-file-contents))
4994 (unwind-protect
4995 (progn
4996 (fset 'find-buffer-file-type (lambda (filename) t))
4997 (insert-file-contents filename visit beg end replace))
70c11b0b 4998 ;; Save exit.
94be87e8
MA
4999 (if find-buffer-file-type-function
5000 (fset 'find-buffer-file-type find-buffer-file-type-function)
5001 (fmakunbound 'find-buffer-file-type)))))
5002
38c65fca 5003(defun tramp-handle-find-backup-file-name (filename)
00d6fd04 5004 "Like `find-backup-file-name' for Tramp files."
07dfe738
KG
5005 (with-parsed-tramp-file-name filename nil
5006 ;; We set both variables. It doesn't matter whether it is
b533bc97 5007 ;; Emacs or XEmacs.
07dfe738 5008 (let ((backup-directory-alist
b533bc97 5009 ;; Emacs case.
07dfe738 5010 (when (boundp 'backup-directory-alist)
b86c1cd8 5011 (if (symbol-value 'tramp-backup-directory-alist)
07dfe738 5012 (mapcar
aa485f7c
MA
5013 (lambda (x)
5014 (cons
5015 (car x)
5016 (if (and (stringp (cdr x))
5017 (file-name-absolute-p (cdr x))
5018 (not (tramp-file-name-p (cdr x))))
5019 (tramp-make-tramp-file-name method user host (cdr x))
5020 (cdr x))))
07dfe738
KG
5021 (symbol-value 'tramp-backup-directory-alist))
5022 (symbol-value 'backup-directory-alist))))
5023
5024 (bkup-backup-directory-info
b533bc97 5025 ;; XEmacs case.
07dfe738 5026 (when (boundp 'bkup-backup-directory-info)
b86c1cd8 5027 (if (symbol-value 'tramp-bkup-backup-directory-info)
07dfe738 5028 (mapcar
aa485f7c
MA
5029 (lambda (x)
5030 (nconc
5031 (list (car x))
5032 (list
5033 (if (and (stringp (car (cdr x)))
5034 (file-name-absolute-p (car (cdr x)))
5035 (not (tramp-file-name-p (car (cdr x)))))
5036 (tramp-make-tramp-file-name
5037 method user host (car (cdr x)))
5038 (car (cdr x))))
5039 (cdr (cdr x))))
07dfe738
KG
5040 (symbol-value 'tramp-bkup-backup-directory-info))
5041 (symbol-value 'bkup-backup-directory-info)))))
5042
5043 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
38c65fca 5044
c1105d05 5045(defun tramp-handle-make-auto-save-file-name ()
00d6fd04 5046 "Like `make-auto-save-file-name' for Tramp files.
c1105d05 5047Returns a file name in `tramp-auto-save-directory' for autosaving this file."
00d6fd04
MA
5048 (let ((tramp-auto-save-directory tramp-auto-save-directory)
5049 (buffer-file-name
5050 (tramp-subst-strs-in-string
5051 '(("_" . "|")
5052 ("/" . "_a")
5053 (":" . "_b")
5054 ("|" . "__")
5055 ("[" . "_l")
5056 ("]" . "_r"))
5057 (buffer-file-name))))
1a762140
MA
5058 ;; File name must be unique. This is ensured with Emacs 22 (see
5059 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5060 ;; all other cases we must do it ourselves.
5061 (when (boundp 'auto-save-file-name-transforms)
9e6ab520 5062 (mapc
aa485f7c
MA
5063 (lambda (x)
5064 (when (and (string-match (car x) buffer-file-name)
5065 (not (car (cddr x))))
5066 (setq tramp-auto-save-directory
5067 (or tramp-auto-save-directory
5068 (tramp-compat-temporary-file-directory)))))
1a762140
MA
5069 (symbol-value 'auto-save-file-name-transforms)))
5070 ;; Create directory.
5071 (when tramp-auto-save-directory
00d6fd04
MA
5072 (setq buffer-file-name
5073 (expand-file-name buffer-file-name tramp-auto-save-directory))
1a762140
MA
5074 (unless (file-exists-p tramp-auto-save-directory)
5075 (make-directory tramp-auto-save-directory t)))
00d6fd04
MA
5076 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5077 ;; it is not a magic file name operation (since Emacs 22).
5078 ;; We must deactivate it temporarily.
5079 (if (not (ad-is-active 'make-auto-save-file-name))
5080 (tramp-run-real-handler 'make-auto-save-file-name nil)
5081 ;; else
5082 (ad-deactivate 'make-auto-save-file-name)
5083 (prog1
5084 (tramp-run-real-handler 'make-auto-save-file-name nil)
5085 (ad-activate 'make-auto-save-file-name)))))
5086
5087(defvar tramp-handle-write-region-hook nil
5088 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5089
b88f2d0a 5090;; CCC grok LOCKNAME
fb7933a3
KG
5091(defun tramp-handle-write-region
5092 (start end filename &optional append visit lockname confirm)
00d6fd04 5093 "Like `write-region' for Tramp files."
fb7933a3 5094 (setq filename (expand-file-name filename))
c62c9d08 5095 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
5096 ;; Following part commented out because we don't know what to do about
5097 ;; file locking, and it does not appear to be a problem to ignore it.
5098 ;; Ange-ftp ignores it, too.
5099 ;; (when (and lockname (stringp lockname))
5100 ;; (setq lockname (expand-file-name lockname)))
5101 ;; (unless (or (eq lockname nil)
5102 ;; (string= lockname filename))
5103 ;; (error
5104 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
8d60099b 5105
94be87e8 5106 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
00d6fd04
MA
5107 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
5108 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
5109 (tramp-error v 'file-error "File not overwritten")))
8d60099b 5110
a4aeb9a4 5111 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
9c13938d 5112 (tramp-get-remote-uid v 'integer)))
a4aeb9a4 5113 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
9c13938d
MA
5114 (tramp-get-remote-gid v 'integer))))
5115
5116 (if (and (tramp-local-host-p v)
93c3eb7c 5117 ;; `file-writable-p' calls `file-expand-file-name'. We
87bdd2c7
MA
5118 ;; cannot use `tramp-run-real-handler' therefore.
5119 (let (file-name-handler-alist)
82f3844e
MA
5120 (and
5121 (file-writable-p (file-name-directory localname))
5122 (or (file-directory-p localname)
5123 (file-writable-p localname)))))
9c13938d 5124 ;; Short track: if we are on the local host, we can run directly.
aac0b0f2
MA
5125 (tramp-run-real-handler
5126 'write-region
5127 (list start end localname append 'no-message lockname confirm))
9c13938d 5128
6a29a838 5129 (let ((modes (save-excursion (tramp-default-file-modes filename)))
9c13938d 5130 ;; We use this to save the value of
6a29a838
MA
5131 ;; `last-coding-system-used' after writing the tmp
5132 ;; file. At the end of the function, we set
9c13938d
MA
5133 ;; `last-coding-system-used' to this saved value. This
5134 ;; way, any intermediary coding systems used while
5135 ;; talking to the remote shell or suchlike won't hose
5136 ;; this variable. This approach was snarfed from
5137 ;; ange-ftp.el.
5138 coding-system-used
5139 ;; Write region into a tmp file. This isn't really
5140 ;; needed if we use an encoding function, but currently
5141 ;; we use it always because this makes the logic
293c24f9
MA
5142 ;; simpler.
5143 (tmpfile (or tramp-temp-buffer-file-name
5144 (tramp-compat-make-temp-file filename))))
5145
5146 ;; If `append' is non-nil, we copy the file locally, and let
5147 ;; the native `write-region' implementation do the job.
5148 (when append (copy-file filename tmpfile 'ok))
9c13938d
MA
5149
5150 ;; We say `no-message' here because we don't want the
5151 ;; visited file modtime data to be clobbered from the temp
5152 ;; file. We call `set-visited-file-modtime' ourselves later
eb562962
MA
5153 ;; on. We must ensure that `file-coding-system-alist'
5154 ;; matches `tmpfile'.
6a29a838
MA
5155 (let (file-name-handler-alist
5156 (file-coding-system-alist
eb562962 5157 (tramp-find-file-name-coding-system-alist filename tmpfile)))
ce2cc728
MA
5158 (condition-case err
5159 (tramp-run-real-handler
5160 'write-region
5161 (list start end tmpfile append 'no-message lockname confirm))
2988341a 5162 ((error quit)
b88f2d0a 5163 (setq tramp-temp-buffer-file-name nil)
eba082a2 5164 (delete-file tmpfile)
2988341a 5165 (signal (car err) (cdr err))))
ce2cc728 5166
eb562962
MA
5167 ;; Now, `last-coding-system-used' has the right value. Remember it.
5168 (when (boundp 'last-coding-system-used)
5169 (setq coding-system-used
5170 (symbol-value 'last-coding-system-used))))
5171
9c13938d
MA
5172 ;; The permissions of the temporary file should be set. If
5173 ;; filename does not exist (eq modes nil) it has been
5174 ;; renamed to the backup file. This case `save-buffer'
5175 ;; handles permissions.
459a5f4b
MA
5176 ;; Ensure, that it is still readable.
5177 (when modes
5178 (set-file-modes
5179 tmpfile (logior (or modes 0) (tramp-octal-to-decimal "0400"))))
9c13938d
MA
5180
5181 ;; This is a bit lengthy due to the different methods
5182 ;; possible for file transfer. First, we check whether the
5183 ;; method uses an rcp program. If so, we call it.
5184 ;; Otherwise, both encoding and decoding command must be
5185 ;; specified. However, if the method _also_ specifies an
5186 ;; encoding function, then that is used for encoding the
5187 ;; contents of the tmp file.
6a29a838
MA
5188 (let* ((size (nth 7 (file-attributes tmpfile)))
5189 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
5190 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
5191 (cond
5192 ;; `copy-file' handles direct copy and out-of-band methods.
5193 ((or (tramp-local-host-p v)
5194 (tramp-method-out-of-band-p v size))
5195 (if (and (not (stringp start))
5196 (= (or end (point-max)) (point-max))
5197 (= (or start (point-min)) (point-min))
5198 (tramp-get-method-parameter
5199 method 'tramp-copy-keep-tmpfile))
5200 (progn
5201 (setq tramp-temp-buffer-file-name tmpfile)
5202 (condition-case err
5203 ;; We keep the local file for performance
5204 ;; reasons, useful for "rsync".
5205 (copy-file tmpfile filename t)
5206 ((error quit)
5207 (setq tramp-temp-buffer-file-name nil)
eba082a2 5208 (delete-file tmpfile)
6a29a838
MA
5209 (signal (car err) (cdr err)))))
5210 (setq tramp-temp-buffer-file-name nil)
5211 ;; Don't rename, in order to keep context in SELinux.
5212 (unwind-protect
5213 (copy-file tmpfile filename t)
eba082a2 5214 (delete-file tmpfile))))
6a29a838
MA
5215
5216 ;; Use inline file transfer.
5217 (rem-dec
5218 ;; Encode tmpfile.
191bb792 5219 (unwind-protect
6a29a838
MA
5220 (with-temp-buffer
5221 (set-buffer-multibyte nil)
5222 ;; Use encoding function or command.
5223 (if (functionp loc-enc)
3b30ccda
MA
5224 (with-progress-reporter
5225 v 3 (format "Encoding region using function `%s'"
5226 loc-enc)
6a29a838
MA
5227 (let ((coding-system-for-read 'binary))
5228 (insert-file-contents-literally tmpfile))
5229 ;; The following `let' is a workaround for the
5230 ;; base64.el that comes with pgnus-0.84. If
5231 ;; both of the following conditions are
5232 ;; satisfied, it tries to write to a local
5233 ;; file in default-directory, but at this
5234 ;; point, default-directory is remote.
5235 ;; (`call-process-region' can't write to
5236 ;; remote files, it seems.) The file in
5237 ;; question is a tmp file anyway.
5238 (let ((default-directory
5239 (tramp-compat-temporary-file-directory)))
5240 (funcall loc-enc (point-min) (point-max))))
5241
3b30ccda
MA
5242 (with-progress-reporter
5243 v 3 (format "Encoding region using command `%s'"
5244 loc-enc)
5245 (unless (zerop (tramp-call-local-coding-command
5246 loc-enc tmpfile t))
5247 (tramp-error
5248 v 'file-error
5249 (concat "Cannot write to `%s', "
5250 "local encoding command `%s' failed")
5251 filename loc-enc))))
6a29a838
MA
5252
5253 ;; Send buffer into remote decoding command which
5254 ;; writes to remote file. Because this happens on
5255 ;; the remote host, we cannot use the function.
3b30ccda
MA
5256 (with-progress-reporter
5257 v 3
5258 (format "Decoding region into remote file %s" filename)
5259 (goto-char (point-max))
5260 (unless (bolp) (newline))
5261 (tramp-send-command
5262 v
5263 (format
5264 (concat rem-dec " <<'EOF'\n%sEOF")
5265 (tramp-shell-quote-argument localname)
5266 (buffer-string)))
5267 (tramp-barf-unless-okay
5268 v nil
5269 "Couldn't write region to `%s', decode using `%s' failed"
5270 filename rem-dec)
5271 ;; When `file-precious-flag' is set, the region is
5272 ;; written to a temporary file. Check that the
5273 ;; checksum is equal to that from the local tmpfile.
5274 (when file-precious-flag
5275 (erase-buffer)
5276 (and
5277 ;; cksum runs locally, if possible.
5278 (zerop (tramp-local-call-process "cksum" tmpfile t))
5279 ;; cksum runs remotely.
5280 (zerop
5281 (tramp-send-command-and-check
5282 v
5283 (format
5284 "cksum <%s"
5285 (tramp-shell-quote-argument localname))))
5286 ;; ... they are different.
5287 (not
5288 (string-equal
5289 (buffer-string)
5290 (with-current-buffer (tramp-get-buffer v)
5291 (buffer-string))))
5292 (tramp-error
5293 v 'file-error
5294 (concat "Couldn't write region to `%s',"
5295 " decode using `%s' failed")
5296 filename rem-dec)))))
8d60099b 5297
6a29a838 5298 ;; Save exit.
eba082a2 5299 (delete-file tmpfile)))
8d60099b 5300
6a29a838
MA
5301 ;; That's not expected.
5302 (t
5303 (tramp-error
5304 v 'file-error
5305 (concat "Method `%s' should specify both encoding and "
5306 "decoding command or an rcp program")
5307 method))))
258800f8 5308
9c13938d
MA
5309 ;; Make `last-coding-system-used' have the right value.
5310 (when coding-system-used
5311 (set 'last-coding-system-used coding-system-used))))
0f205eee 5312
aac0b0f2
MA
5313 (tramp-flush-file-property v (file-name-directory localname))
5314 (tramp-flush-file-property v localname)
5315
57671b72
MA
5316 ;; We must protect `last-coding-system-used', now we have set it
5317 ;; to its correct value.
293c24f9 5318 (let (last-coding-system-used (need-chown t))
57671b72
MA
5319 ;; Set file modification time.
5320 (when (or (eq visit t) (stringp visit))
293c24f9
MA
5321 (let ((file-attr (file-attributes filename)))
5322 (set-visited-file-modtime
5323 ;; We must pass modtime explicitely, because filename can
5324 ;; be different from (buffer-file-name), f.e. if
5325 ;; `file-precious-flag' is set.
5326 (nth 5 file-attr))
5327 (when (and (eq (nth 2 file-attr) uid)
5328 (eq (nth 3 file-attr) gid))
5329 (setq need-chown nil))))
57671b72
MA
5330
5331 ;; Set the ownership.
293c24f9
MA
5332 (when need-chown
5333 (tramp-set-file-uid-gid filename uid gid))
57671b72
MA
5334 (when (or (eq visit t) (null visit) (stringp visit))
5335 (tramp-message v 0 "Wrote %s" filename))
5336 (run-hooks 'tramp-handle-write-region-hook)))))
fb7933a3 5337
946a5aeb
MA
5338(defvar tramp-vc-registered-file-names nil
5339 "List used to collect file names, which are checked during `vc-registered'.")
5340
5341;; VC backends check for the existence of various different special
5342;; files. This is very time consuming, because every single check
5343;; requires a remote command (the file cache must be invalidated).
5344;; Therefore, we apply a kind of optimization. We install the file
5345;; name handler `tramp-vc-file-name-handler', which does nothing but
5346;; remembers all file names for which `file-exists-p' or
5347;; `file-readable-p' has been applied. A first run of `vc-registered'
5348;; is performed. Afterwards, a script is applied for all collected
5349;; file names, using just one remote command. The result of this
5350;; script is used to fill the file cache with actual values. Now we
5351;; can reset the file name handlers, and we make a second run of
5352;; `vc-registered', which returns the expected result without sending
5353;; any other remote command.
49096407
MA
5354(defun tramp-handle-vc-registered (file)
5355 "Like `vc-registered' for Tramp files."
655bded0
MA
5356 (with-temp-message ""
5357 (with-parsed-tramp-file-name file nil
5358 (with-progress-reporter
5359 v 3 (format "Checking `vc-registered' for %s" file)
5360
5361 ;; There could be new files, created by the vc backend. We
5362 ;; cannot reuse the old cache entries, therefore.
5363 (let (tramp-vc-registered-file-names
5364 (tramp-cache-inhibit-cache (current-time))
5365 (file-name-handler-alist
5366 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5367
5368 ;; Here we collect only file names, which need an operation.
5369 (tramp-run-real-handler 'vc-registered (list file))
5370 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5371
5372 ;; Send just one command, in order to fill the cache.
5373 (when tramp-vc-registered-file-names
5374 (tramp-maybe-send-script
5375 v
5376 (format tramp-vc-registered-read-file-names
5377 (tramp-get-file-exists-command v)
5378 (format "%s -r" (tramp-get-test-command v)))
5379 "tramp_vc_registered_read_file_names")
5380
5381 (dolist
5382 (elt
5383 (tramp-send-command-and-read
5384 v
5385 (format
5386 "tramp_vc_registered_read_file_names %s"
5387 (mapconcat 'tramp-shell-quote-argument
5388 tramp-vc-registered-file-names
5389 " "))))
5390
5391 (tramp-set-file-property
5392 v (car elt) (cadr elt) (cadr (cdr elt))))))
5393
5394 ;; Second run. Now all `file-exists-p' or `file-readable-p'
5395 ;; calls shall be answered from the file cache. We unset
5396 ;; `process-file-side-effects' in order to keep the cache when
5397 ;; `process-file' calls appear.
5398 (let (process-file-side-effects)
5399 (tramp-run-real-handler 'vc-registered (list file)))))))
49096407 5400
a01b1e22
MA
5401;;;###autoload
5402(progn (defun tramp-run-real-handler (operation args)
fb7933a3 5403 "Invoke normal file name handler for OPERATION.
c62c9d08
KG
5404First arg specifies the OPERATION, second arg is a list of arguments to
5405pass to the OPERATION."
4007ba5b
KG
5406 (let* ((inhibit-file-name-handlers
5407 `(tramp-file-name-handler
946a5aeb 5408 tramp-vc-file-name-handler
4007ba5b
KG
5409 tramp-completion-file-name-handler
5410 cygwin-mount-name-hook-function
5411 cygwin-mount-map-drive-hook-function
5412 .
5413 ,(and (eq inhibit-file-name-operation operation)
5414 inhibit-file-name-handlers)))
5415 (inhibit-file-name-operation operation))
a01b1e22 5416 (apply operation args))))
16674e4f 5417
a01b1e22
MA
5418;;;###autoload
5419(progn (defun tramp-completion-run-real-handler (operation args)
16674e4f
KG
5420 "Invoke `tramp-file-name-handler' for OPERATION.
5421First arg specifies the OPERATION, second arg is a list of arguments to
5422pass to the OPERATION."
4007ba5b
KG
5423 (let* ((inhibit-file-name-handlers
5424 `(tramp-completion-file-name-handler
5425 cygwin-mount-name-hook-function
5426 cygwin-mount-map-drive-hook-function
5427 .
5428 ,(and (eq inhibit-file-name-operation operation)
5429 inhibit-file-name-handlers)))
5430 (inhibit-file-name-operation operation))
a01b1e22 5431 (apply operation args))))
fb7933a3 5432
4007ba5b
KG
5433;; We handle here all file primitives. Most of them have the file
5434;; name as first parameter; nevertheless we check for them explicitly
04bf5b65 5435;; in order to be signaled if a new primitive appears. This
4007ba5b
KG
5436;; scenario is needed because there isn't a way to decide by
5437;; syntactical means whether a foreign method must be called. It would
19a87064 5438;; ease the life if `file-name-handler-alist' would support a decision
4007ba5b
KG
5439;; function as well but regexp only.
5440(defun tramp-file-name-for-operation (operation &rest args)
5441 "Return file name related to OPERATION file primitive.
5442ARGS are the arguments OPERATION has been called with."
5443 (cond
b533bc97 5444 ;; FILE resp DIRECTORY.
4007ba5b
KG
5445 ((member operation
5446 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
5447 'delete-file 'diff-latest-backup-file 'directory-file-name
5448 'directory-files 'directory-files-and-attributes
5449 'dired-compress-file 'dired-uncache
5450 'file-accessible-directory-p 'file-attributes
5451 'file-directory-p 'file-executable-p 'file-exists-p
25f14cdb 5452 'file-local-copy 'file-remote-p 'file-modes
19a87064
MA
5453 'file-name-as-directory 'file-name-directory
5454 'file-name-nondirectory 'file-name-sans-versions
5455 'file-ownership-preserved-p 'file-readable-p
5456 'file-regular-p 'file-symlink-p 'file-truename
5457 'file-writable-p 'find-backup-file-name 'find-file-noselect
5458 'get-file-buffer 'insert-directory 'insert-file-contents
5459 'load 'make-directory 'make-directory-internal
5460 'set-file-modes 'substitute-in-file-name
5461 'unhandled-file-name-directory 'vc-registered
b533bc97 5462 ;; Emacs 22+ only.
ce3f516f 5463 'set-file-times
25f14cdb
MA
5464 ;; Emacs 24+ only.
5465 'file-selinux-context 'set-file-selinux-context
b533bc97 5466 ;; XEmacs only.
4007ba5b
KG
5467 'abbreviate-file-name 'create-file-buffer
5468 'dired-file-modtime 'dired-make-compressed-filename
5469 'dired-recursive-delete-directory 'dired-set-file-modtime
5470 'dired-shell-unhandle-file-name 'dired-uucode-file
5615d63f 5471 'insert-file-contents-literally 'make-temp-name 'recover-file
4007ba5b 5472 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
8daea7fc
KG
5473 (if (file-name-absolute-p (nth 0 args))
5474 (nth 0 args)
5475 (expand-file-name (nth 0 args))))
b533bc97 5476 ;; FILE DIRECTORY resp FILE1 FILE2.
4007ba5b
KG
5477 ((member operation
5478 (list 'add-name-to-file 'copy-file 'expand-file-name
5479 'file-name-all-completions 'file-name-completion
5480 'file-newer-than-file-p 'make-symbolic-link 'rename-file
b533bc97 5481 ;; Emacs 23+ only.
263c02ef 5482 'copy-directory
b533bc97 5483 ;; XEmacs only.
4007ba5b
KG
5484 'dired-make-relative-symlink
5485 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
5486 (save-match-data
5487 (cond
5488 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
5489 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
5490 (t (buffer-file-name (current-buffer))))))
b533bc97 5491 ;; START END FILE.
4007ba5b
KG
5492 ((eq operation 'write-region)
5493 (nth 2 args))
b533bc97 5494 ;; BUFFER.
4007ba5b 5495 ((member operation
00d6fd04 5496 (list 'set-visited-file-modtime 'verify-visited-file-modtime
b533bc97 5497 ;; Emacs 22+ only.
00d6fd04 5498 'make-auto-save-file-name
b533bc97 5499 ;; XEmacs only.
4007ba5b
KG
5500 'backup-buffer))
5501 (buffer-file-name
5502 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
b533bc97 5503 ;; COMMAND.
4007ba5b 5504 ((member operation
b533bc97 5505 (list ;; not in Emacs 23+.
00d6fd04 5506 'dired-call-process
b533bc97 5507 ;; Emacs only.
b71c9e75 5508 'shell-command
b533bc97 5509 ;; Emacs 22+ only.
0457dd55 5510 'process-file
b533bc97 5511 ;; Emacs 23+ only.
00d6fd04 5512 'start-file-process
b533bc97 5513 ;; XEmacs only.
00d6fd04 5514 'dired-print-file 'dired-shell-call-process
b533bc97 5515 ;; nowhere yet.
00d6fd04 5516 'executable-find 'start-process 'call-process))
4007ba5b 5517 default-directory)
b533bc97 5518 ;; Unknown file primitive.
4007ba5b
KG
5519 (t (error "unknown file I/O primitive: %s" operation))))
5520
5521(defun tramp-find-foreign-file-name-handler (filename)
5522 "Return foreign file name handler if exists."
b533bc97 5523 (when (tramp-tramp-file-p filename)
9ce8462a
MA
5524 (let ((v (tramp-dissect-file-name filename t))
5525 (handler tramp-foreign-file-name-handler-alist)
5526 elt res)
5527 ;; When we are not fully sure that filename completion is safe,
5528 ;; we should not return a handler.
5529 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
1834b39f
MA
5530 (and (tramp-file-name-host v)
5531 (not (member (tramp-file-name-host v)
5532 (mapcar 'car tramp-methods))))
9ce8462a
MA
5533 (not (tramp-completion-mode-p)))
5534 (while handler
5535 (setq elt (car handler)
5536 handler (cdr handler))
5537 (when (funcall (car elt) filename)
5538 (setq handler nil
5539 res (cdr elt))))
5540 res))))
4007ba5b 5541
fb7933a3
KG
5542;; Main function.
5543;;;###autoload
5544(defun tramp-file-name-handler (operation &rest args)
ea9d1443 5545 "Invoke Tramp file name handler.
a4aeb9a4 5546Falls back to normal file name handler if no Tramp file name handler exists."
2e271195
MA
5547 (if tramp-mode
5548 (save-match-data
5549 (let* ((filename
5550 (tramp-replace-environment-variables
5551 (apply 'tramp-file-name-for-operation operation args)))
5552 (completion (tramp-completion-mode-p))
5553 (foreign (tramp-find-foreign-file-name-handler filename)))
5554 (with-parsed-tramp-file-name filename nil
4874f5e6
MA
5555 ;; Call the backend function.
5556 (if foreign
5557 (condition-case err
5558 (apply foreign operation args)
5559 (error
5560 (cond
5561 ;; When we are in completion mode, some failed
5562 ;; operations shall return at least a default
5563 ;; value in order to give the user a chance to
5564 ;; correct the file name in the minibuffer.
5565 ((and completion (zerop (length localname))
5566 (memq operation '(file-exists-p file-directory-p)))
5567 t)
5568 ((and completion (zerop (length localname))
5569 (memq operation
5570 '(expand-file-name file-name-as-directory)))
5571 filename)
5572 ;; Propagate the error.
5573 (t (signal (car err) (cdr err))))))
5574 ;; Nothing to do for us.
5575 (tramp-run-real-handler operation args)))))
5576
2e271195
MA
5577 ;; When `tramp-mode' is not enabled, we don't do anything.
5578 (tramp-run-real-handler operation args)))
fb7933a3 5579
07dfe738
KG
5580;; In Emacs, there is some concurrency due to timers. If a timer
5581;; interrupts Tramp and wishes to use the same connection buffer as
5582;; the "main" Emacs, then garbage might occur in the connection
5583;; buffer. Therefore, we need to make sure that a timer does not use
5584;; the same connection buffer as the "main" Emacs. We implement a
5585;; cheap global lock, instead of locking each connection buffer
5586;; separately. The global lock is based on two variables,
5587;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5588;; (with setq) to indicate a lock. But Tramp also calls itself during
5589;; processing of a single file operation, so we need to allow
5590;; recursive calls. That's where the `tramp-locker' variable comes in
5591;; -- it is let-bound to t during the execution of the current
5592;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5593;; then we should just proceed because we have been called
5594;; recursively. But if `tramp-locker' is nil, then we are a timer
5595;; interrupting the "main" Emacs, and then we signal an error.
5596
5597(defvar tramp-locked nil
5598 "If non-nil, then Tramp is currently busy.
5599Together with `tramp-locker', this implements a locking mechanism
5600preventing reentrant calls of Tramp.")
5601
5602(defvar tramp-locker nil
5603 "If non-nil, then a caller has locked Tramp.
5604Together with `tramp-locked', this implements a locking mechanism
5605preventing reentrant calls of Tramp.")
5606
ea9d1443
KG
5607(defun tramp-sh-file-name-handler (operation &rest args)
5608 "Invoke remote-shell Tramp file name handler.
5609Fall back to normal file name handler if no Tramp handler exists."
07dfe738 5610 (when (and tramp-locked (not tramp-locker))
11c71217 5611 (setq tramp-locked nil)
00d6fd04 5612 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
07dfe738
KG
5613 (let ((tl tramp-locked))
5614 (unwind-protect
5615 (progn
5616 (setq tramp-locked t)
5617 (let ((tramp-locker t))
5618 (save-match-data
5619 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5620 (if fn
5621 (apply (cdr fn) args)
5622 (tramp-run-real-handler operation args))))))
5623 (setq tramp-locked tl))))
ea9d1443 5624
946a5aeb
MA
5625(defun tramp-vc-file-name-handler (operation &rest args)
5626 "Invoke special file name handler, which collects files to be handled."
5627 (save-match-data
5628 (let ((filename
5629 (tramp-replace-environment-variables
5630 (apply 'tramp-file-name-for-operation operation args)))
5631 (fn (assoc operation tramp-file-name-handler-alist)))
5632 (with-parsed-tramp-file-name filename nil
5633 (cond
5634 ;; That's what we want: file names, for which checks are
5635 ;; applied. We assume, that VC uses only `file-exists-p' and
5636 ;; `file-readable-p' checks; otherwise we must extend the
5637 ;; list. We do not perform any action, but return nil, in
5638 ;; order to keep `vc-registered' running.
5639 ((and fn (memq operation '(file-exists-p file-readable-p)))
5640 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5641 nil)
5642 ;; Tramp file name handlers like `expand-file-name'. They
5643 ;; must still work.
5644 (fn
5645 (save-match-data (apply (cdr fn) args)))
5646 ;; Default file name handlers, we don't care.
5647 (t (tramp-run-real-handler operation args)))))))
5648
16674e4f 5649;;;###autoload
1ecc6145 5650(progn (defun tramp-completion-file-name-handler (operation &rest args)
a4aeb9a4
MA
5651 "Invoke Tramp file name completion handler.
5652Falls back to normal file name handler if no Tramp file name handler exists."
57671b72
MA
5653 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5654 ;; would otherwise use backslash.
aff67808
MA
5655 (let ((directory-sep-char ?/)
5656 (fn (assoc operation tramp-completion-file-name-handler-alist)))
aa485f7c
MA
5657 (if (and
5658 ;; When `tramp-mode' is not enabled, we don't do anything.
5659 fn tramp-mode
5660 ;; For other syntaxes than `sep', the regexp matches many common
5661 ;; situations where the user doesn't actually want to use Tramp.
5662 ;; So to avoid autoloading Tramp after typing just "/s", we
5663 ;; disable this part of the completion, unless the user implicitly
5664 ;; indicated his interest in using a fancier completion system.
5665 (or (eq tramp-syntax 'sep)
a94d821f
MA
5666 (featurep 'tramp) ;; If it's loaded, we may as well use it.
5667 ;; `partial-completion-mode' does not exist in XEmacs.
5668 ;; It is obsoleted with Emacs 24.1.
0d5852cf
MA
5669 (and (boundp 'partial-completion-mode)
5670 (symbol-value 'partial-completion-mode))
aa485f7c
MA
5671 ;; FIXME: These may have been loaded even if the user never
5672 ;; intended to use them.
5673 (featurep 'ido)
5674 (featurep 'icicles)))
aff67808
MA
5675 (save-match-data (apply (cdr fn) args))
5676 (tramp-completion-run-real-handler operation args)))))
a01b1e22 5677
b25a52cc 5678;;;###autoload
aa485f7c
MA
5679(progn (defun tramp-register-file-name-handlers ()
5680 "Add Tramp file name handlers to `file-name-handler-alist'."
5681 ;; Remove autoloaded handlers from file name handler alist. Useful,
00d6fd04
MA
5682 ;; if `tramp-syntax' has been changed.
5683 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
aa485f7c
MA
5684 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5685 (let ((a1 (rassq
5686 'tramp-completion-file-name-handler file-name-handler-alist)))
5687 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5688 ;; Add the handlers.
a01b1e22
MA
5689 (add-to-list 'file-name-handler-alist
5690 (cons tramp-file-name-regexp 'tramp-file-name-handler))
0c0b61f1 5691 (put 'tramp-file-name-handler 'safe-magic t)
aa485f7c
MA
5692 (add-to-list 'file-name-handler-alist
5693 (cons tramp-completion-file-name-regexp
5694 'tramp-completion-file-name-handler))
5695 (put 'tramp-completion-file-name-handler 'safe-magic t)
5696 ;; If jka-compr or epa-file are already loaded, move them to the
5697 ;; front of `file-name-handler-alist'.
5698 (dolist (fnh '(epa-file-handler jka-compr-handler))
5699 (let ((entry (rassoc fnh file-name-handler-alist)))
5700 (when entry
5701 (setq file-name-handler-alist
5702 (cons entry (delete entry file-name-handler-alist))))))))
69cee873 5703
00d6fd04
MA
5704;; `tramp-file-name-handler' must be registered before evaluation of
5705;; site-start and init files, because there might exist remote files
5706;; already, f.e. files kept via recentf-mode.
aa485f7c
MA
5707;;;###autoload(tramp-register-file-name-handlers)
5708(tramp-register-file-name-handlers)
b25a52cc 5709
fb7933a3 5710;;;###autoload
8c04e197 5711(defun tramp-unload-file-name-handlers ()
a69c01a0
MA
5712 (setq file-name-handler-alist
5713 (delete (rassoc 'tramp-file-name-handler
5714 file-name-handler-alist)
5715 (delete (rassoc 'tramp-completion-file-name-handler
5716 file-name-handler-alist)
5717 file-name-handler-alist))))
5718
8c04e197 5719(add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
a69c01a0 5720
0664ff72 5721;;; File name handler functions for completion mode:
a6e96327
MA
5722
5723(defvar tramp-completion-mode nil
5724 "If non-nil, external packages signal that they are in file name completion.
5725
5726This is necessary, because Tramp uses a heuristic depending on last
5727input event. This fails when external packages use other characters
5728but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5729should never be set globally, the intention is to let-bind it.")
16674e4f
KG
5730
5731;; Necessary because `tramp-file-name-regexp-unified' and
00d6fd04
MA
5732;; `tramp-completion-file-name-regexp-unified' aren't different. If
5733;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5734;; to `tramp-file-name-handler'). Otherwise, it takes
5735;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5736;; risky, because completing a file might require loading other files,
5737;; like "~/.netrc", and for them it shouldn't be decided based on that
5738;; variable. On the other hand, those files shouldn't have partial
a4aeb9a4
MA
5739;; Tramp file name syntax. Maybe another variable should be introduced
5740;; overwriting this check in such cases. Or we change Tramp file name
00d6fd04 5741;; syntax in order to avoid ambiguities, like in XEmacs ...
6c4e47fa 5742(defun tramp-completion-mode-p ()
a94d821f 5743 "Check, whether method / user name / host name completion is active."
6c4e47fa 5744 (or
5f2b693f
MA
5745 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
5746 (and (boundp 'non-essential) (symbol-value 'non-essential))
a6e96327
MA
5747 tramp-completion-mode
5748 ;; Emacs.
94be87e8 5749 (equal last-input-event 'tab)
6c4e47fa 5750 (and (natnump last-input-event)
94be87e8 5751 (or
a6e96327 5752 ;; ?\t has event-modifier 'control.
800a97b8 5753 (equal last-input-event ?\t)
94be87e8 5754 (and (not (event-modifiers last-input-event))
800a97b8
SM
5755 (or (equal last-input-event ?\?)
5756 (equal last-input-event ?\ )))))
a6e96327 5757 ;; XEmacs.
6c4e47fa
MA
5758 (and (featurep 'xemacs)
5759 ;; `last-input-event' might be nil.
5760 (not (null last-input-event))
5761 ;; `last-input-event' may have no character approximation.
0d5852cf 5762 (tramp-compat-funcall 'event-to-character last-input-event)
94be87e8 5763 (or
a6e96327 5764 ;; ?\t has event-modifier 'control.
800a97b8 5765 (equal
0d5852cf 5766 (tramp-compat-funcall 'event-to-character last-input-event) ?\t)
94be87e8 5767 (and (not (event-modifiers last-input-event))
800a97b8 5768 (or (equal
0d5852cf
MA
5769 (tramp-compat-funcall 'event-to-character last-input-event)
5770 ?\?)
800a97b8 5771 (equal
0d5852cf
MA
5772 (tramp-compat-funcall 'event-to-character last-input-event)
5773 ?\ )))))))
16674e4f 5774
acd1f317
MA
5775(defun tramp-connectable-p (filename)
5776 "Check, whether it is possible to connect the remote host w/o side-effects.
5777This is true, if either the remote host is already connected, or if we are
5778not in completion mode."
5779 (and (tramp-tramp-file-p filename)
5780 (with-parsed-tramp-file-name filename nil
5781 (or (get-buffer (tramp-buffer-name v))
5782 (not (tramp-completion-mode-p))))))
5783
16674e4f
KG
5784;; Method, host name and user name completion.
5785;; `tramp-completion-dissect-file-name' returns a list of
5786;; tramp-file-name structures. For all of them we return possible completions.
a01b1e22 5787;;;###autoload
16674e4f 5788(defun tramp-completion-handle-file-name-all-completions (filename directory)
00d6fd04 5789 "Like `file-name-all-completions' for partial Tramp files."
16674e4f 5790
00d6fd04
MA
5791 (let* ((fullname (tramp-drop-volume-letter
5792 (expand-file-name filename directory)))
5793 ;; Possible completion structures.
5794 (v (tramp-completion-dissect-file-name fullname))
5795 result result1)
5796
5797 (while v
5798 (let* ((car (car v))
5799 (method (tramp-file-name-method car))
5800 (user (tramp-file-name-user car))
5801 (host (tramp-file-name-host car))
5802 (localname (tramp-file-name-localname car))
5803 (m (tramp-find-method method user host))
5804 (tramp-current-user user) ; see `tramp-parse-passwd'
5805 all-user-hosts)
5806
5807 (unless localname ;; Nothing to complete.
5808
5809 (if (or user host)
5810
5811 ;; Method dependent user / host combinations.
5812 (progn
9e6ab520 5813 (mapc
00d6fd04
MA
5814 (lambda (x)
5815 (setq all-user-hosts
5816 (append all-user-hosts
5817 (funcall (nth 0 x) (nth 1 x)))))
5818 (tramp-get-completion-function m))
5819
9e6ab520
MA
5820 (setq result
5821 (append result
5822 (mapcar
5823 (lambda (x)
5824 (tramp-get-completion-user-host
5825 method user host (nth 0 x) (nth 1 x)))
5826 (delq nil all-user-hosts)))))
00d6fd04
MA
5827
5828 ;; Possible methods.
5829 (setq result
5830 (append result (tramp-get-completion-methods m)))))
5831
5832 (setq v (cdr v))))
5833
5834 ;; Unify list, remove nil elements.
5835 (while result
5836 (let ((car (car result)))
5837 (when car
5838 (add-to-list
5839 'result1
5840 (substring car (length (tramp-drop-volume-letter directory)))))
5841 (setq result (cdr result))))
5842
5843 ;; Complete local parts.
5844 (append
5845 result1
5846 (condition-case nil
489fe4c2
MA
5847 (apply (if (tramp-connectable-p fullname)
5848 'tramp-completion-run-real-handler
5849 'tramp-run-real-handler)
5850 'file-name-all-completions (list (list filename directory)))
00d6fd04 5851 (error nil)))))
16674e4f
KG
5852
5853;; Method, host name and user name completion for a file.
a01b1e22 5854;;;###autoload
e1e17cae
MA
5855(defun tramp-completion-handle-file-name-completion
5856 (filename directory &optional predicate)
00d6fd04 5857 "Like `file-name-completion' for Tramp files."
e1e17cae
MA
5858 (try-completion
5859 filename
5860 (mapcar 'list (file-name-all-completions filename directory))
acd1f317
MA
5861 (when (and predicate
5862 (tramp-connectable-p (expand-file-name filename directory)))
83e20b5c 5863 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
16674e4f
KG
5864
5865;; I misuse a little bit the tramp-file-name structure in order to handle
5866;; completion possibilities for partial methods / user names / host names.
5867;; Return value is a list of tramp-file-name structures according to possible
00d6fd04 5868;; completions. If "localname" is non-nil it means there
16674e4f
KG
5869;; shouldn't be a completion anymore.
5870
5871;; Expected results:
5872
00d6fd04
MA
5873;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5874;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5875;; [nil "x" nil nil]
5876;; ["x" nil nil nil]
5877
5878;; "/x:" "/x:y" "/x:y:"
5879;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5880;; "/[x/" "/[x/y"
5881;; ["x" nil "" nil] ["x" nil "y" nil]
5882;; ["x" "" nil nil] ["x" "y" nil nil]
5883
5884;; "/x:y@" "/x:y@z" "/x:y@z:"
5885;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5886;; "/[x/y@" "/[x/y@z"
5887;; ["x" nil "y" nil] ["x" "y" "z" nil]
16674e4f
KG
5888(defun tramp-completion-dissect-file-name (name)
5889 "Returns a list of `tramp-file-name' structures.
5890They are collected by `tramp-completion-dissect-file-name1'."
5891
5892 (let* ((result)
4007ba5b 5893 (x-nil "\\|\\(\\)")
b96e6899
MA
5894 (tramp-completion-ipv6-regexp
5895 (format
5896 "[^%s]*"
5897 (if (zerop (length tramp-postfix-ipv6-format))
5898 tramp-postfix-host-format
5899 tramp-postfix-ipv6-format)))
4007ba5b
KG
5900 ;; "/method" "/[method"
5901 (tramp-completion-file-name-structure1
5902 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
5903 1 nil nil nil))
5904 ;; "/user" "/[user"
5905 (tramp-completion-file-name-structure2
5906 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
5907 nil 1 nil nil))
5908 ;; "/host" "/[host"
5909 (tramp-completion-file-name-structure3
5910 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
5911 nil nil 1 nil))
b96e6899 5912 ;; "/[ipv6" "/[ipv6"
4007ba5b 5913 (tramp-completion-file-name-structure4
b96e6899
MA
5914 (list (concat tramp-prefix-regexp
5915 tramp-prefix-ipv6-regexp
5916 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5917 nil nil 1 nil))
5918 ;; "/user@host" "/[user@host"
5919 (tramp-completion-file-name-structure5
4007ba5b
KG
5920 (list (concat tramp-prefix-regexp
5921 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5922 "\\(" tramp-host-regexp x-nil "\\)$")
5923 nil 1 2 nil))
b96e6899
MA
5924 ;; "/user@[ipv6" "/[user@ipv6"
5925 (tramp-completion-file-name-structure6
5926 (list (concat tramp-prefix-regexp
5927 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5928 tramp-prefix-ipv6-regexp
5929 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5930 nil 1 2 nil))
00d6fd04 5931 ;; "/method:user" "/[method/user" "/method://user"
b96e6899 5932 (tramp-completion-file-name-structure7
4007ba5b 5933 (list (concat tramp-prefix-regexp
00d6fd04 5934 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
5935 "\\(" tramp-user-regexp x-nil "\\)$")
5936 1 2 nil nil))
00d6fd04 5937 ;; "/method:host" "/[method/host" "/method://host"
b96e6899 5938 (tramp-completion-file-name-structure8
4007ba5b 5939 (list (concat tramp-prefix-regexp
00d6fd04 5940 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
5941 "\\(" tramp-host-regexp x-nil "\\)$")
5942 1 nil 2 nil))
b96e6899
MA
5943 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5944 (tramp-completion-file-name-structure9
5945 (list (concat tramp-prefix-regexp
5946 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5947 tramp-prefix-ipv6-regexp
5948 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5949 1 nil 2 nil))
00d6fd04 5950 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
b96e6899 5951 (tramp-completion-file-name-structure10
4007ba5b 5952 (list (concat tramp-prefix-regexp
00d6fd04 5953 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
5954 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5955 "\\(" tramp-host-regexp x-nil "\\)$")
00d6fd04 5956 1 2 3 nil))
b96e6899
MA
5957 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5958 (tramp-completion-file-name-structure11
5959 (list (concat tramp-prefix-regexp
5960 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5961 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5962 tramp-prefix-ipv6-regexp
5963 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5964 1 2 3 nil))
00d6fd04 5965 ;; "/method: "/method:/"
b96e6899 5966 (tramp-completion-file-name-structure12
00d6fd04
MA
5967 (list
5968 (if (equal tramp-syntax 'url)
5969 (concat tramp-prefix-regexp
5970 "\\(" tramp-method-regexp "\\)"
5971 "\\(" (substring tramp-postfix-method-regexp 0 1)
5972 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5973 "\\(" "\\)$")
5974 ;; Should not match if not URL syntax.
5975 (concat tramp-prefix-regexp "/$"))
5976 1 3 nil nil))
5977 ;; "/method: "/method:/"
b96e6899 5978 (tramp-completion-file-name-structure13
00d6fd04
MA
5979 (list
5980 (if (equal tramp-syntax 'url)
5981 (concat tramp-prefix-regexp
5982 "\\(" tramp-method-regexp "\\)"
5983 "\\(" (substring tramp-postfix-method-regexp 0 1)
5984 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5985 "\\(" "\\)$")
5986 ;; Should not match if not URL syntax.
5987 (concat tramp-prefix-regexp "/$"))
5988 1 nil 3 nil)))
4007ba5b 5989
9e6ab520 5990 (mapc (lambda (regexp)
16674e4f
KG
5991 (add-to-list 'result
5992 (tramp-completion-dissect-file-name1 regexp name)))
5993 (list
5994 tramp-completion-file-name-structure1
5995 tramp-completion-file-name-structure2
5996 tramp-completion-file-name-structure3
5997 tramp-completion-file-name-structure4
5998 tramp-completion-file-name-structure5
5999 tramp-completion-file-name-structure6
6000 tramp-completion-file-name-structure7
00d6fd04
MA
6001 tramp-completion-file-name-structure8
6002 tramp-completion-file-name-structure9
b96e6899
MA
6003 tramp-completion-file-name-structure10
6004 tramp-completion-file-name-structure11
6005 tramp-completion-file-name-structure12
6006 tramp-completion-file-name-structure13
16674e4f
KG
6007 tramp-file-name-structure))
6008
6009 (delq nil result)))
6010
6011(defun tramp-completion-dissect-file-name1 (structure name)
6012 "Returns a `tramp-file-name' structure matching STRUCTURE.
00d6fd04 6013The structure consists of remote method, remote user,
7432277c 6014remote host and localname (filename on remote host)."
fb7933a3 6015
00d6fd04
MA
6016 (save-match-data
6017 (when (string-match (nth 0 structure) name)
6018 (let ((method (and (nth 1 structure)
6019 (match-string (nth 1 structure) name)))
6020 (user (and (nth 2 structure)
6021 (match-string (nth 2 structure) name)))
6022 (host (and (nth 3 structure)
6023 (match-string (nth 3 structure) name)))
6024 (localname (and (nth 4 structure)
6025 (match-string (nth 4 structure) name))))
6026 (vector method user host localname)))))
16674e4f
KG
6027
6028;; This function returns all possible method completions, adding the
6029;; trailing method delimeter.
16674e4f
KG
6030(defun tramp-get-completion-methods (partial-method)
6031 "Returns all method completions for PARTIAL-METHOD."
4007ba5b
KG
6032 (mapcar
6033 (lambda (method)
6034 (and method
6035 (string-match (concat "^" (regexp-quote partial-method)) method)
00d6fd04
MA
6036 (tramp-completion-make-tramp-file-name method nil nil nil)))
6037 (mapcar 'car tramp-methods)))
16674e4f
KG
6038
6039;; Compares partial user and host names with possible completions.
6040(defun tramp-get-completion-user-host (method partial-user partial-host user host)
6041 "Returns the most expanded string for user and host name completion.
6042PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
6043 (cond
6044
6045 ((and partial-user partial-host)
6046 (if (and host
6047 (string-match (concat "^" (regexp-quote partial-host)) host)
6048 (string-equal partial-user (or user partial-user)))
6049 (setq user partial-user)
6050 (setq user nil
6051 host nil)))
6052
6053 (partial-user
6054 (setq host nil)
6055 (unless
6056 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
6057 (setq user nil)))
6058
6059 (partial-host
6060 (setq user nil)
6061 (unless
6062 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
6063 (setq host nil)))
6064
6065 (t (setq user nil
6066 host nil)))
6067
292ffc15 6068 (unless (zerop (+ (length user) (length host)))
00d6fd04 6069 (tramp-completion-make-tramp-file-name method user host nil)))
16674e4f
KG
6070
6071(defun tramp-parse-rhosts (filename)
6072 "Return a list of (user host) tuples allowed to access.
292ffc15 6073Either user or host may be nil."
00d6fd04
MA
6074 ;; On Windows, there are problems in completion when
6075 ;; `default-directory' is remote.
9e6ab520 6076 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6077 res)
8daea7fc 6078 (when (file-readable-p filename)
16674e4f
KG
6079 (with-temp-buffer
6080 (insert-file-contents filename)
6081 (goto-char (point-min))
6082 (while (not (eobp))
292ffc15 6083 (push (tramp-parse-rhosts-group) res))))
16674e4f
KG
6084 res))
6085
16674e4f
KG
6086(defun tramp-parse-rhosts-group ()
6087 "Return a (user host) tuple allowed to access.
292ffc15 6088Either user or host may be nil."
16674e4f
KG
6089 (let ((result)
6090 (regexp
6091 (concat
6092 "^\\(" tramp-host-regexp "\\)"
6093 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
9e6ab520 6094 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f
KG
6095 (when (re-search-forward regexp nil t)
6096 (setq result (append (list (match-string 3) (match-string 1)))))
6097 (widen)
6098 (forward-line 1)
6099 result))
6100
6101(defun tramp-parse-shosts (filename)
6102 "Return a list of (user host) tuples allowed to access.
6103User is always nil."
00d6fd04
MA
6104 ;; On Windows, there are problems in completion when
6105 ;; `default-directory' is remote.
9e6ab520 6106 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6107 res)
8daea7fc 6108 (when (file-readable-p filename)
16674e4f
KG
6109 (with-temp-buffer
6110 (insert-file-contents filename)
6111 (goto-char (point-min))
6112 (while (not (eobp))
292ffc15 6113 (push (tramp-parse-shosts-group) res))))
16674e4f
KG
6114 res))
6115
6116(defun tramp-parse-shosts-group ()
6117 "Return a (user host) tuple allowed to access.
6118User is always nil."
16674e4f
KG
6119 (let ((result)
6120 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
9e6ab520 6121 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f
KG
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
8daea7fc
KG
6130(defun tramp-parse-sconfig (filename)
6131 "Return a list of (user host) tuples allowed to access.
6132User is always nil."
00d6fd04
MA
6133 ;; On Windows, there are problems in completion when
6134 ;; `default-directory' is remote.
9e6ab520 6135 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6136 res)
8daea7fc
KG
6137 (when (file-readable-p filename)
6138 (with-temp-buffer
6139 (insert-file-contents filename)
6140 (goto-char (point-min))
6141 (while (not (eobp))
6142 (push (tramp-parse-sconfig-group) res))))
6143 res))
6144
6145(defun tramp-parse-sconfig-group ()
6146 "Return a (user host) tuple allowed to access.
6147User is always nil."
8daea7fc
KG
6148 (let ((result)
6149 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
9e6ab520 6150 (narrow-to-region (point) (tramp-compat-line-end-position))
8daea7fc
KG
6151 (when (re-search-forward regexp nil t)
6152 (setq result (list nil (match-string 1))))
6153 (widen)
6154 (or
6155 (> (skip-chars-forward ",") 0)
6156 (forward-line 1))
6157 result))
6158
5ec2cc41
KG
6159(defun tramp-parse-shostkeys (dirname)
6160 "Return a list of (user host) tuples allowed to access.
6161User is always nil."
00d6fd04
MA
6162 ;; On Windows, there are problems in completion when
6163 ;; `default-directory' is remote.
9e6ab520 6164 (let* ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
6165 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
6166 (files (when (file-directory-p dirname) (directory-files dirname)))
6167 result)
5ec2cc41
KG
6168 (while files
6169 (when (string-match regexp (car files))
6170 (push (list nil (match-string 1 (car files))) result))
6171 (setq files (cdr files)))
6172 result))
6173
6174(defun tramp-parse-sknownhosts (dirname)
6175 "Return a list of (user host) tuples allowed to access.
6176User is always nil."
00d6fd04
MA
6177 ;; On Windows, there are problems in completion when
6178 ;; `default-directory' is remote.
9e6ab520 6179 (let* ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
6180 (regexp (concat "^\\(" tramp-host-regexp
6181 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
6182 (files (when (file-directory-p dirname) (directory-files dirname)))
6183 result)
5ec2cc41
KG
6184 (while files
6185 (when (string-match regexp (car files))
6186 (push (list nil (match-string 1 (car files))) result))
6187 (setq files (cdr files)))
6188 result))
6189
16674e4f
KG
6190(defun tramp-parse-hosts (filename)
6191 "Return a list of (user host) tuples allowed to access.
6192User is always nil."
00d6fd04
MA
6193 ;; On Windows, there are problems in completion when
6194 ;; `default-directory' is remote.
9e6ab520 6195 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6196 res)
8daea7fc 6197 (when (file-readable-p filename)
16674e4f
KG
6198 (with-temp-buffer
6199 (insert-file-contents filename)
6200 (goto-char (point-min))
6201 (while (not (eobp))
292ffc15 6202 (push (tramp-parse-hosts-group) res))))
16674e4f
KG
6203 res))
6204
6205(defun tramp-parse-hosts-group ()
6206 "Return a (user host) tuple allowed to access.
6207User is always nil."
16674e4f 6208 (let ((result)
b96e6899
MA
6209 (regexp
6210 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
9e6ab520 6211 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f 6212 (when (re-search-forward regexp nil t)
b96e6899 6213 (setq result (list nil (match-string 1))))
16674e4f
KG
6214 (widen)
6215 (or
6216 (> (skip-chars-forward " \t") 0)
6217 (forward-line 1))
6218 result))
6219
8daea7fc
KG
6220;; For su-alike methods it would be desirable to return "root@localhost"
6221;; as default. Unfortunately, we have no information whether any user name
00d6fd04 6222;; has been typed already. So we use `tramp-current-user' as indication,
8daea7fc 6223;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
16674e4f
KG
6224(defun tramp-parse-passwd (filename)
6225 "Return a list of (user host) tuples allowed to access.
6226Host is always \"localhost\"."
00d6fd04
MA
6227 ;; On Windows, there are problems in completion when
6228 ;; `default-directory' is remote.
9e6ab520 6229 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6230 res)
8daea7fc 6231 (if (zerop (length tramp-current-user))
16674e4f 6232 '(("root" nil))
8daea7fc 6233 (when (file-readable-p filename)
16674e4f
KG
6234 (with-temp-buffer
6235 (insert-file-contents filename)
6236 (goto-char (point-min))
6237 (while (not (eobp))
292ffc15 6238 (push (tramp-parse-passwd-group) res))))
16674e4f
KG
6239 res)))
6240
6241(defun tramp-parse-passwd-group ()
6242 "Return a (user host) tuple allowed to access.
292ffc15 6243Host is always \"localhost\"."
16674e4f
KG
6244 (let ((result)
6245 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
9e6ab520 6246 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f
KG
6247 (when (re-search-forward regexp nil t)
6248 (setq result (list (match-string 1) "localhost")))
6249 (widen)
6250 (forward-line 1)
6251 result))
6252
292ffc15
KG
6253(defun tramp-parse-netrc (filename)
6254 "Return a list of (user host) tuples allowed to access.
6255User may be nil."
00d6fd04
MA
6256 ;; On Windows, there are problems in completion when
6257 ;; `default-directory' is remote.
9e6ab520 6258 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6259 res)
8daea7fc 6260 (when (file-readable-p filename)
292ffc15
KG
6261 (with-temp-buffer
6262 (insert-file-contents filename)
6263 (goto-char (point-min))
6264 (while (not (eobp))
6265 (push (tramp-parse-netrc-group) res))))
6266 res))
6267
6268(defun tramp-parse-netrc-group ()
6269 "Return a (user host) tuple allowed to access.
6270User may be nil."
292ffc15
KG
6271 (let ((result)
6272 (regexp
6273 (concat
6274 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
6275 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
9e6ab520 6276 (narrow-to-region (point) (tramp-compat-line-end-position))
292ffc15
KG
6277 (when (re-search-forward regexp nil t)
6278 (setq result (list (match-string 3) (match-string 1))))
6279 (widen)
6280 (forward-line 1)
6281 result))
6282
00d6fd04
MA
6283(defun tramp-parse-putty (registry)
6284 "Return a list of (user host) tuples allowed to access.
6285User is always nil."
6286 ;; On Windows, there are problems in completion when
6287 ;; `default-directory' is remote.
9e6ab520 6288 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
6289 res)
6290 (with-temp-buffer
a4aeb9a4 6291 (when (zerop (tramp-local-call-process "reg" nil t nil "query" registry))
00d6fd04
MA
6292 (goto-char (point-min))
6293 (while (not (eobp))
6294 (push (tramp-parse-putty-group registry) res))))
6295 res))
6296
6297(defun tramp-parse-putty-group (registry)
6298 "Return a (user host) tuple allowed to access.
6299User is always nil."
6300 (let ((result)
6301 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
9e6ab520 6302 (narrow-to-region (point) (tramp-compat-line-end-position))
00d6fd04
MA
6303 (when (re-search-forward regexp nil t)
6304 (setq result (list nil (match-string 1))))
6305 (widen)
6306 (forward-line 1)
6307 result))
6308
fb7933a3
KG
6309;;; Internal Functions:
6310
00d6fd04
MA
6311(defun tramp-maybe-send-script (vec script name)
6312 "Define in remote shell function NAME implemented as SCRIPT.
6313Only send the definition if it has not already been done."
6314 (let* ((p (tramp-get-connection-process vec))
6315 (scripts (tramp-get-connection-property p "scripts" nil)))
1834b39f 6316 (unless (member name scripts)
3b30ccda
MA
6317 (with-progress-reporter vec 5 (format "Sending script `%s'" name)
6318 ;; The script could contain a call of Perl. This is masked with `%s'.
6319 (tramp-send-command-and-check
6320 vec
6321 (format "%s () {\n%s\n}" name
6322 (format script (tramp-get-remote-perl vec))))
6323 (tramp-set-connection-property p "scripts" (cons name scripts))))))
c82c5727 6324
fb7933a3 6325(defun tramp-set-auto-save ()
00d6fd04 6326 (when (and ;; ange-ftp has its own auto-save mechanism
7177e2a3
MA
6327 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6328 'tramp-sh-file-name-handler)
fb7933a3
KG
6329 auto-save-default)
6330 (auto-save-mode 1)))
6331(add-hook 'find-file-hooks 'tramp-set-auto-save t)
a69c01a0 6332(add-hook 'tramp-unload-hook
aa485f7c
MA
6333 (lambda ()
6334 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
fb7933a3
KG
6335
6336(defun tramp-run-test (switch filename)
6337 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6338Returns the exit code of the `test' program."
00d6fd04
MA
6339 (with-parsed-tramp-file-name filename nil
6340 (tramp-send-command-and-check
6341 v
6342 (format
6343 "%s %s %s"
6344 (tramp-get-test-command v)
6345 switch
6346 (tramp-shell-quote-argument localname)))))
6347
6348(defun tramp-run-test2 (format-string file1 file2)
6349 "Run `test'-like program on the remote system, given FILE1, FILE2.
6350FORMAT-STRING contains the program name, switches, and place holders.
6351Returns the exit code of the `test' program. Barfs if the methods,
fb7933a3 6352hosts, or files, disagree."
00d6fd04
MA
6353 (unless (tramp-equal-remote file1 file2)
6354 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6355 (tramp-error
6356 v 'file-error
6357 "tramp-run-test2 only implemented for same method, user, host")))
6358 (with-parsed-tramp-file-name file1 v1
6359 (with-parsed-tramp-file-name file1 v2
fb7933a3 6360 (tramp-send-command-and-check
00d6fd04
MA
6361 v1
6362 (format format-string
6363 (tramp-shell-quote-argument v1-localname)
6364 (tramp-shell-quote-argument v2-localname))))))
fb7933a3 6365
00d6fd04
MA
6366(defun tramp-buffer-name (vec)
6367 "A name for the connection buffer VEC."
6368 ;; We must use `tramp-file-name-real-host', because for gateway
6369 ;; methods the default port will be expanded later on, which would
6370 ;; tamper the name.
6371 (let ((method (tramp-file-name-method vec))
6372 (user (tramp-file-name-user vec))
6373 (host (tramp-file-name-real-host vec)))
6374 (if (not (zerop (length user)))
6375 (format "*tramp/%s %s@%s*" method user host)
6376 (format "*tramp/%s %s*" method host))))
6377
b88f2d0a
MA
6378(defun tramp-delete-temp-file-function ()
6379 "Remove temporary files related to current buffer."
6380 (when (stringp tramp-temp-buffer-file-name)
6381 (condition-case nil
eba082a2 6382 (delete-file tramp-temp-buffer-file-name)
b88f2d0a
MA
6383 (error nil))))
6384
6385(add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6386(add-hook 'tramp-cache-unload-hook
6387 (lambda ()
6388 (remove-hook 'kill-buffer-hook
6389 'tramp-delete-temp-file-function)))
6390
00d6fd04
MA
6391(defun tramp-get-buffer (vec)
6392 "Get the connection buffer to be used for VEC."
6393 (or (get-buffer (tramp-buffer-name vec))
6394 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
6395 (setq buffer-undo-list t)
6396 (setq default-directory
6397 (tramp-make-tramp-file-name
6398 (tramp-file-name-method vec)
6399 (tramp-file-name-user vec)
6400 (tramp-file-name-host vec)
6401 "/"))
6402 (current-buffer))))
6403
6404(defun tramp-get-connection-buffer (vec)
6405 "Get the connection buffer to be used for VEC.
6406In case a second asynchronous communication has been started, it is different
6407from `tramp-get-buffer'."
6408 (or (tramp-get-connection-property vec "process-buffer" nil)
6409 (tramp-get-buffer vec)))
6410
6411(defun tramp-get-connection-process (vec)
6412 "Get the connection process to be used for VEC.
6413In case a second asynchronous communication has been started, it is different
6414from the default one."
6415 (get-process
6416 (or (tramp-get-connection-property vec "process-name" nil)
6417 (tramp-buffer-name vec))))
6418
6419(defun tramp-debug-buffer-name (vec)
6420 "A name for the debug buffer for VEC."
6421 ;; We must use `tramp-file-name-real-host', because for gateway
6422 ;; methods the default port will be expanded later on, which would
6423 ;; tamper the name.
6424 (let ((method (tramp-file-name-method vec))
6425 (user (tramp-file-name-user vec))
6426 (host (tramp-file-name-real-host vec)))
6427 (if (not (zerop (length user)))
6428 (format "*debug tramp/%s %s@%s*" method user host)
6429 (format "*debug tramp/%s %s*" method host))))
6430
b533bc97
MA
6431(defconst tramp-debug-outline-regexp
6432 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6433
00d6fd04
MA
6434(defun tramp-get-debug-buffer (vec)
6435 "Get the debug buffer for VEC."
01917a18 6436 (with-current-buffer
00d6fd04
MA
6437 (get-buffer-create (tramp-debug-buffer-name vec))
6438 (when (bobp)
6439 (setq buffer-undo-list t)
b533bc97 6440 ;; Activate `outline-mode'. This runs `text-mode-hook' and
9ce8462a 6441 ;; `outline-mode-hook'. We must prevent that local processes
b533bc97
MA
6442 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
6443 ;; Furthermore, `outline-regexp' must have the correct value
6444 ;; already, because it is used by `font-lock-compile-keywords'.
6445 (let ((default-directory (tramp-compat-temporary-file-directory))
6446 (outline-regexp tramp-debug-outline-regexp))
00d6fd04 6447 (outline-mode))
b533bc97 6448 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
9ce8462a 6449 (set (make-local-variable 'outline-level) 'tramp-outline-level))
01917a18 6450 (current-buffer)))
fb7933a3 6451
00d6fd04
MA
6452(defun tramp-outline-level ()
6453 "Return the depth to which a statement is nested in the outline.
6454Point must be at the beginning of a header line.
6455
6456The outline level is equal to the verbosity of the Tramp message."
6457 (1+ (string-to-number (match-string 1))))
fb7933a3 6458
00d6fd04
MA
6459(defun tramp-find-executable
6460 (vec progname dirlist &optional ignore-tilde ignore-path)
6461 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6462First arg VEC specifies the connection, PROGNAME is the program
6463to search for, and DIRLIST gives the list of directories to
6464search. If IGNORE-TILDE is non-nil, directory names starting
6465with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6466only in DIRLIST.
fb7933a3 6467
7432277c 6468Returns the absolute file name of PROGNAME, if found, and nil otherwise.
fb7933a3
KG
6469
6470This function expects to be in the right *tramp* buffer."
c0e17ff2 6471 (with-current-buffer (tramp-get-connection-buffer vec)
00d6fd04
MA
6472 (let (result)
6473 ;; Check whether the executable is in $PATH. "which(1)" does not
6474 ;; report always a correct error code; therefore we check the
6475 ;; number of words it returns.
6476 (unless ignore-path
6477 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6478 (goto-char (point-min))
c0e17ff2 6479 (if (looking-at "^\\s-*1$")
00d6fd04
MA
6480 (setq result (concat "\\" progname))))
6481 (unless result
6482 (when ignore-tilde
b533bc97 6483 ;; Remove all ~/foo directories from dirlist. In XEmacs,
00d6fd04
MA
6484 ;; `remove' is in CL, and we want to avoid CL dependencies.
6485 (let (newdl d)
6486 (while dirlist
6487 (setq d (car dirlist))
6488 (setq dirlist (cdr dirlist))
6489 (unless (char-equal ?~ (aref d 0))
6490 (setq newdl (cons d newdl))))
6491 (setq dirlist (nreverse newdl))))
6492 (tramp-send-command
6493 vec
6494 (format (concat "while read d; "
6495 "do if test -x $d/%s -a -f $d/%s; "
6496 "then echo tramp_executable $d/%s; "
6497 "break; fi; done <<'EOF'\n"
6498 "%s\nEOF")
6499 progname progname progname (mapconcat 'identity dirlist "\n")))
6500 (goto-char (point-max))
6501 (when (search-backward "tramp_executable " nil t)
6502 (skip-chars-forward "^ ")
6503 (skip-chars-forward " ")
9e6ab520
MA
6504 (setq result (buffer-substring
6505 (point) (tramp-compat-line-end-position)))))
00d6fd04
MA
6506 result)))
6507
6508(defun tramp-set-remote-path (vec)
6509 "Sets the remote environment PATH to existing directories.
6510I.e., for each directory in `tramp-remote-path', it is tested
6511whether it exists and if so, it is added to the environment
6512variable PATH."
6513 (tramp-message vec 5 (format "Setting $PATH environment variable"))
f84638eb
MA
6514 (tramp-send-command
6515 vec (format "PATH=%s; export PATH"
6516 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
fb7933a3 6517
cfb5c0db
MA
6518;; ------------------------------------------------------------
6519;; -- Communication with external shell --
6520;; ------------------------------------------------------------
fb7933a3 6521
00d6fd04 6522(defun tramp-find-file-exists-command (vec)
fb7933a3
KG
6523 "Find a command on the remote host for checking if a file exists.
6524Here, we are looking for a command which has zero exit status if the
6525file exists and nonzero exit status otherwise."
00d6fd04 6526 (let ((existing "/")
fb7933a3 6527 (nonexisting
00d6fd04
MA
6528 (tramp-shell-quote-argument "/ this file does not exist "))
6529 result)
fb7933a3
KG
6530 ;; The algorithm is as follows: we try a list of several commands.
6531 ;; For each command, we first run `$cmd /' -- this should return
6532 ;; true, as the root directory always exists. And then we run
00d6fd04 6533 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
fb7933a3
KG
6534 ;; does not exist. This should return false. We use the first
6535 ;; command we find that seems to work.
6536 ;; The list of commands to try is as follows:
00d6fd04
MA
6537 ;; `ls -d' This works on most systems, but NetBSD 1.4
6538 ;; has a bug: `ls' always returns zero exit
6539 ;; status, even for files which don't exist.
6540 ;; `test -e' Some Bourne shells have a `test' builtin
6541 ;; which does not know the `-e' option.
6542 ;; `/bin/test -e' For those, the `test' binary on disk normally
6543 ;; provides the option. Alas, the binary
6544 ;; is sometimes `/bin/test' and sometimes it's
6545 ;; `/usr/bin/test'.
6546 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
fb7933a3 6547 (unless (or
00d6fd04
MA
6548 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6549 (zerop (tramp-send-command-and-check
6550 vec (format "%s %s" result existing)))
6551 (not (zerop (tramp-send-command-and-check
6552 vec (format "%s %s" result nonexisting)))))
6553 (and (setq result "/bin/test -e")
6554 (zerop (tramp-send-command-and-check
6555 vec (format "%s %s" result existing)))
6556 (not (zerop (tramp-send-command-and-check
6557 vec (format "%s %s" result nonexisting)))))
6558 (and (setq result "/usr/bin/test -e")
6559 (zerop (tramp-send-command-and-check
6560 vec (format "%s %s" result existing)))
6561 (not (zerop (tramp-send-command-and-check
6562 vec (format "%s %s" result nonexisting)))))
6563 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6564 (zerop (tramp-send-command-and-check
6565 vec (format "%s %s" result existing)))
6566 (not (zerop (tramp-send-command-and-check
6567 vec (format "%s %s" result nonexisting))))))
6568 (tramp-error
6569 vec 'file-error "Couldn't find command to check if file exists"))
6570 result))
bf247b6e 6571
fb7933a3 6572;; CCC test ksh or bash found for tilde expansion?
00d6fd04
MA
6573(defun tramp-find-shell (vec)
6574 "Opens a shell on the remote host which groks tilde expansion."
6575 (unless (tramp-get-connection-property vec "remote-shell" nil)
6576 (let (shell)
6577 (with-current-buffer (tramp-get-buffer vec)
7e780ff1 6578 (tramp-send-command vec "echo ~root" t)
00d6fd04 6579 (cond
c0e17ff2
MA
6580 ((or (string-match "^~root$" (buffer-string))
6581 ;; The default shell (ksh93) of OpenSolaris is buggy.
6582 (string-equal (tramp-get-connection-property vec "uname" "")
6583 "SunOS 5.11"))
00d6fd04 6584 (setq shell
f84638eb 6585 (or (tramp-find-executable
c0e17ff2 6586 vec "bash" (tramp-get-remote-path vec) t t)
f84638eb 6587 (tramp-find-executable
c0e17ff2 6588 vec "ksh" (tramp-get-remote-path vec) t t)))
00d6fd04
MA
6589 (unless shell
6590 (tramp-error
6591 vec 'file-error
6592 "Couldn't find a shell which groks tilde expansion"))
6593 ;; Find arguments for this shell.
6594 (let ((alist tramp-sh-extra-args)
6595 item extra-args)
6596 (while (and alist (null extra-args))
6597 (setq item (pop alist))
6598 (when (string-match (car item) shell)
6599 (setq extra-args (cdr item))))
6600 (when extra-args (setq shell (concat shell " " extra-args))))
6601 (tramp-message
3b30ccda 6602 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
dab816a9 6603 (let ((tramp-end-of-output tramp-initial-end-of-output))
a4aeb9a4 6604 (tramp-send-command
b08104a0 6605 vec
70c11b0b
MA
6606 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6607 (shell-quote-argument tramp-end-of-output) shell)
b08104a0 6608 t))
a0a5183a 6609 ;; Setting prompts.
3b30ccda
MA
6610 (with-progress-reporter vec 5 (format "Setting remote shell prompt")
6611 (tramp-send-command
6612 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6613 (tramp-send-command vec "PS2=''" t)
6614 (tramp-send-command vec "PS3=''" t)
6615 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
a0a5183a 6616
00d6fd04
MA
6617 (t (tramp-message
6618 vec 5 "Remote `%s' groks tilde expansion, good"
6619 (tramp-get-method-parameter
6620 (tramp-file-name-method vec) 'tramp-remote-sh))
6621 (tramp-set-connection-property
6622 vec "remote-shell"
6623 (tramp-get-method-parameter
6624 (tramp-file-name-method vec) 'tramp-remote-sh))))))))
fb7933a3 6625
bf247b6e
KS
6626;; ------------------------------------------------------------
6627;; -- Functions for establishing connection --
6628;; ------------------------------------------------------------
fb7933a3 6629
ac474af1
KG
6630;; The following functions are actions to be taken when seeing certain
6631;; prompts from the remote host. See the variable
6632;; `tramp-actions-before-shell' for usage of these functions.
6633
00d6fd04 6634(defun tramp-action-login (proc vec)
ac474af1 6635 "Send the login name."
00d6fd04
MA
6636 (when (not (stringp tramp-current-user))
6637 (save-window-excursion
6638 (let ((enable-recursive-minibuffers t))
6639 (pop-to-buffer (tramp-get-connection-buffer vec))
6640 (setq tramp-current-user (read-string (match-string 0))))))
6641 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
6642 (with-current-buffer (tramp-get-connection-buffer vec)
6643 (tramp-message vec 6 "\n%s" (buffer-string)))
6644 (tramp-send-string vec tramp-current-user))
6645
6646(defun tramp-action-password (proc vec)
ac474af1 6647 "Query the user for a password."
70c11b0b
MA
6648 (with-current-buffer (process-buffer proc)
6649 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
6650 (tramp-message vec 3 "Sending %s" (match-string 1)))
00d6fd04
MA
6651 (tramp-enter-password proc))
6652
6653(defun tramp-action-succeed (proc vec)
ac474af1 6654 "Signal success in finding shell prompt."
ac474af1
KG
6655 (throw 'tramp-action 'ok))
6656
00d6fd04 6657(defun tramp-action-permission-denied (proc vec)
ac474af1 6658 "Signal permission denied."
00d6fd04 6659 (kill-process proc)
ac474af1
KG
6660 (throw 'tramp-action 'permission-denied))
6661
00d6fd04 6662(defun tramp-action-yesno (proc vec)
3cdaec13
KG
6663 "Ask the user for confirmation using `yes-or-no-p'.
6664Send \"yes\" to remote process on confirmation, abort otherwise.
6665See also `tramp-action-yn'."
ac474af1 6666 (save-window-excursion
00d6fd04
MA
6667 (let ((enable-recursive-minibuffers t))
6668 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6669 (unless (yes-or-no-p (match-string 0))
6670 (kill-process proc)
6671 (throw 'tramp-action 'permission-denied))
6672 (with-current-buffer (tramp-get-connection-buffer vec)
6673 (tramp-message vec 6 "\n%s" (buffer-string)))
6674 (tramp-send-string vec "yes"))))
6675
6676(defun tramp-action-yn (proc vec)
3cdaec13
KG
6677 "Ask the user for confirmation using `y-or-n-p'.
6678Send \"y\" to remote process on confirmation, abort otherwise.
6679See also `tramp-action-yesno'."
6680 (save-window-excursion
00d6fd04
MA
6681 (let ((enable-recursive-minibuffers t))
6682 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6683 (unless (y-or-n-p (match-string 0))
6684 (kill-process proc)
6685 (throw 'tramp-action 'permission-denied))
6686 (with-current-buffer (tramp-get-connection-buffer vec)
6687 (tramp-message vec 6 "\n%s" (buffer-string)))
6688 (tramp-send-string vec "y"))))
6689
6690(defun tramp-action-terminal (proc vec)
487f4fb7
KG
6691 "Tell the remote host which terminal type to use.
6692The terminal type can be configured with `tramp-terminal-type'."
00d6fd04 6693 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
7e780ff1
MA
6694 (with-current-buffer (tramp-get-connection-buffer vec)
6695 (tramp-message vec 6 "\n%s" (buffer-string)))
00d6fd04 6696 (tramp-send-string vec tramp-terminal-type))
487f4fb7 6697
00d6fd04 6698(defun tramp-action-process-alive (proc vec)
a94d821f 6699 "Check, whether a process has finished."
00d6fd04 6700 (unless (memq (process-status proc) '(run open))
19a87064
MA
6701 (throw 'tramp-action 'process-died)))
6702
00d6fd04 6703(defun tramp-action-out-of-band (proc vec)
a94d821f 6704 "Check, whether an out-of-band copy has finished."
00d6fd04
MA
6705 (cond ((and (memq (process-status proc) '(stop exit))
6706 (zerop (process-exit-status proc)))
6707 (tramp-message vec 3 "Process has finished.")
38c65fca 6708 (throw 'tramp-action 'ok))
00d6fd04
MA
6709 ((or (and (memq (process-status proc) '(stop exit))
6710 (not (zerop (process-exit-status proc))))
6711 (memq (process-status proc) '(signal)))
01917a18
MA
6712 ;; `scp' could have copied correctly, but set modes could have failed.
6713 ;; This can be ignored.
00d6fd04
MA
6714 (with-current-buffer (process-buffer proc)
6715 (goto-char (point-min))
6716 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
6717 (progn
6718 (tramp-message vec 5 "'set mode' error ignored.")
6719 (tramp-message vec 3 "Process has finished.")
6720 (throw 'tramp-action 'ok))
6721 (tramp-message vec 3 "Process has died.")
6722 (throw 'tramp-action 'process-died))))
38c65fca
KG
6723 (t nil)))
6724
ac474af1
KG
6725;; Functions for processing the actions.
6726
00d6fd04 6727(defun tramp-process-one-action (proc vec actions)
ac474af1 6728 "Wait for output from the shell and perform one action."
00d6fd04 6729 (let (found todo item pattern action)
e6466697 6730 (while (not found)
00d6fd04
MA
6731 ;; Reread output once all actions have been performed.
6732 ;; Obviously, the output was not complete.
6733 (tramp-accept-process-output proc 1)
e6466697
MA
6734 (setq todo actions)
6735 (while todo
e6466697 6736 (setq item (pop todo))
95d610cb 6737 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
e6466697 6738 (setq action (nth 1 item))
00d6fd04
MA
6739 (tramp-message
6740 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
6741 (when (tramp-check-for-regexp proc pattern)
6742 (tramp-message vec 5 "Call `%s'" (symbol-name action))
6743 (setq found (funcall action proc vec)))))
e6466697
MA
6744 found))
6745
00d6fd04 6746(defun tramp-process-actions (proc vec actions &optional timeout)
e6466697 6747 "Perform actions until success or TIMEOUT."
9e021389
MA
6748 ;; Preserve message for `progress-reporter'.
6749 (with-temp-message ""
6750 ;; Enable auth-source and password-cache.
6751 (tramp-set-connection-property vec "first-password-request" t)
6752 (let (exit)
6753 (while (not exit)
6754 (tramp-message proc 3 "Waiting for prompts from remote shell")
6755 (setq exit
6756 (catch 'tramp-action
6757 (if timeout
6758 (with-timeout (timeout)
6759 (tramp-process-one-action proc vec actions))
6760 (tramp-process-one-action proc vec actions)))))
6761 (with-current-buffer (tramp-get-connection-buffer vec)
6762 (tramp-message vec 6 "\n%s" (buffer-string)))
6763 (unless (eq exit 'ok)
6764 (tramp-clear-passwd vec)
6765 (tramp-error-with-buffer
6766 nil vec 'file-error
6767 (cond
6768 ((eq exit 'permission-denied) "Permission denied")
6769 ((eq exit 'process-died) "Process died")
6770 (t "Login failed")))))))
fb7933a3
KG
6771
6772;; Utility functions.
6773
00d6fd04 6774(defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
d2a2c17f
MA
6775 "Like `accept-process-output' for Tramp processes.
6776This is needed in order to hide `last-coding-system-used', which is set
6777for process communication also."
00d6fd04
MA
6778 (with-current-buffer (process-buffer proc)
6779 (tramp-message proc 10 "%s %s" proc (process-status proc))
6780 (let (buffer-read-only last-coding-system-used)
6781 ;; Under Windows XP, accept-process-output doesn't return
6782 ;; sometimes. So we add an additional timeout.
6783 (with-timeout ((or timeout 1))
6784 (accept-process-output proc timeout timeout-msecs)))
6785 (tramp-message proc 10 "\n%s" (buffer-string))))
6786
6787(defun tramp-check-for-regexp (proc regexp)
a94d821f 6788 "Check, whether REGEXP is contained in process buffer of PROC.
00d6fd04
MA
6789Erase echoed commands if exists."
6790 (with-current-buffer (process-buffer proc)
6791 (goto-char (point-min))
674da028 6792
00d6fd04
MA
6793 ;; Check whether we need to remove echo output.
6794 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
6795 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
6796 (let ((begin (match-beginning 0)))
6797 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
6798 ;; Discard echo from remote output.
6799 (tramp-set-connection-property proc "check-remote-echo" nil)
6800 (tramp-message proc 5 "echo-mark found")
b533bc97 6801 (forward-line 1)
00d6fd04
MA
6802 (delete-region begin (point))
6803 (goto-char (point-min)))))
674da028 6804
68712eb6
MA
6805 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
6806 ;; Sometimes, the echo string is suppressed on the remote side.
6807 (not (string-equal
0d5852cf
MA
6808 (tramp-compat-funcall
6809 'substring-no-properties tramp-echo-mark-marker
68712eb6 6810 0 (min tramp-echo-mark-marker-length (1- (point-max))))
0d5852cf
MA
6811 (tramp-compat-funcall
6812 'buffer-substring-no-properties
68712eb6 6813 1 (min (1+ tramp-echo-mark-marker-length) (point-max))))))
70c11b0b 6814 ;; No echo to be handled, now we can look for the regexp.
674da028 6815 (goto-char (point-min))
00d6fd04 6816 (re-search-forward regexp nil t))))
d2a2c17f 6817
fb7933a3
KG
6818(defun tramp-wait-for-regexp (proc timeout regexp)
6819 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6820Expects the output of PROC to be sent to the current buffer. Returns
6821the string that matched, or nil. Waits indefinitely if TIMEOUT is
6822nil."
00d6fd04
MA
6823 (with-current-buffer (process-buffer proc)
6824 (let ((found (tramp-check-for-regexp proc regexp))
6825 (start-time (current-time)))
6826 (cond (timeout
6827 ;; Work around a bug in XEmacs 21, where the timeout
6828 ;; expires faster than it should. This degenerates
6829 ;; to polling for buggy XEmacsen, but oh, well.
6830 (while (and (not found)
6831 (< (tramp-time-diff (current-time) start-time)
6832 timeout))
6833 (with-timeout (timeout)
6834 (while (not found)
6835 (tramp-accept-process-output proc 1)
6836 (unless (memq (process-status proc) '(run open))
6837 (tramp-error-with-buffer
6838 nil proc 'file-error "Process has died"))
6839 (setq found (tramp-check-for-regexp proc regexp))))))
6840 (t
6841 (while (not found)
6842 (tramp-accept-process-output proc 1)
6843 (unless (memq (process-status proc) '(run open))
6844 (tramp-error-with-buffer
6845 nil proc 'file-error "Process has died"))
6846 (setq found (tramp-check-for-regexp proc regexp)))))
6847 (tramp-message proc 6 "\n%s" (buffer-string))
fb7933a3 6848 (when (not found)
00d6fd04
MA
6849 (if timeout
6850 (tramp-error
6851 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
6852 regexp timeout)
6853 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6854 found)))
fb7933a3 6855
b25a52cc
KG
6856(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6857 "Wait for shell prompt and barf if none appears.
6858Looks at process PROC to see if a shell prompt appears in TIMEOUT
6859seconds. If not, it produces an error message with the given ERROR-ARGS."
7e780ff1
MA
6860 (unless
6861 (tramp-wait-for-regexp
6862 proc timeout
6863 (format
6864 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
00d6fd04
MA
6865 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6866
7e780ff1
MA
6867;; We don't call `tramp-send-string' in order to hide the password
6868;; from the debug buffer, and because end-of-line handling of the
6869;; string.
6870(defun tramp-enter-password (proc)
00d6fd04
MA
6871 "Prompt for a password and send it to the remote end."
6872 (process-send-string
7e780ff1
MA
6873 proc (concat (tramp-read-passwd proc)
6874 (or (tramp-get-method-parameter
6875 tramp-current-method
6876 'tramp-password-end-of-line)
6877 tramp-default-password-end-of-line))))
00d6fd04
MA
6878
6879(defun tramp-open-connection-setup-interactive-shell (proc vec)
fb7933a3 6880 "Set up an interactive shell.
00d6fd04
MA
6881Mainly sets the prompt and the echo correctly. PROC is the shell
6882process to set up. VEC specifies the connection."
dab816a9 6883 (let ((tramp-end-of-output tramp-initial-end-of-output))
8950769a
MA
6884 ;; It is useful to set the prompt in the following command because
6885 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6886 ;; about and thus /bin/sh will display a strange prompt. For
6887 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6888 ;; display the current working directory but /bin/sh will display
6889 ;; a dollar sign. The following command line sets $PS1 to a sane
6890 ;; value, and works under Bourne-ish shells as well as csh-like
6891 ;; shells. Daniel Pittman reports that the unusual positioning of
6892 ;; the single quotes makes it work under `rc', too. We also unset
6893 ;; the variable $ENV because that is read by some sh
6894 ;; implementations (eg, bash when called as sh) on startup; this
6895 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6896 ;; is another way to set the prompt in /bin/bash, it must be
6897 ;; discarded as well.
a4aeb9a4
MA
6898 (tramp-send-command
6899 vec
6900 (format
70c11b0b
MA
6901 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6902 (shell-quote-argument tramp-end-of-output)
a4aeb9a4
MA
6903 (tramp-get-method-parameter
6904 (tramp-file-name-method vec) 'tramp-remote-sh))
8950769a
MA
6905 t)
6906
6907 ;; Disable echo.
6908 (tramp-message vec 5 "Setting up remote shell environment")
6909 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
6910 ;; Check whether the echo has really been disabled. Some
6911 ;; implementations, like busybox of embedded GNU/Linux, don't
6912 ;; support disabling.
6913 (tramp-send-command vec "echo foo" t)
6914 (with-current-buffer (process-buffer proc)
6915 (goto-char (point-min))
6916 (when (looking-at "echo foo")
6917 (tramp-set-connection-property proc "remote-echo" t)
6918 (tramp-message vec 5 "Remote echo still on. Ok.")
6919 ;; Make sure backspaces and their echo are enabled and no line
6920 ;; width magic interferes with them.
6921 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
e42c6bbc 6922
7e780ff1 6923 (tramp-message vec 5 "Setting shell prompt")
70c11b0b
MA
6924 (tramp-send-command
6925 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
9fa0d3aa
MA
6926 (tramp-send-command vec "PS2=''" t)
6927 (tramp-send-command vec "PS3=''" t)
6928 (tramp-send-command vec "PROMPT_COMMAND=''" t)
e42c6bbc 6929
fb7933a3
KG
6930 ;; Try to set up the coding system correctly.
6931 ;; CCC this can't be the right way to do it. Hm.
00d6fd04 6932 (tramp-message vec 5 "Determining coding system")
7e780ff1 6933 (tramp-send-command vec "echo foo ; echo bar" t)
00d6fd04 6934 (with-current-buffer (process-buffer proc)
fb7933a3
KG
6935 (goto-char (point-min))
6936 (if (featurep 'mule)
00d6fd04
MA
6937 ;; Use MULE to select the right EOL convention for communicating
6938 ;; with the process.
0d5852cf 6939 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
00d6fd04
MA
6940 (cons 'undecided 'undecided)))
6941 cs-decode cs-encode)
6942 (when (symbolp cs) (setq cs (cons cs cs)))
6943 (setq cs-decode (car cs))
6944 (setq cs-encode (cdr cs))
6945 (unless cs-decode (setq cs-decode 'undecided))
6946 (unless cs-encode (setq cs-encode 'undecided))
6947 (setq cs-encode (tramp-coding-system-change-eol-conversion
6948 cs-encode 'unix))
6949 (when (search-forward "\r" nil t)
6950 (setq cs-decode (tramp-coding-system-change-eol-conversion
6951 cs-decode 'dos)))
0d5852cf
MA
6952 (tramp-compat-funcall
6953 'set-buffer-process-coding-system cs-decode cs-encode)
70c11b0b
MA
6954 (tramp-message
6955 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
fb7933a3
KG
6956 ;; Look for ^M and do something useful if found.
6957 (when (search-forward "\r" nil t)
00d6fd04
MA
6958 ;; We have found a ^M but cannot frob the process coding system
6959 ;; because we're running on a non-MULE Emacs. Let's try
6960 ;; stty, instead.
7e780ff1 6961 (tramp-send-command vec "stty -onlcr" t))))
7e5686f0
MA
6962 ;; Dump stty settings in the traces.
6963 (when (>= tramp-verbose 10)
6964 (tramp-send-command vec "stty -a" t))
7e780ff1 6965 (tramp-send-command vec "set +o vi +o emacs" t)
e42c6bbc
MA
6966
6967 ;; Check whether the output of "uname -sr" has been changed. If
6968 ;; yes, this is a strong indication that we must expire all
d8ac123e
MA
6969 ;; connection properties. We start again with
6970 ;; `tramp-maybe-open-connection', it will be catched there.
e42c6bbc
MA
6971 (tramp-message vec 5 "Checking system information")
6972 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
6973 (new-uname
6974 (tramp-set-connection-property
6975 vec "uname"
6976 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
6977 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
d8ac123e 6978 (with-current-buffer (tramp-get-debug-buffer vec)
7ba94701 6979 ;; Keep the debug buffer.
2296b54d
MA
6980 (rename-buffer
6981 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
0d5852cf 6982 (tramp-compat-funcall 'tramp-cleanup-connection vec)
d8ac123e
MA
6983 (if (= (point-min) (point-max))
6984 (kill-buffer nil)
6985 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
6986 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
6987 (tramp-get-buffer vec)
6988 (tramp-message
6989 vec 3
6990 "Connection reset, because remote host changed from `%s' to `%s'"
6991 old-uname new-uname)
6992 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
e42c6bbc
MA
6993
6994 ;; Check whether the remote host suffers from buggy
6995 ;; `send-process-string'. This is known for FreeBSD (see comment in
6996 ;; `send_process', file process.c). I've tested sending 624 bytes
6997 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
6998 ;; this host type is detected locally. It cannot handle remote
6999 ;; hosts, though.
00d6fd04
MA
7000 (with-connection-property proc "chunksize"
7001 (cond
7002 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
7003 tramp-chunksize)
7004 (t
7005 (tramp-message
7006 vec 5 "Checking remote host type for `send-process-string' bug")
7007 (if (string-match
e42c6bbc 7008 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
00d6fd04 7009 500 0))))
e42c6bbc 7010
00d6fd04
MA
7011 ;; Set remote PATH variable.
7012 (tramp-set-remote-path vec)
e42c6bbc 7013
fb7933a3
KG
7014 ;; Search for a good shell before searching for a command which
7015 ;; checks if a file exists. This is done because Tramp wants to use
7016 ;; "test foo; echo $?" to check if various conditions hold, and
7017 ;; there are buggy /bin/sh implementations which don't execute the
7018 ;; "echo $?" part if the "test" part has an error. In particular,
c0e17ff2
MA
7019 ;; the OpenSolaris /bin/sh is a problem. There are also other
7020 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
7021 ;; in in function declarations, or changing HISTFILE in place.
7022 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
7023 ;; detected.
00d6fd04 7024 (tramp-find-shell vec)
e42c6bbc 7025
00d6fd04 7026 ;; Disable unexpected output.
7e780ff1 7027 (tramp-send-command vec "mesg n; biff n" t)
e42c6bbc 7028
00d6fd04
MA
7029 ;; Set the environment.
7030 (tramp-message vec 5 "Setting default environment")
661aaece 7031
00d6fd04
MA
7032 (let ((env (copy-sequence tramp-remote-process-environment))
7033 unset item)
7034 (while env
70c11b0b 7035 (setq item (tramp-compat-split-string (car env) "="))
7e5686f0
MA
7036 (setcdr item (mapconcat 'identity (cdr item) "="))
7037 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
00d6fd04 7038 (tramp-send-command
7e5686f0 7039 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
00d6fd04
MA
7040 (push (car item) unset))
7041 (setq env (cdr env)))
7042 (when unset
fb7933a3 7043 (tramp-send-command
7e780ff1 7044 vec (format "unset %s" (mapconcat 'identity unset " "))))) t)
fb7933a3 7045
ac474af1
KG
7046;; CCC: We should either implement a Perl version of base64 encoding
7047;; and decoding. Then we just use that in the last item. The other
7048;; alternative is to use the Perl version of UU encoding. But then
7049;; we need a Lisp version of uuencode.
16674e4f
KG
7050;;
7051;; Old text from documentation of tramp-methods:
7052;; Using a uuencode/uudecode inline method is discouraged, please use one
7053;; of the base64 methods instead since base64 encoding is much more
7054;; reliable and the commands are more standardized between the different
7055;; Unix versions. But if you can't use base64 for some reason, please
7056;; note that the default uudecode command does not work well for some
7057;; Unices, in particular AIX and Irix. For AIX, you might want to use
7058;; the following command for uudecode:
7059;;
7060;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7061;;
7062;; For Irix, no solution is known yet.
7063
00d6fd04
MA
7064(defconst tramp-local-coding-commands
7065 '((b64 base64-encode-region base64-decode-region)
7066 (uu tramp-uuencode-region uudecode-decode-region)
7067 (pack
7068 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7069 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7070 "List of local coding commands for inline transfer.
16674e4f
KG
7071Each item is a list that looks like this:
7072
b533bc97 7073\(FORMAT ENCODING DECODING\)
ac474af1 7074
00d6fd04
MA
7075FORMAT is symbol describing the encoding/decoding format. It can be
7076`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
ac474af1 7077
00d6fd04
MA
7078ENCODING and DECODING can be strings, giving commands, or symbols,
7079giving functions. If they are strings, then they can contain
16674e4f
KG
7080the \"%s\" format specifier. If that specifier is present, the input
7081filename will be put into the command line at that spot. If the
7082specifier is not present, the input should be read from standard
7083input.
ac474af1 7084
16674e4f
KG
7085If they are functions, they will be called with two arguments, start
7086and end of region, and are expected to replace the region contents
7087with the encoded or decoded results, respectively.")
ac474af1 7088
00d6fd04 7089(defconst tramp-remote-coding-commands
3dc847a3
MA
7090 '((b64 "base64" "base64 -d")
7091 (b64 "mimencode -b" "mimencode -u -b")
00d6fd04
MA
7092 (b64 "mmencode -b" "mmencode -u -b")
7093 (b64 "recode data..base64" "recode base64..data")
7094 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
7095 (b64 tramp-perl-encode tramp-perl-decode)
7096 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7097 (uu "uuencode xxx" "uudecode -o -")
7098 (uu "uuencode xxx" "uudecode -p")
7099 (uu "uuencode xxx" tramp-uudecode)
7100 (pack
7101 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7102 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7103 "List of remote coding commands for inline transfer.
7104Each item is a list that looks like this:
7105
b533bc97 7106\(FORMAT ENCODING DECODING\)
00d6fd04
MA
7107
7108FORMAT is symbol describing the encoding/decoding format. It can be
7109`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7110
7111ENCODING and DECODING can be strings, giving commands, or symbols,
7112giving variables. If they are strings, then they can contain
7113the \"%s\" format specifier. If that specifier is present, the input
7114filename will be put into the command line at that spot. If the
7115specifier is not present, the input should be read from standard
7116input.
7117
7118If they are variables, this variable is a string containing a Perl
7119implementation for this functionality. This Perl program will be transferred
db9e401b 7120to the remote host, and it is available as shell function with the same name.")
00d6fd04
MA
7121
7122(defun tramp-find-inline-encoding (vec)
ac474af1 7123 "Find an inline transfer encoding that works.
00d6fd04
MA
7124Goes through the list `tramp-local-coding-commands' and
7125`tramp-remote-coding-commands'."
7126 (save-excursion
7127 (let ((local-commands tramp-local-coding-commands)
7128 (magic "xyzzy")
7129 loc-enc loc-dec rem-enc rem-dec litem ritem found)
7130 (while (and local-commands (not found))
7131 (setq litem (pop local-commands))
7132 (catch 'wont-work-local
7133 (let ((format (nth 0 litem))
7134 (remote-commands tramp-remote-coding-commands))
7135 (setq loc-enc (nth 1 litem))
7136 (setq loc-dec (nth 2 litem))
7137 ;; If the local encoder or decoder is a string, the
7138 ;; corresponding command has to work locally.
7139 (if (not (stringp loc-enc))
7140 (tramp-message
7141 vec 5 "Checking local encoding function `%s'" loc-enc)
7142 (tramp-message
7143 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
7144 (unless (zerop (tramp-call-local-coding-command
7145 loc-enc nil nil))
7146 (throw 'wont-work-local nil)))
7147 (if (not (stringp loc-dec))
7148 (tramp-message
7149 vec 5 "Checking local decoding function `%s'" loc-dec)
7150 (tramp-message
7151 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
7152 (unless (zerop (tramp-call-local-coding-command
7153 loc-dec nil nil))
7154 (throw 'wont-work-local nil)))
7155 ;; Search for remote coding commands with the same format
7156 (while (and remote-commands (not found))
7157 (setq ritem (pop remote-commands))
7158 (catch 'wont-work-remote
7159 (when (equal format (nth 0 ritem))
7160 (setq rem-enc (nth 1 ritem))
7161 (setq rem-dec (nth 2 ritem))
7162 ;; Check if remote encoding and decoding commands can be
7163 ;; called remotely with null input and output. This makes
7164 ;; sure there are no syntax errors and the command is really
7165 ;; found. Note that we do not redirect stdout to /dev/null,
7166 ;; for two reasons: when checking the decoding command, we
7167 ;; actually check the output it gives. And also, when
7168 ;; redirecting "mimencode" output to /dev/null, then as root
7169 ;; it might change the permissions of /dev/null!
7170 (when (not (stringp rem-enc))
7171 (let ((name (symbol-name rem-enc)))
7172 (while (string-match (regexp-quote "-") name)
7173 (setq name (replace-match "_" nil t name)))
7174 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
7175 (setq rem-enc name)))
7176 (tramp-message
7177 vec 5
7178 "Checking remote encoding command `%s' for sanity" rem-enc)
7179 (unless (zerop (tramp-send-command-and-check
7180 vec (format "%s </dev/null" rem-enc) t))
7181 (throw 'wont-work-remote nil))
7182
7183 (when (not (stringp rem-dec))
7184 (let ((name (symbol-name rem-dec)))
7185 (while (string-match (regexp-quote "-") name)
7186 (setq name (replace-match "_" nil t name)))
7187 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
7188 (setq rem-dec name)))
7189 (tramp-message
7190 vec 5
7191 "Checking remote decoding command `%s' for sanity" rem-dec)
7192 (unless (zerop (tramp-send-command-and-check
7193 vec
7194 (format "echo %s | %s | %s"
b593f105
MA
7195 magic rem-enc rem-dec)
7196 t))
00d6fd04
MA
7197 (throw 'wont-work-remote nil))
7198
7199 (with-current-buffer (tramp-get-buffer vec)
7200 (goto-char (point-min))
7201 (unless (looking-at (regexp-quote magic))
7202 (throw 'wont-work-remote nil)))
7203
7204 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7205 (setq rem-enc (nth 1 ritem))
7206 (setq rem-dec (nth 2 ritem))
7207 (setq found t)))))))
7208
1d7e9a01 7209 ;; Did we find something?
00d6fd04 7210 (unless found
7e5686f0
MA
7211 (tramp-error
7212 vec 'file-error "Couldn't find an inline transfer encoding"))
00d6fd04
MA
7213
7214 ;; Set connection properties.
7215 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
7216 (tramp-set-connection-property vec "local-encoding" loc-enc)
7217 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
7218 (tramp-set-connection-property vec "local-decoding" loc-dec)
7219 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
7220 (tramp-set-connection-property vec "remote-encoding" rem-enc)
7221 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
7222 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
16674e4f
KG
7223
7224(defun tramp-call-local-coding-command (cmd input output)
7225 "Call the local encoding or decoding command.
7226If CMD contains \"%s\", provide input file INPUT there in command.
7227Otherwise, INPUT is passed via standard input.
7228INPUT can also be nil which means `/dev/null'.
7229OUTPUT can be a string (which specifies a filename), or t (which
7230means standard output and thus the current buffer), or nil (which
7231means discard it)."
a4aeb9a4
MA
7232 (tramp-local-call-process
7233 tramp-encoding-shell
7234 (when (and input (not (string-match "%s" cmd))) input)
7235 (if (eq output t) t nil)
7236 nil
7237 tramp-encoding-command-switch
7238 (concat
7239 (if (string-match "%s" cmd) (format cmd input) cmd)
7240 (if (stringp output) (concat "> " output) ""))))
00d6fd04 7241
6a29a838
MA
7242(defconst tramp-inline-compress-commands
7243 '(("gzip" "gzip -d")
7244 ("bzip2" "bzip2 -d")
7245 ("compress" "compress -d"))
7246 "List of compress and decompress commands for inline transfer.
7247Each item is a list that looks like this:
7248
7249\(COMPRESS DECOMPRESS\)
7250
7251COMPRESS or DECOMPRESS are strings with the respective commands.")
7252
7253(defun tramp-find-inline-compress (vec)
7254 "Find an inline transfer compress command that works.
7255Goes through the list `tramp-inline-compress-commands'."
7256 (save-excursion
7257 (let ((commands tramp-inline-compress-commands)
7258 (magic "xyzzy")
7259 item compress decompress
7260 found)
7261 (while (and commands (not found))
7262 (catch 'next
7263 (setq item (pop commands)
7264 compress (nth 0 item)
7265 decompress (nth 1 item))
7266 (tramp-message
7267 vec 5
7268 "Checking local compress command `%s', `%s' for sanity"
7269 compress decompress)
7270 (unless (zerop (tramp-call-local-coding-command
7271 (format "echo %s | %s | %s"
7272 magic compress decompress) nil nil))
7273 (throw 'next nil))
7274 (tramp-message
7275 vec 5
7276 "Checking remote compress command `%s', `%s' for sanity"
7277 compress decompress)
7278 (unless (zerop (tramp-send-command-and-check
7279 vec (format "echo %s | %s | %s"
7280 magic compress decompress) t))
7281 (throw 'next nil))
7282 (setq found t)))
7283
7284 ;; Did we find something?
7285 (if found
7286 (progn
7287 ;; Set connection properties.
7288 (tramp-message
7289 vec 5 "Using inline transfer compress command `%s'" compress)
7290 (tramp-set-connection-property vec "inline-compress" compress)
7291 (tramp-message
7292 vec 5 "Using inline transfer decompress command `%s'" decompress)
7293 (tramp-set-connection-property vec "inline-decompress" decompress))
7294
7295 (tramp-set-connection-property vec "inline-compress" nil)
7296 (tramp-set-connection-property vec "inline-decompress" nil)
7297 (tramp-message
7298 vec 2 "Couldn't find an inline transfer compress command")))))
7299
00d6fd04
MA
7300(defun tramp-compute-multi-hops (vec)
7301 "Expands VEC according to `tramp-default-proxies-alist'.
7302Gateway hops are already opened."
7303 (let ((target-alist `(,vec))
7304 (choices tramp-default-proxies-alist)
7305 item proxy)
7306
7307 ;; Look for proxy hosts to be passed.
7308 (while choices
7309 (setq item (pop choices)
70c11b0b 7310 proxy (eval (nth 2 item)))
00d6fd04
MA
7311 (when (and
7312 ;; host
70c11b0b 7313 (string-match (or (eval (nth 0 item)) "")
00d6fd04
MA
7314 (or (tramp-file-name-host (car target-alist)) ""))
7315 ;; user
70c11b0b 7316 (string-match (or (eval (nth 1 item)) "")
00d6fd04
MA
7317 (or (tramp-file-name-user (car target-alist)) "")))
7318 (if (null proxy)
7319 ;; No more hops needed.
7320 (setq choices nil)
7321 ;; Replace placeholders.
7322 (setq proxy
7323 (format-spec
7324 proxy
b533bc97
MA
7325 (format-spec-make
7326 ?u (or (tramp-file-name-user (car target-alist)) "")
7327 ?h (or (tramp-file-name-host (car target-alist)) ""))))
00d6fd04
MA
7328 (with-parsed-tramp-file-name proxy l
7329 ;; Add the hop.
7330 (add-to-list 'target-alist l)
7331 ;; Start next search.
7332 (setq choices tramp-default-proxies-alist)))))
7333
7334 ;; Handle gateways.
8a4438b6
MA
7335 (when (and (boundp 'tramp-gw-tunnel-method)
7336 (string-match (format
7337 "^\\(%s\\|%s\\)$"
7338 (symbol-value 'tramp-gw-tunnel-method)
7339 (symbol-value 'tramp-gw-socks-method))
7340 (tramp-file-name-method (car target-alist))))
00d6fd04
MA
7341 (let ((gw (pop target-alist))
7342 (hop (pop target-alist)))
7343 ;; Is the method prepared for gateways?
7344 (unless (tramp-get-method-parameter
7345 (tramp-file-name-method hop) 'tramp-default-port)
7346 (tramp-error
7347 vec 'file-error
7348 "Method `%s' is not supported for gateway access."
7349 (tramp-file-name-method hop)))
7350 ;; Add default port if needed.
7351 (unless
7352 (string-match
7353 tramp-host-with-port-regexp (tramp-file-name-host hop))
7354 (aset hop 2
7355 (concat
7356 (tramp-file-name-host hop) tramp-prefix-port-format
7357 (number-to-string
7358 (tramp-get-method-parameter
7359 (tramp-file-name-method hop) 'tramp-default-port)))))
7360 ;; Open the gateway connection.
7361 (add-to-list
7362 'target-alist
7363 (vector
7364 (tramp-file-name-method hop) (tramp-file-name-user hop)
0d5852cf 7365 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil))
00d6fd04
MA
7366 ;; For the password prompt, we need the correct values.
7367 ;; Therefore, we must remember the gateway vector. But we
7368 ;; cannot do it as connection property, because it shouldn't
7369 ;; be persistent. And we have no started process yet either.
7370 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
7371
7372 ;; Foreign and out-of-band methods are not supported for multi-hops.
7373 (when (cdr target-alist)
7374 (setq choices target-alist)
7375 (while choices
7376 (setq item (pop choices))
7377 (when
7378 (or
7379 (not
7380 (tramp-get-method-parameter
7381 (tramp-file-name-method item) 'tramp-login-program))
7382 (tramp-get-method-parameter
7383 (tramp-file-name-method item) 'tramp-copy-program))
7384 (tramp-error
7385 vec 'file-error
7386 "Method `%s' is not supported for multi-hops."
7387 (tramp-file-name-method item)))))
7388
2991e49f
MA
7389 ;; In case the host name is not used for the remote shell
7390 ;; command, the user could be misguided by applying a random
7391 ;; hostname.
7392 (let* ((v (car target-alist))
7393 (method (tramp-file-name-method v))
7394 (host (tramp-file-name-host v)))
7395 (unless
7396 (or
7397 ;; There are multi-hops.
7398 (cdr target-alist)
7399 ;; The host name is used for the remote shell command.
7400 (member
7401 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7402 ;; The host is local. We cannot use `tramp-local-host-p'
7403 ;; here, because it opens a connection as well.
b96e6899 7404 (string-match tramp-local-host-regexp host))
2991e49f 7405 (tramp-error
42bc9b6d
MA
7406 v 'file-error
7407 "Host `%s' looks like a remote host, `%s' can only use the local host"
7408 host method)))
2991e49f 7409
00d6fd04
MA
7410 ;; Result.
7411 target-alist))
7412
7413(defun tramp-maybe-open-connection (vec)
7414 "Maybe open a connection VEC.
fb7933a3
KG
7415Does not do anything if a connection is already open, but re-opens the
7416connection if a previous connection has died for some reason."
d8ac123e
MA
7417 (catch 'uname-changed
7418 (let ((p (tramp-get-connection-process vec))
a7723e05 7419 (process-name (tramp-get-connection-property vec "process-name" nil))
d8ac123e
MA
7420 (process-environment (copy-sequence process-environment)))
7421
7422 ;; If too much time has passed since last command was sent, look
7423 ;; whether process is still alive. If it isn't, kill it. When
7424 ;; using ssh, it can sometimes happen that the remote end has
7425 ;; hung up but the local ssh client doesn't recognize this until
7426 ;; it tries to send some data to the remote end. So that's why
7427 ;; we try to send a command from time to time, then look again
7428 ;; whether the process is really alive.
7429 (condition-case nil
7430 (when (and (> (tramp-time-diff
7431 (current-time)
7432 (tramp-get-connection-property
7433 p "last-cmd-time" '(0 0 0)))
7434 60)
7435 p (processp p) (memq (process-status p) '(run open)))
7436 (tramp-send-command vec "echo are you awake" t t)
7437 (unless (and (memq (process-status p) '(run open))
7438 (tramp-wait-for-output p 10))
7439 ;; The error will be catched locally.
7440 (tramp-error vec 'file-error "Awake did fail")))
7441 (file-error
7442 (tramp-flush-connection-property vec)
7443 (tramp-flush-connection-property p)
7444 (delete-process p)
7445 (setq p nil)))
7446
7447 ;; New connection must be opened.
7448 (unless (and p (processp p) (memq (process-status p) '(run open)))
7449
7450 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7451 ;; messages from the beginning.
7452 (tramp-get-buffer vec)
655bded0
MA
7453 (with-progress-reporter
7454 vec 3
7455 (if (zerop (length (tramp-file-name-user vec)))
7456 (format "Opening connection for %s using %s"
7457 (tramp-file-name-host vec)
7458 (tramp-file-name-method vec))
7459 (format "Opening connection for %s@%s using %s"
7460 (tramp-file-name-user vec)
7461 (tramp-file-name-host vec)
7462 (tramp-file-name-method vec)))
7463
7464 ;; Start new process.
7465 (when (and p (processp p))
7466 (delete-process p))
7467 (setenv "TERM" tramp-terminal-type)
7468 (setenv "LC_ALL" "C")
7469 (setenv "PROMPT_COMMAND")
7470 (setenv "PS1" tramp-initial-end-of-output)
7471 (let* ((target-alist (tramp-compute-multi-hops vec))
7472 (process-connection-type tramp-process-connection-type)
7473 (process-adaptive-read-buffering nil)
7474 (coding-system-for-read nil)
7475 ;; This must be done in order to avoid our file name handler.
7476 (p (let ((default-directory
7477 (tramp-compat-temporary-file-directory)))
7478 (start-process
7479 (or process-name (tramp-buffer-name vec))
7480 (tramp-get-connection-buffer vec)
7481 tramp-encoding-shell))))
00d6fd04 7482
655bded0
MA
7483 (tramp-message
7484 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
d8ac123e 7485
655bded0
MA
7486 ;; Check whether process is alive.
7487 (tramp-set-process-query-on-exit-flag p nil)
a94d821f 7488 (tramp-barf-if-no-shell-prompt
655bded0
MA
7489 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell)
7490
7491 ;; Now do all the connections as specified.
7492 (while target-alist
7493 (let* ((hop (car target-alist))
7494 (l-method (tramp-file-name-method hop))
7495 (l-user (tramp-file-name-user hop))
7496 (l-host (tramp-file-name-host hop))
7497 (l-port nil)
7498 (login-program
7499 (tramp-get-method-parameter
7500 l-method 'tramp-login-program))
7501 (login-args
7502 (tramp-get-method-parameter l-method 'tramp-login-args))
7503 (async-args
7504 (tramp-get-method-parameter l-method 'tramp-async-args))
7505 (gw-args
7506 (tramp-get-method-parameter l-method 'tramp-gw-args))
7507 (gw (tramp-get-file-property hop "" "gateway" nil))
7508 (g-method (and gw (tramp-file-name-method gw)))
7509 (g-user (and gw (tramp-file-name-user gw)))
7510 (g-host (and gw (tramp-file-name-host gw)))
7511 (command login-program)
7512 ;; We don't create the temporary file. In fact,
7513 ;; it is just a prefix for the ControlPath option
7514 ;; of ssh; the real temporary file has another
7515 ;; name, and it is created and protected by ssh.
7516 ;; It is also removed by ssh, when the connection
7517 ;; is closed.
7518 (tmpfile
7519 (tramp-set-connection-property
7520 p "temp-file"
7521 (make-temp-name
7522 (expand-file-name
7523 tramp-temp-name-prefix
7524 (tramp-compat-temporary-file-directory)))))
7525 spec)
7526
7527 ;; Add arguments for asynchrononous processes.
7528 (when (and process-name async-args)
7529 (setq login-args (append login-args async-args)))
7530
7531 ;; Add gateway arguments if necessary.
7532 (when (and gw gw-args)
7533 (setq login-args (append login-args gw-args)))
7534
7535 ;; Check for port number. Until now, there's no need
7536 ;; for handling like method, user, host.
7537 (when (string-match tramp-host-with-port-regexp l-host)
d8ac123e
MA
7538 (setq l-port (match-string 2 l-host)
7539 l-host (match-string 1 l-host)))
7540
655bded0
MA
7541 ;; Set variables for computing the prompt for reading
7542 ;; password. They can also be derived from a gateway.
7543 (setq tramp-current-method (or g-method l-method)
7544 tramp-current-user (or g-user l-user)
7545 tramp-current-host (or g-host l-host))
7546
7547 ;; Replace login-args place holders.
7548 (setq
7549 l-host (or l-host "")
7550 l-user (or l-user "")
7551 l-port (or l-port "")
7552 spec (format-spec-make
7553 ?h l-host ?u l-user ?p l-port ?t tmpfile)
7554 command
7555 (concat
7556 ;; We do not want to see the trailing local prompt in
7557 ;; `start-file-process'.
7558 (unless (memq system-type '(windows-nt)) "exec ")
7559 command " "
7560 (mapconcat
7561 (lambda (x)
7562 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7563 (unless (member "" x) (mapconcat 'identity x " ")))
7564 login-args " ")
7565 ;; Local shell could be a Windows COMSPEC. It
7566 ;; doesn't know the ";" syntax, but we must exit
7567 ;; always for `start-file-process'. "exec" does not
7568 ;; work either.
7569 (if (memq system-type '(windows-nt)) " && exit || exit")))
7570
7571 ;; Send the command.
7572 (tramp-message vec 3 "Sending command `%s'" command)
7573 (tramp-send-command vec command t t)
7574 (tramp-process-actions p vec tramp-actions-before-shell 60)
7575 (tramp-message
7576 vec 3 "Found remote shell prompt on `%s'" l-host))
7577 ;; Next hop.
7578 (setq target-alist (cdr target-alist)))
7579
7580 ;; Make initial shell settings.
7581 (tramp-open-connection-setup-interactive-shell p vec)))))))
00d6fd04
MA
7582
7583(defun tramp-send-command (vec command &optional neveropen nooutput)
7584 "Send the COMMAND to connection VEC.
7585Erases temporary buffer before sending the command. If optional
7586arg NEVEROPEN is non-nil, never try to open the connection. This
7587is meant to be used from `tramp-maybe-open-connection' only. The
7588function waits for output unless NOOUTPUT is set."
7589 (unless neveropen (tramp-maybe-open-connection vec))
7590 (let ((p (tramp-get-connection-process vec)))
8950769a 7591 (when (tramp-get-connection-property p "remote-echo" nil)
00d6fd04
MA
7592 ;; We mark the command string that it can be erased in the output buffer.
7593 (tramp-set-connection-property p "check-remote-echo" t)
7594 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7595 (tramp-message vec 6 "%s" command)
7596 (tramp-send-string vec command)
7597 (unless nooutput (tramp-wait-for-output p))))
7598
00d6fd04 7599(defun tramp-wait-for-output (proc &optional timeout)
7e5686f0
MA
7600 "Wait for output from remote command."
7601 (unless (buffer-live-p (process-buffer proc))
7602 (delete-process proc)
7603 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
00d6fd04 7604 (with-current-buffer (process-buffer proc)
dab816a9 7605 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
bede3e9f 7606 ;; be leading escape sequences, which must be ignored.
dab816a9 7607 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
bede3e9f
MA
7608 ;; Sometimes, the commands do not return a newline but a
7609 ;; null byte before the shell prompt, for example "git
7610 ;; ls-files -c -z ...".
7611 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7612 (found (tramp-wait-for-regexp proc timeout regexp1)))
00d6fd04
MA
7613 (if found
7614 (let (buffer-read-only)
7e5686f0
MA
7615 ;; A simple-minded busybox has sent " ^H" sequences.
7616 ;; Delete them.
7617 (goto-char (point-min))
7618 (when (re-search-forward
7619 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
7620 (forward-line 1)
7621 (delete-region (point-min) (point)))
7622 ;; Delete the prompt.
00d6fd04 7623 (goto-char (point-max))
0664ff72 7624 (re-search-backward regexp nil t)
00d6fd04
MA
7625 (delete-region (point) (point-max)))
7626 (if timeout
7627 (tramp-error
7628 proc 'file-error
7629 "[[Remote prompt `%s' not found in %d secs]]"
7630 tramp-end-of-output timeout)
7631 (tramp-error
7632 proc 'file-error
7633 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7634 ;; Return value is whether end-of-output sentinel was found.
7635 found)))
fb7933a3 7636
b593f105
MA
7637(defun tramp-send-command-and-check
7638 (vec command &optional subshell dont-suppress-err)
fb7933a3 7639 "Run COMMAND and check its exit status.
fb7933a3
KG
7640Sends `echo $?' along with the COMMAND for checking the exit status. If
7641COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7642
b593f105
MA
7643If the optional argument SUBSHELL is non-nil, the command is
7644executed in a subshell, ie surrounded by parentheses. If
7645DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
00d6fd04
MA
7646 (tramp-send-command
7647 vec
7648 (concat (if subshell "( " "")
7649 command
b593f105 7650 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
00d6fd04 7651 "echo tramp_exit_status $?"
b593f105 7652 (if subshell " )" "")))
00d6fd04
MA
7653 (with-current-buffer (tramp-get-connection-buffer vec)
7654 (goto-char (point-max))
7655 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7656 (tramp-error
7657 vec 'file-error "Couldn't find exit status of `%s'" command))
7658 (skip-chars-forward "^ ")
7659 (prog1
7660 (read (current-buffer))
7661 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7662
7663(defun tramp-barf-unless-okay (vec command fmt &rest args)
fb7933a3
KG
7664 "Run COMMAND, check exit status, throw error if exit status not okay.
7665Similar to `tramp-send-command-and-check' but accepts two more arguments
7666FMT and ARGS which are passed to `error'."
00d6fd04
MA
7667 (unless (zerop (tramp-send-command-and-check vec command))
7668 (apply 'tramp-error vec 'file-error fmt args)))
7669
7670(defun tramp-send-command-and-read (vec command)
7671 "Run COMMAND and return the output, which must be a Lisp expression.
7672In case there is no valid Lisp expression, it raises an error"
7673 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7674 (with-current-buffer (tramp-get-connection-buffer vec)
7675 ;; Read the expression.
7676 (goto-char (point-min))
7677 (condition-case nil
7678 (prog1 (read (current-buffer))
7679 ;; Error handling.
9e6ab520 7680 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
9ce8462a 7681 (error nil)))
00d6fd04
MA
7682 (error (tramp-error
7683 vec 'file-error
7684 "`%s' does not return a valid Lisp expression: `%s'"
7685 command (buffer-string))))))
fb7933a3 7686
7432277c
KG
7687;; It seems that Tru64 Unix does not like it if long strings are sent
7688;; to it in one go. (This happens when sending the Perl
7689;; `file-attributes' implementation, for instance.) Therefore, we
27e813fe 7690;; have this function which sends the string in chunks.
00d6fd04
MA
7691(defun tramp-send-string (vec string)
7692 "Send the STRING via connection VEC.
7432277c
KG
7693
7694The STRING is expected to use Unix line-endings, but the lines sent to
7695the remote host use line-endings as defined in the variable
00d6fd04
MA
7696`tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7697 (let* ((p (tramp-get-connection-process vec))
7698 (chunksize (tramp-get-connection-property p "chunksize" nil)))
7699 (unless p
7700 (tramp-error
7701 vec 'file-error "Can't send string to remote host -- not logged in"))
7702 (tramp-set-connection-property p "last-cmd-time" (current-time))
7703 (tramp-message vec 10 "%s" string)
7704 (with-current-buffer (tramp-get-connection-buffer vec)
7705 ;; Clean up the buffer. We cannot call `erase-buffer' because
7706 ;; narrowing might be in effect.
7707 (let (buffer-read-only) (delete-region (point-min) (point-max)))
27e813fe 7708 ;; Replace "\n" by `tramp-rsh-end-of-line'.
00d6fd04
MA
7709 (setq string
7710 (mapconcat 'identity
70c11b0b 7711 (tramp-compat-split-string string "\n")
00d6fd04
MA
7712 tramp-rsh-end-of-line))
7713 (unless (or (string= string "")
7714 (string-equal (substring string -1) tramp-rsh-end-of-line))
7715 (setq string (concat string tramp-rsh-end-of-line)))
27e813fe 7716 ;; Send the string.
00d6fd04
MA
7717 (if (and chunksize (not (zerop chunksize)))
7718 (let ((pos 0)
7719 (end (length string)))
7720 (while (< pos end)
7721 (tramp-message
7722 vec 10 "Sending chunk from %s to %s"
7723 pos (min (+ pos chunksize) end))
7724 (process-send-string
7725 p (substring string pos (min (+ pos chunksize) end)))
7726 (setq pos (+ pos chunksize))))
7727 (process-send-string p string)))))
fb7933a3
KG
7728
7729(defun tramp-mode-string-to-int (mode-string)
7730 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
f3c071dd
MA
7731 (let* (case-fold-search
7732 (mode-chars (string-to-vector mode-string))
fb7933a3
KG
7733 (owner-read (aref mode-chars 1))
7734 (owner-write (aref mode-chars 2))
7735 (owner-execute-or-setid (aref mode-chars 3))
7736 (group-read (aref mode-chars 4))
7737 (group-write (aref mode-chars 5))
7738 (group-execute-or-setid (aref mode-chars 6))
7739 (other-read (aref mode-chars 7))
7740 (other-write (aref mode-chars 8))
7741 (other-execute-or-sticky (aref mode-chars 9)))
7742 (save-match-data
7743 (logior
f3c071dd
MA
7744 (cond
7745 ((char-equal owner-read ?r) (tramp-octal-to-decimal "00400"))
7746 ((char-equal owner-read ?-) 0)
7747 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7748 (cond
7749 ((char-equal owner-write ?w) (tramp-octal-to-decimal "00200"))
7750 ((char-equal owner-write ?-) 0)
7751 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7752 (cond
7753 ((char-equal owner-execute-or-setid ?x)
7754 (tramp-octal-to-decimal "00100"))
7755 ((char-equal owner-execute-or-setid ?S)
7756 (tramp-octal-to-decimal "04000"))
7757 ((char-equal owner-execute-or-setid ?s)
7758 (tramp-octal-to-decimal "04100"))
7759 ((char-equal owner-execute-or-setid ?-) 0)
7760 (t (error "Fourth char `%c' must be one of `xsS-'"
7761 owner-execute-or-setid)))
7762 (cond
7763 ((char-equal group-read ?r) (tramp-octal-to-decimal "00040"))
7764 ((char-equal group-read ?-) 0)
7765 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7766 (cond
7767 ((char-equal group-write ?w) (tramp-octal-to-decimal "00020"))
7768 ((char-equal group-write ?-) 0)
7769 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7770 (cond
7771 ((char-equal group-execute-or-setid ?x)
7772 (tramp-octal-to-decimal "00010"))
7773 ((char-equal group-execute-or-setid ?S)
7774 (tramp-octal-to-decimal "02000"))
7775 ((char-equal group-execute-or-setid ?s)
7776 (tramp-octal-to-decimal "02010"))
7777 ((char-equal group-execute-or-setid ?-) 0)
7778 (t (error "Seventh char `%c' must be one of `xsS-'"
7779 group-execute-or-setid)))
7780 (cond
7781 ((char-equal other-read ?r)
7782 (tramp-octal-to-decimal "00004"))
7783 ((char-equal other-read ?-) 0)
7784 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7785 (cond
7786 ((char-equal other-write ?w) (tramp-octal-to-decimal "00002"))
7787 ((char-equal other-write ?-) 0)
fb7933a3 7788 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
f3c071dd
MA
7789 (cond
7790 ((char-equal other-execute-or-sticky ?x)
7791 (tramp-octal-to-decimal "00001"))
7792 ((char-equal other-execute-or-sticky ?T)
7793 (tramp-octal-to-decimal "01000"))
7794 ((char-equal other-execute-or-sticky ?t)
7795 (tramp-octal-to-decimal "01001"))
7796 ((char-equal other-execute-or-sticky ?-) 0)
7797 (t (error "Tenth char `%c' must be one of `xtT-'"
7798 other-execute-or-sticky)))))))
fb7933a3 7799
00d6fd04
MA
7800(defun tramp-convert-file-attributes (vec attr)
7801 "Convert file-attributes ATTR generated by perl script, stat or ls.
c82c5727
LH
7802Convert file mode bits to string and set virtual device number.
7803Return ATTR."
680db9ac
MA
7804 (when attr
7805 ;; Convert last access time.
7806 (unless (listp (nth 4 attr))
7807 (setcar (nthcdr 4 attr)
7808 (list (floor (nth 4 attr) 65536)
7809 (floor (mod (nth 4 attr) 65536)))))
7810 ;; Convert last modification time.
7811 (unless (listp (nth 5 attr))
7812 (setcar (nthcdr 5 attr)
7813 (list (floor (nth 5 attr) 65536)
7814 (floor (mod (nth 5 attr) 65536)))))
7815 ;; Convert last status change time.
7816 (unless (listp (nth 6 attr))
7817 (setcar (nthcdr 6 attr)
7818 (list (floor (nth 6 attr) 65536)
7819 (floor (mod (nth 6 attr) 65536)))))
7820 ;; Convert file size.
7821 (when (< (nth 7 attr) 0)
7822 (setcar (nthcdr 7 attr) -1))
7823 (when (and (floatp (nth 7 attr))
7824 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7825 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7826 ;; Convert file mode bits to string.
7827 (unless (stringp (nth 8 attr))
7828 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7829 (when (stringp (car attr))
7830 (aset (nth 8 attr) 0 ?l)))
7831 ;; Convert directory indication bit.
7832 (when (string-match "^d" (nth 8 attr))
7833 (setcar attr t))
7834 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7835 (when (consp (car attr))
7836 (if (and (stringp (caar attr))
7837 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7838 (setcar attr (match-string 1 (caar attr)))
7839 (setcar attr nil)))
7840 ;; Set file's gid change bit.
7841 (setcar (nthcdr 9 attr)
7842 (if (numberp (nth 3 attr))
7843 (not (= (nth 3 attr)
7844 (tramp-get-remote-gid vec 'integer)))
7845 (not (string-equal
7846 (nth 3 attr)
7847 (tramp-get-remote-gid vec 'string)))))
7848 ;; Convert inode.
7849 (unless (listp (nth 10 attr))
7850 (setcar (nthcdr 10 attr)
7851 (condition-case nil
7852 (cons (floor (nth 10 attr) 65536)
7853 (floor (mod (nth 10 attr) 65536)))
7854 ;; Inodes can be incredible huge. We must hide this.
7855 (error (tramp-get-inode vec)))))
7856 ;; Set virtual device number.
7857 (setcar (nthcdr 11 attr)
7858 (tramp-get-device vec))
7859 attr))
c82c5727 7860
293c24f9
MA
7861(defun tramp-check-cached-permissions (vec access)
7862 "Check `file-attributes' caches for VEC.
7863Return t if according to the cache access type ACCESS is known to
7864be granted."
7865 (let ((result nil)
7866 (offset (cond
7867 ((eq ?r access) 1)
7868 ((eq ?w access) 2)
7869 ((eq ?x access) 3))))
7870 (dolist (suffix '("string" "integer") result)
7871 (setq
7872 result
7873 (or
7874 result
7875 (let ((file-attr
7876 (tramp-get-file-property
7877 vec (tramp-file-name-localname vec)
7878 (concat "file-attributes-" suffix) nil))
7879 (remote-uid
7880 (tramp-get-connection-property
7881 vec (concat "uid-" suffix) nil))
7882 (remote-gid
7883 (tramp-get-connection-property
7884 vec (concat "gid-" suffix) nil)))
7885 (and
7886 file-attr
7887 (or
7888 ;; Not a symlink
7889 (eq t (car file-attr))
7890 (null (car file-attr)))
7891 (or
7892 ;; World accessible.
7893 (eq access (aref (nth 8 file-attr) (+ offset 6)))
7894 ;; User accessible and owned by user.
7895 (and
7896 (eq access (aref (nth 8 file-attr) offset))
7897 (equal remote-uid (nth 2 file-attr)))
7898 ;; Group accessible and owned by user's
7899 ;; principal group.
7900 (and
7901 (eq access (aref (nth 8 file-attr) (+ offset 3)))
7902 (equal remote-gid (nth 3 file-attr)))))))))))
7903
ce3f516f 7904(defun tramp-get-inode (vec)
00d6fd04
MA
7905 "Returns the virtual inode number.
7906If it doesn't exist, generate a new one."
ce3f516f
MA
7907 (let ((string (tramp-make-tramp-file-name
7908 (tramp-file-name-method vec)
7909 (tramp-file-name-user vec)
7910 (tramp-file-name-host vec)
7911 "")))
00d6fd04
MA
7912 (unless (assoc string tramp-inodes)
7913 (add-to-list 'tramp-inodes
7914 (list string (length tramp-inodes))))
7915 (nth 1 (assoc string tramp-inodes))))
7916
7917(defun tramp-get-device (vec)
c82c5727
LH
7918 "Returns the virtual device number.
7919If it doesn't exist, generate a new one."
00d6fd04
MA
7920 (let ((string (tramp-make-tramp-file-name
7921 (tramp-file-name-method vec)
7922 (tramp-file-name-user vec)
7923 (tramp-file-name-host vec)
7924 "")))
c82c5727
LH
7925 (unless (assoc string tramp-devices)
7926 (add-to-list 'tramp-devices
7927 (list string (length tramp-devices))))
b946a456 7928 (cons -1 (nth 1 (assoc string tramp-devices)))))
fb7933a3
KG
7929
7930(defun tramp-file-mode-from-int (mode)
7931 "Turn an integer representing a file mode into an ls(1)-like string."
7932 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
7933 (user (logand (lsh mode -6) 7))
7934 (group (logand (lsh mode -3) 7))
7935 (other (logand (lsh mode -0) 7))
7936 (suid (> (logand (lsh mode -9) 4) 0))
7937 (sgid (> (logand (lsh mode -9) 2) 0))
7938 (sticky (> (logand (lsh mode -9) 1) 0)))
7939 (setq user (tramp-file-mode-permissions user suid "s"))
7940 (setq group (tramp-file-mode-permissions group sgid "s"))
7941 (setq other (tramp-file-mode-permissions other sticky "t"))
7942 (concat type user group other)))
7943
fb7933a3
KG
7944(defun tramp-file-mode-permissions (perm suid suid-text)
7945 "Convert a permission bitset into a string.
7946This is used internally by `tramp-file-mode-from-int'."
7947 (let ((r (> (logand perm 4) 0))
7948 (w (> (logand perm 2) 0))
7949 (x (> (logand perm 1) 0)))
7950 (concat (or (and r "r") "-")
7951 (or (and w "w") "-")
7952 (or (and suid x suid-text) ; suid, execute
7953 (and suid (upcase suid-text)) ; suid, !execute
7954 (and x "x") "-")))) ; !suid
7955
fb7933a3
KG
7956(defun tramp-decimal-to-octal (i)
7957 "Return a string consisting of the octal digits of I.
7958Not actually used. Use `(format \"%o\" i)' instead?"
7959 (cond ((< i 0) (error "Cannot convert negative number to octal"))
7960 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
7961 ((zerop i) "0")
7962 (t (concat (tramp-decimal-to-octal (/ i 8))
7963 (number-to-string (% i 8))))))
7964
fb7933a3
KG
7965;; Kudos to Gerd Moellmann for this suggestion.
7966(defun tramp-octal-to-decimal (ostr)
7967 "Given a string of octal digits, return a decimal number."
7968 (let ((x (or ostr "")))
7969 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7970 (unless (string-match "\\`[0-7]*\\'" x)
7971 (error "Non-octal junk in string `%s'" x))
7972 (string-to-number ostr 8)))
7973
7974(defun tramp-shell-case-fold (string)
7975 "Converts STRING to shell glob pattern which ignores case."
7976 (mapconcat
7977 (lambda (c)
7978 (if (equal (downcase c) (upcase c))
7979 (vector c)
7980 (format "[%c%c]" (downcase c) (upcase c))))
7981 string
7982 ""))
7983
7984
bf247b6e 7985;; ------------------------------------------------------------
a4aeb9a4 7986;; -- Tramp file names --
bf247b6e 7987;; ------------------------------------------------------------
fb7933a3
KG
7988;; Conversion functions between external representation and
7989;; internal data structure. Convenience functions for internal
7990;; data structure.
7991
00d6fd04 7992(defun tramp-file-name-p (vec)
a94d821f 7993 "Check, whether VEC is a Tramp object."
00d6fd04
MA
7994 (and (vectorp vec) (= 4 (length vec))))
7995
7996(defun tramp-file-name-method (vec)
7997 "Return method component of VEC."
7998 (and (tramp-file-name-p vec) (aref vec 0)))
7999
8000(defun tramp-file-name-user (vec)
8001 "Return user component of VEC."
8002 (and (tramp-file-name-p vec) (aref vec 1)))
8003
8004(defun tramp-file-name-host (vec)
8005 "Return host component of VEC."
8006 (and (tramp-file-name-p vec) (aref vec 2)))
8007
8008(defun tramp-file-name-localname (vec)
8009 "Return localname component of VEC."
8010 (and (tramp-file-name-p vec) (aref vec 3)))
8011
dea31ca6 8012;; The user part of a Tramp file name vector can be of kind
b96e6899 8013;; "user%domain". Sometimes, we must extract these parts.
dea31ca6
MA
8014(defun tramp-file-name-real-user (vec)
8015 "Return the user name of VEC without domain."
a17632c1
MA
8016 (save-match-data
8017 (let ((user (tramp-file-name-user vec)))
8018 (if (and (stringp user)
8019 (string-match tramp-user-with-domain-regexp user))
8020 (match-string 1 user)
8021 user))))
dea31ca6
MA
8022
8023(defun tramp-file-name-domain (vec)
8024 "Return the domain name of VEC."
a17632c1
MA
8025 (save-match-data
8026 (let ((user (tramp-file-name-user vec)))
8027 (and (stringp user)
8028 (string-match tramp-user-with-domain-regexp user)
8029 (match-string 2 user)))))
dea31ca6 8030
00d6fd04
MA
8031;; The host part of a Tramp file name vector can be of kind
8032;; "host#port". Sometimes, we must extract these parts.
8a4438b6 8033(defun tramp-file-name-real-host (vec)
00d6fd04 8034 "Return the host name of VEC without port."
a17632c1
MA
8035 (save-match-data
8036 (let ((host (tramp-file-name-host vec)))
8037 (if (and (stringp host)
8038 (string-match tramp-host-with-port-regexp host))
8039 (match-string 1 host)
8040 host))))
00d6fd04 8041
8a4438b6 8042(defun tramp-file-name-port (vec)
00d6fd04 8043 "Return the port number of VEC."
a17632c1
MA
8044 (save-match-data
8045 (let ((host (tramp-file-name-host vec)))
8046 (and (stringp host)
8047 (string-match tramp-host-with-port-regexp host)
8048 (string-to-number (match-string 2 host))))))
fb7933a3
KG
8049
8050(defun tramp-tramp-file-p (name)
a09dc9bf 8051 "Return t if NAME is a string with Tramp file name syntax."
fb7933a3 8052 (save-match-data
a09dc9bf 8053 (and (stringp name) (string-match tramp-file-name-regexp name))))
bf247b6e 8054
8a4438b6 8055(defun tramp-find-method (method user host)
00d6fd04
MA
8056 "Return the right method string to use.
8057This is METHOD, if non-nil. Otherwise, do a lookup in
8058`tramp-default-method-alist'."
8059 (or method
8060 (let ((choices tramp-default-method-alist)
8061 lmethod item)
8062 (while choices
8063 (setq item (pop choices))
8064 (when (and (string-match (or (nth 0 item) "") (or host ""))
8065 (string-match (or (nth 1 item) "") (or user "")))
8066 (setq lmethod (nth 2 item))
8067 (setq choices nil)))
8068 lmethod)
8069 tramp-default-method))
8070
8a4438b6 8071(defun tramp-find-user (method user host)
00d6fd04
MA
8072 "Return the right user string to use.
8073This is USER, if non-nil. Otherwise, do a lookup in
8074`tramp-default-user-alist'."
8075 (or user
8076 (let ((choices tramp-default-user-alist)
8077 luser item)
8078 (while choices
8079 (setq item (pop choices))
8080 (when (and (string-match (or (nth 0 item) "") (or method ""))
8081 (string-match (or (nth 1 item) "") (or host "")))
8082 (setq luser (nth 2 item))
8083 (setq choices nil)))
8084 luser)
8085 tramp-default-user))
8086
8a4438b6 8087(defun tramp-find-host (method user host)
00d6fd04
MA
8088 "Return the right host string to use.
8089This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
8090 (or (and (> (length host) 0) host)
8091 tramp-default-host))
8092
9ce8462a 8093(defun tramp-dissect-file-name (name &optional nodefault)
00d6fd04 8094 "Return a `tramp-file-name' structure.
9ce8462a
MA
8095The structure consists of remote method, remote user, remote host
8096and localname (file name on remote host). If NODEFAULT is
8097non-nil, the file name parts are not expanded to their default
8098values."
4007ba5b 8099 (save-match-data
00d6fd04 8100 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
a4aeb9a4 8101 (unless match (error "Not a Tramp file name: %s" name))
00d6fd04
MA
8102 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
8103 (user (match-string (nth 2 tramp-file-name-structure) name))
8104 (host (match-string (nth 3 tramp-file-name-structure) name))
8105 (localname (match-string (nth 4 tramp-file-name-structure) name)))
5d449a36
MA
8106 (when (member method '("multi" "multiu"))
8107 (error
8108 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
8109 method))
b96e6899
MA
8110 (when host
8111 (when (string-match tramp-prefix-ipv6-regexp host)
8112 (setq host (replace-match "" nil t host)))
8113 (when (string-match tramp-postfix-ipv6-regexp host)
8114 (setq host (replace-match "" nil t host))))
9ce8462a
MA
8115 (if nodefault
8116 (vector method user host localname)
8117 (vector
8118 (tramp-find-method method user host)
8119 (tramp-find-user method user host)
8120 (tramp-find-host method user host)
8121 localname))))))
00d6fd04
MA
8122
8123(defun tramp-equal-remote (file1 file2)
a94d821f 8124 "Check, whether the remote parts of FILE1 and FILE2 are identical.
00d6fd04
MA
8125The check depends on method, user and host name of the files. If
8126one of the components is missing, the default values are used.
8127The local file name parts of FILE1 and FILE2 are not taken into
8128account.
fb7933a3 8129
00d6fd04
MA
8130Example:
8131
8132 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
8133
8134would yield `t'. On the other hand, the following check results in nil:
8135
8136 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
9e6ab520
MA
8137 (and (stringp (file-remote-p file1))
8138 (stringp (file-remote-p file2))
94be87e8 8139 (string-equal (file-remote-p file1) (file-remote-p file2))))
00d6fd04
MA
8140
8141(defun tramp-make-tramp-file-name (method user host localname)
8142 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
8143 (concat tramp-prefix-format
8144 (when (not (zerop (length method)))
8145 (concat method tramp-postfix-method-format))
8146 (when (not (zerop (length user)))
8147 (concat user tramp-postfix-user-format))
b96e6899
MA
8148 (when host
8149 (if (string-match tramp-ipv6-regexp host)
8150 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8151 host))
8152 tramp-postfix-host-format
00d6fd04
MA
8153 (when localname localname)))
8154
8155(defun tramp-completion-make-tramp-file-name (method user host localname)
8156 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
8157It must not be a complete Tramp file name, but as long as there are
8158necessary only. This function will be used in file name completion."
8159 (concat tramp-prefix-format
8160 (when (not (zerop (length method)))
8161 (concat method tramp-postfix-method-format))
8162 (when (not (zerop (length user)))
8163 (concat user tramp-postfix-user-format))
8164 (when (not (zerop (length host)))
b96e6899
MA
8165 (concat
8166 (if (string-match tramp-ipv6-regexp host)
8167 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8168 host)
8169 tramp-postfix-host-format))
00d6fd04
MA
8170 (when localname localname)))
8171
8172(defun tramp-make-copy-program-file-name (vec)
8173 "Create a file name suitable to be passed to `rcp' and workalikes."
8174 (let ((user (tramp-file-name-user vec))
0f205eee 8175 (host (tramp-file-name-real-host vec))
00d6fd04
MA
8176 (localname (tramp-shell-quote-argument
8177 (tramp-file-name-localname vec))))
8178 (if (not (zerop (length user)))
8179 (format "%s@%s:%s" user host localname)
8180 (format "%s:%s" host localname))))
8181
7f49fe46 8182(defun tramp-method-out-of-band-p (vec size)
38c65fca 8183 "Return t if this is an out-of-band method, nil otherwise."
7f49fe46
MA
8184 (and
8185 ;; It shall be an out-of-band method.
8186 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
8187 ;; Either the file size is large enough, or (in rare cases) there
8188 ;; does not exist a remote encoding.
6a29a838
MA
8189 (or (null tramp-copy-size-limit)
8190 (> size tramp-copy-size-limit)
8191 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
fb7933a3 8192
0f205eee
MA
8193(defun tramp-local-host-p (vec)
8194 "Return t if this points to the local host, nil otherwise."
c992abdb
MA
8195 ;; We cannot use `tramp-file-name-real-host'. A port is an
8196 ;; indication for an ssh tunnel or alike.
8197 (let ((host (tramp-file-name-host vec)))
0f205eee
MA
8198 (and
8199 (stringp host)
b96e6899 8200 (string-match tramp-local-host-regexp host)
46bcd78c
MA
8201 ;; The method shall be applied to one of the shell file name
8202 ;; handler. `tramp-local-host-p' is also called for "smb" and
8203 ;; alike, where it must fail.
8204 (tramp-get-method-parameter
8205 (tramp-file-name-method vec) 'tramp-login-program)
a0a5183a
MA
8206 ;; The local temp directory must be writable for the other user.
8207 (file-writable-p
8208 (tramp-make-tramp-file-name
8209 (tramp-file-name-method vec)
8210 (tramp-file-name-user vec)
8211 host
93c3eb7c
MA
8212 (tramp-compat-temporary-file-directory)))
8213 ;; On some systems, chown runs only for root.
8214 (or (zerop (user-uid))
8215 (zerop (tramp-get-remote-uid vec 'integer))))))
0f205eee 8216
fb7933a3
KG
8217;; Variables local to connection.
8218
f84638eb 8219(defun tramp-get-remote-path (vec)
70c11b0b
MA
8220 (with-connection-property
8221 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8222 ;; cache the result for the session only. Otherwise, the result
8223 ;; is cached persistently.
8224 (if (memq 'tramp-own-remote-path tramp-remote-path)
8225 (tramp-get-connection-process vec)
8226 vec)
8227 "remote-path"
b533bc97 8228 (let* ((remote-path (copy-tree tramp-remote-path))
70c11b0b
MA
8229 (elt1 (memq 'tramp-default-remote-path remote-path))
8230 (elt2 (memq 'tramp-own-remote-path remote-path))
f84638eb 8231 (default-remote-path
70c11b0b 8232 (when elt1
f84638eb 8233 (condition-case nil
70c11b0b
MA
8234 (tramp-send-command-and-read
8235 vec "echo \\\"`getconf PATH`\\\"")
f84638eb
MA
8236 ;; Default if "getconf" is not available.
8237 (error
8238 (tramp-message
8239 vec 3
8240 "`getconf PATH' not successful, using default value \"%s\"."
8241 "/bin:/usr/bin")
70c11b0b
MA
8242 "/bin:/usr/bin"))))
8243 (own-remote-path
8244 (when elt2
8245 (condition-case nil
8246 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
8247 ;; Default if "getconf" is not available.
8248 (error
8249 (tramp-message
8250 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8251 nil)))))
8252
8253 ;; Replace place holder `tramp-default-remote-path'.
8254 (when elt1
8255 (setcdr elt1
f84638eb 8256 (append
70c11b0b
MA
8257 (tramp-compat-split-string default-remote-path ":")
8258 (cdr elt1)))
f84638eb
MA
8259 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
8260
70c11b0b
MA
8261 ;; Replace place holder `tramp-own-remote-path'.
8262 (when elt2
8263 (setcdr elt2
8264 (append
8265 (tramp-compat-split-string own-remote-path ":")
8266 (cdr elt2)))
8267 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
8268
8269 ;; Remove double entries.
8270 (setq elt1 remote-path)
8271 (while (consp elt1)
8272 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
8273 (setcar elt2 nil))
8274 (setq elt1 (cdr elt1)))
8275
f84638eb
MA
8276 ;; Remove non-existing directories.
8277 (delq
8278 nil
8279 (mapcar
8280 (lambda (x)
8281 (and
70c11b0b
MA
8282 (stringp x)
8283 (file-directory-p
8284 (tramp-make-tramp-file-name
8285 (tramp-file-name-method vec)
8286 (tramp-file-name-user vec)
8287 (tramp-file-name-host vec)
8288 x))
f84638eb
MA
8289 x))
8290 remote-path)))))
8291
a4aeb9a4
MA
8292(defun tramp-get-remote-tmpdir (vec)
8293 (with-connection-property vec "tmp-directory"
8294 (let ((dir (tramp-shell-quote-argument "/tmp")))
8295 (if (and (zerop
8296 (tramp-send-command-and-check
8297 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
8298 (zerop
8299 (tramp-send-command-and-check
8300 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
8301 dir
8302 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
8303
00d6fd04
MA
8304(defun tramp-get-ls-command (vec)
8305 (with-connection-property vec "ls"
946a5aeb
MA
8306 (tramp-message vec 5 "Finding a suitable `ls' command")
8307 (or
8308 (catch 'ls-found
8309 (dolist (cmd '("ls" "gnuls" "gls"))
8310 (let ((dl (tramp-get-remote-path vec))
8311 result)
8312 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
7e5686f0
MA
8313 ;; Check parameters. On busybox, "ls" output coloring is
8314 ;; enabled by default sometimes. So we try to disable it
8315 ;; when possible. $LS_COLORING is not supported there.
946a5aeb
MA
8316 (when (zerop (tramp-send-command-and-check
8317 vec (format "%s -lnd /" result)))
7e5686f0
MA
8318 (when (zerop (tramp-send-command-and-check
8319 vec (format "%s --color=never /" result)))
8320 (setq result (concat result " --color=never")))
946a5aeb
MA
8321 (throw 'ls-found result))
8322 (setq dl (cdr dl))))))
8323 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
00d6fd04 8324
8e754ea2
MA
8325(defun tramp-get-ls-command-with-dired (vec)
8326 (save-match-data
8327 (with-connection-property vec "ls-dired"
8328 (tramp-message vec 5 "Checking, whether `ls --dired' works")
8329 (zerop (tramp-send-command-and-check
7f49fe46 8330 vec (format "%s --dired /" (tramp-get-ls-command vec)))))))
8e754ea2 8331
00d6fd04
MA
8332(defun tramp-get-test-command (vec)
8333 (with-connection-property vec "test"
946a5aeb
MA
8334 (tramp-message vec 5 "Finding a suitable `test' command")
8335 (if (zerop (tramp-send-command-and-check vec "test 0"))
8336 "test"
8337 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
00d6fd04
MA
8338
8339(defun tramp-get-test-nt-command (vec)
8340 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8341 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8342 ;; for otherwise the shell crashes.
8343 (with-connection-property vec "test-nt"
8344 (or
8345 (progn
8346 (tramp-send-command
8347 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
8348 (with-current-buffer (tramp-get-buffer vec)
8349 (goto-char (point-min))
a0a5183a 8350 (when (looking-at (regexp-quote tramp-end-of-output))
00d6fd04
MA
8351 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
8352 (progn
8353 (tramp-send-command
8354 vec
8355 (format
8356 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8357 (tramp-get-test-command vec)))
8358 "tramp_test_nt %s %s"))))
8359
8360(defun tramp-get-file-exists-command (vec)
8361 (with-connection-property vec "file-exists"
946a5aeb
MA
8362 (tramp-message vec 5 "Finding command to check if file exists")
8363 (tramp-find-file-exists-command vec)))
00d6fd04
MA
8364
8365(defun tramp-get-remote-ln (vec)
8366 (with-connection-property vec "ln"
946a5aeb
MA
8367 (tramp-message vec 5 "Finding a suitable `ln' command")
8368 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
00d6fd04
MA
8369
8370(defun tramp-get-remote-perl (vec)
8371 (with-connection-property vec "perl"
946a5aeb 8372 (tramp-message vec 5 "Finding a suitable `perl' command")
293c24f9
MA
8373 (let ((result
8374 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
8375 (tramp-find-executable
8376 vec "perl" (tramp-get-remote-path vec)))))
8377 ;; We must check also for some Perl modules.
8378 (when result
8379 (with-connection-property vec "perl-file-spec"
8380 (zerop
8381 (tramp-send-command-and-check
8382 vec (format "%s -e 'use File::Spec;'" result))))
8383 (with-connection-property vec "perl-cwd-realpath"
8384 (zerop
8385 (tramp-send-command-and-check
8386 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
8387 result)))
00d6fd04
MA
8388
8389(defun tramp-get-remote-stat (vec)
8390 (with-connection-property vec "stat"
946a5aeb
MA
8391 (tramp-message vec 5 "Finding a suitable `stat' command")
8392 (let ((result (tramp-find-executable
8393 vec "stat" (tramp-get-remote-path vec)))
8394 tmp)
8395 ;; Check whether stat(1) returns usable syntax. %s does not
8396 ;; work on older AIX systems.
8397 (when result
8398 (setq tmp
8399 ;; We don't want to display an error message.
8400 (with-temp-message (or (current-message) "")
8401 (condition-case nil
8402 (tramp-send-command-and-read
8403 vec (format "%s -c '(\"%%N\" %%s)' /" result))
8404 (error nil))))
8405 (unless (and (listp tmp) (stringp (car tmp))
8406 (string-match "^./.$" (car tmp))
8407 (integerp (cadr tmp)))
8408 (setq result nil)))
8409 result)))
fb7933a3 8410
293c24f9
MA
8411(defun tramp-get-remote-readlink (vec)
8412 (with-connection-property vec "readlink"
8413 (tramp-message vec 5 "Finding a suitable `readlink' command")
8414 (let ((result (tramp-find-executable
8415 vec "readlink" (tramp-get-remote-path vec))))
8416 (when (and result
8417 ;; We don't want to display an error message.
8418 (with-temp-message (or (current-message) "")
8419 (condition-case nil
8420 (zerop
8421 (tramp-send-command-and-check
8422 vec (format "%s --canonicalize-missing /" result)))
8423 (error nil))))
8424 result))))
8425
b6827fff
MA
8426(defun tramp-get-remote-trash (vec)
8427 (with-connection-property vec "trash"
8428 (tramp-message vec 5 "Finding a suitable `trash' command")
8429 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
8430
00d6fd04
MA
8431(defun tramp-get-remote-id (vec)
8432 (with-connection-property vec "id"
946a5aeb
MA
8433 (tramp-message vec 5 "Finding POSIX `id' command")
8434 (or
8435 (catch 'id-found
8436 (let ((dl (tramp-get-remote-path vec))
8437 result)
8438 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8439 ;; Check POSIX parameter.
8440 (when (zerop (tramp-send-command-and-check
8441 vec (format "%s -u" result)))
8442 (throw 'id-found result))
8443 (setq dl (cdr dl)))))
8444 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
00d6fd04
MA
8445
8446(defun tramp-get-remote-uid (vec id-format)
8447 (with-connection-property vec (format "uid-%s" id-format)
8448 (let ((res (tramp-send-command-and-read
8449 vec
8450 (format "%s -u%s %s"
8451 (tramp-get-remote-id vec)
8452 (if (equal id-format 'integer) "" "n")
8453 (if (equal id-format 'integer)
8454 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8455 ;; The command might not always return a number.
8456 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8457
8458(defun tramp-get-remote-gid (vec id-format)
8459 (with-connection-property vec (format "gid-%s" id-format)
8460 (let ((res (tramp-send-command-and-read
8461 vec
8462 (format "%s -g%s %s"
8463 (tramp-get-remote-id vec)
8464 (if (equal id-format 'integer) "" "n")
8465 (if (equal id-format 'integer)
8466 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8467 ;; The command might not always return a number.
8468 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
fb7933a3 8469
8d60099b
MA
8470(defun tramp-get-local-uid (id-format)
8471 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8472
8473(defun tramp-get-local-gid (id-format)
9e6ab520 8474 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8d60099b 8475
00d6fd04 8476;; Some predefined connection properties.
6a29a838
MA
8477(defun tramp-get-inline-compress (vec prop size)
8478 "Return the compress command related to PROP.
8479PROP is either `inline-compress' or `inline-decompress'. SIZE is
8480the length of the file to be compressed.
8481
8482If no corresponding command is found, nil is returned."
8483 (when (and (integerp tramp-inline-compress-start-size)
8484 (> size tramp-inline-compress-start-size))
8485 (with-connection-property vec prop
8486 (tramp-find-inline-compress vec)
8487 (tramp-get-connection-property vec prop nil))))
8488
8489(defun tramp-get-inline-coding (vec prop size)
8490 "Return the coding command related to PROP.
8491PROP is either `remote-encoding', `remode-decoding',
8492`local-encoding' or `local-decoding'.
8493
8494SIZE is the length of the file to be coded. Depending on SIZE,
8495compression might be applied.
8496
8497If no corresponding command is found, nil is returned.
8498Otherwise, either a string is returned which contains a `%s' mark
8499to be used for the respective input or output file; or a Lisp
8500function cell is returned to be applied on a buffer."
8501 (let ((coding
8502 (with-connection-property vec prop
8503 (tramp-find-inline-encoding vec)
8504 (tramp-get-connection-property vec prop nil)))
8505 (prop1 (if (string-match "encoding" prop)
8506 "inline-compress" "inline-decompress"))
8507 compress)
00d6fd04 8508 ;; The connection property might have been cached. So we must send
6a29a838
MA
8509 ;; the script to the remote side - maybe.
8510 (when (and coding (symbolp coding) (string-match "remote" prop))
8511 (let ((name (symbol-name coding)))
00d6fd04
MA
8512 (while (string-match (regexp-quote "-") name)
8513 (setq name (replace-match "_" nil t name)))
6a29a838
MA
8514 (tramp-maybe-send-script vec (symbol-value coding) name)
8515 (setq coding name)))
8516 (when coding
8517 ;; Check for the `compress' command.
8518 (setq compress (tramp-get-inline-compress vec prop1 size))
8519 ;; Return the value.
8520 (cond
8521 ((and compress (symbolp coding))
8522 (if (string-match "decompress" prop1)
8523 `(lambda (beg end)
8524 (,coding beg end)
8525 (let ((coding-system-for-write 'binary)
8526 (coding-system-for-read 'binary))
8527 (apply
8528 'call-process-region (point-min) (point-max)
8529 (car (split-string ,compress)) t t nil
8530 (cdr (split-string ,compress)))))
8531 `(lambda (beg end)
8532 (let ((coding-system-for-write 'binary)
8533 (coding-system-for-read 'binary))
8534 (apply
8535 'call-process-region beg end
8536 (car (split-string ,compress)) t t nil
8537 (cdr (split-string ,compress))))
8538 (,coding (point-min) (point-max)))))
8539 ((symbolp coding)
8540 coding)
8541 ((and compress (string-match "decoding" prop))
8542 (format "(%s | %s >%%s)" coding compress))
8543 (compress
8544 (format "(%s <%%s | %s)" compress coding))
8545 ((string-match "decoding" prop)
8546 (format "%s >%%s" coding))
8547 (t
8548 (format "%s <%%s" coding))))))
fb7933a3 8549
00d6fd04 8550(defun tramp-get-method-parameter (method param)
c951aecb 8551 "Return the method parameter PARAM.
6a29a838 8552If the `tramp-methods' entry does not exist, return nil."
00d6fd04
MA
8553 (let ((entry (assoc param (assoc method tramp-methods))))
8554 (when entry (cadr entry))))
90f8dc03 8555
fb7933a3
KG
8556;; Auto saving to a special directory.
8557
00cec167 8558(defun tramp-exists-file-name-handler (operation &rest args)
a94d821f 8559 "Check, whether OPERATION runs a file name handler."
00d6fd04
MA
8560 ;; The file name handler is determined on base of either an
8561 ;; argument, `buffer-file-name', or `default-directory'.
8562 (condition-case nil
8563 (let* ((buffer-file-name "/")
8564 (default-directory "/")
8565 (fnha file-name-handler-alist)
8566 (check-file-name-operation operation)
8567 (file-name-handler-alist
8568 (list
8569 (cons "/"
aa485f7c
MA
8570 (lambda (operation &rest args)
8571 "Returns OPERATION if it is the one to be checked."
8572 (if (equal check-file-name-operation operation)
8573 operation
8574 (let ((file-name-handler-alist fnha))
8575 (apply operation args))))))))
00d6fd04
MA
8576 (equal (apply operation args) operation))
8577 (error nil)))
c1105d05
MA
8578
8579(unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8580 (defadvice make-auto-save-file-name
8581 (around tramp-advice-make-auto-save-file-name () activate)
00d6fd04 8582 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
b533bc97 8583 (if (tramp-tramp-file-p (buffer-file-name))
7f49fe46
MA
8584 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8585 ;; directly, because this would bypass the locking mechanism.
8586 (setq ad-return-value
8587 (tramp-file-name-handler 'make-auto-save-file-name))
a69c01a0 8588 ad-do-it))
191bb792
MA
8589 (add-hook
8590 'tramp-unload-hook
8591 (lambda ()
8592 (ad-remove-advice
8593 'make-auto-save-file-name
d7ec1df7
MA
8594 'around 'tramp-advice-make-auto-save-file-name)
8595 (ad-activate 'make-auto-save-file-name))))
fb7933a3 8596
b533bc97
MA
8597;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
8598;; umask. This is a security threat.
414da5ab
MA
8599
8600(defun tramp-set-auto-save-file-modes ()
8601 "Set permissions of autosaved remote files to the original permissions."
8602 (let ((bfn (buffer-file-name)))
b533bc97 8603 (when (and (tramp-tramp-file-p bfn)
b50dd0d2 8604 (buffer-modified-p)
414da5ab 8605 (stringp buffer-auto-save-file-name)
340b8d4f
MA
8606 (not (equal bfn buffer-auto-save-file-name)))
8607 (unless (file-exists-p buffer-auto-save-file-name)
8608 (write-region "" nil buffer-auto-save-file-name))
8609 ;; Permissions should be set always, because there might be an old
8610 ;; auto-saved file belonging to another original file. This could
8611 ;; be a security threat.
7177e2a3 8612 (set-file-modes buffer-auto-save-file-name
11948172 8613 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
414da5ab 8614
b533bc97
MA
8615(unless (and (featurep 'xemacs)
8616 (= emacs-major-version 21)
8617 (> emacs-minor-version 4))
a69c01a0
MA
8618 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
8619 (add-hook 'tramp-unload-hook
aa485f7c
MA
8620 (lambda ()
8621 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
414da5ab 8622
fb7933a3
KG
8623(defun tramp-subst-strs-in-string (alist string)
8624 "Replace all occurrences of the string FROM with TO in STRING.
8625ALIST is of the form ((FROM . TO) ...)."
8626 (save-match-data
8627 (while alist
8628 (let* ((pr (car alist))
8629 (from (car pr))
8630 (to (cdr pr)))
8631 (while (string-match (regexp-quote from) string)
8632 (setq string (replace-match to t t string)))
8633 (setq alist (cdr alist))))
8634 string))
8635
fb7933a3
KG
8636;; ------------------------------------------------------------
8637;; -- Compatibility functions section --
8638;; ------------------------------------------------------------
8639
00d6fd04 8640(defun tramp-read-passwd (proc &optional prompt)
fb7933a3 8641 "Read a password from user (compat function).
5615d63f 8642Consults the auth-source package.
5ec2cc41 8643Invokes `password-read' if available, `read-passwd' else."
00d6fd04
MA
8644 (let* ((key (tramp-make-tramp-file-name
8645 tramp-current-method tramp-current-user
8646 tramp-current-host ""))
8647 (pw-prompt
8648 (or prompt
8649 (with-current-buffer (process-buffer proc)
8650 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
8651 (format "%s for %s " (capitalize (match-string 1)) key)))))
7540f029
MA
8652 (with-parsed-tramp-file-name key nil
8653 (prog1
8654 (or
8655 ;; See if auth-sources contains something useful, if it's bound.
8656 (and (boundp 'auth-sources)
8657 (tramp-get-connection-property v "first-password-request" nil)
8658 ;; Try with Tramp's current method.
0d5852cf
MA
8659 (tramp-compat-funcall
8660 'auth-source-user-or-password
8661 "password" tramp-current-host tramp-current-method))
7540f029
MA
8662 ;; Try the password cache.
8663 (when (functionp 'password-read)
8664 (unless (tramp-get-connection-property
8665 v "first-password-request" nil)
0d5852cf 8666 (tramp-compat-funcall 'password-cache-remove key))
7540f029 8667 (let ((password
0d5852cf
MA
8668 (tramp-compat-funcall 'password-read pw-prompt key)))
8669 (tramp-compat-funcall 'password-cache-add key password)
7540f029
MA
8670 password))
8671 ;; Else, get the password interactively.
8672 (read-passwd pw-prompt))
8673 (tramp-set-connection-property v "first-password-request" nil)))))
00d6fd04 8674
9c13938d
MA
8675(defun tramp-clear-passwd (vec)
8676 "Clear password cache for connection related to VEC."
0d5852cf
MA
8677 (tramp-compat-funcall
8678 'password-cache-remove
8679 (tramp-make-tramp-file-name
8680 (tramp-file-name-method vec)
8681 (tramp-file-name-user vec)
8682 (tramp-file-name-host vec)
8683 "")))
00d6fd04
MA
8684
8685;; Snarfed code from time-date.el and parse-time.el
8686
8687(defconst tramp-half-a-year '(241 17024)
8688"Evaluated by \"(days-to-time 183)\".")
8689
8690(defconst tramp-parse-time-months
8691 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
8692 ("apr" . 4) ("may" . 5) ("jun" . 6)
8693 ("jul" . 7) ("aug" . 8) ("sep" . 9)
8694 ("oct" . 10) ("nov" . 11) ("dec" . 12))
8695 "Alist mapping month names to integers.")
8696
8697(defun tramp-time-less-p (t1 t2)
8698 "Say whether time value T1 is less than time value T2."
8699 (unless t1 (setq t1 '(0 0)))
8700 (unless t2 (setq t2 '(0 0)))
8701 (or (< (car t1) (car t2))
8702 (and (= (car t1) (car t2))
8703 (< (nth 1 t1) (nth 1 t2)))))
8704
8705(defun tramp-time-subtract (t1 t2)
8706 "Subtract two time values.
8707Return the difference in the format of a time value."
8708 (unless t1 (setq t1 '(0 0)))
8709 (unless t2 (setq t2 '(0 0)))
8710 (let ((borrow (< (cadr t1) (cadr t2))))
8711 (list (- (car t1) (car t2) (if borrow 1 0))
8712 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
fb7933a3
KG
8713
8714(defun tramp-time-diff (t1 t2)
8715 "Return the difference between the two times, in seconds.
1a762140 8716T1 and T2 are time values (as returned by `current-time' for example)."
fb7933a3 8717 ;; Pacify byte-compiler with `symbol-function'.
ea9d1443
KG
8718 (cond ((and (fboundp 'subtract-time)
8719 (fboundp 'float-time))
0d5852cf
MA
8720 (tramp-compat-funcall
8721 'float-time (tramp-compat-funcall 'subtract-time t1 t2)))
ea9d1443
KG
8722 ((and (fboundp 'subtract-time)
8723 (fboundp 'time-to-seconds))
0d5852cf
MA
8724 (tramp-compat-funcall
8725 'time-to-seconds (tramp-compat-funcall 'subtract-time t1 t2)))
fb7933a3 8726 ((fboundp 'itimer-time-difference)
0d5852cf
MA
8727 (tramp-compat-funcall
8728 'itimer-time-difference
8729 (if (< (length t1) 3) (append t1 '(0)) t1)
8730 (if (< (length t2) 3) (append t2 '(0)) t2)))
fb7933a3 8731 (t
00d6fd04 8732 (let ((time (tramp-time-subtract t1 t2)))
ea9d1443
KG
8733 (+ (* (car time) 65536.0)
8734 (cadr time)
8735 (/ (or (nth 2 time) 0) 1000000.0))))))
fb7933a3
KG
8736
8737(defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
8738 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8739EOL-TYPE can be one of `dos', `unix', or `mac'."
8740 (cond ((fboundp 'coding-system-change-eol-conversion)
0d5852cf
MA
8741 (tramp-compat-funcall
8742 'coding-system-change-eol-conversion coding-system eol-type))
fb7933a3 8743 ((fboundp 'subsidiary-coding-system)
0d5852cf
MA
8744 (tramp-compat-funcall
8745 'subsidiary-coding-system coding-system
8746 (cond ((eq eol-type 'dos) 'crlf)
8747 ((eq eol-type 'unix) 'lf)
8748 ((eq eol-type 'mac) 'cr)
8749 (t
8750 (error "Unknown EOL-TYPE `%s', must be %s"
8751 eol-type
8752 "`dos', `unix', or `mac'")))))
fb7933a3
KG
8753 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8754
19a87064
MA
8755(defun tramp-set-process-query-on-exit-flag (process flag)
8756 "Specify if query is needed for process when Emacs is exited.
8757If the second argument flag is non-nil, Emacs will query the user before
8758exiting if process is running."
8759 (if (fboundp 'set-process-query-on-exit-flag)
0d5852cf
MA
8760 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
8761 (tramp-compat-funcall 'process-kill-without-query) process flag))
19a87064 8762
19a87064 8763
bf247b6e
KS
8764;; ------------------------------------------------------------
8765;; -- Kludges section --
8766;; ------------------------------------------------------------
fb7933a3
KG
8767
8768;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8769;; does not deal well with newline characters. Newline is replaced by
8770;; backslash newline. But if, say, the string `a backslash newline b'
8771;; is passed to a shell, the shell will expand this into "ab",
8772;; completely omitting the newline. This is not what was intended.
8773;; It does not appear to be possible to make the function
8774;; `shell-quote-argument' work with newlines without making it
8775;; dependent on the shell used. But within this package, we know that
8776;; we will always use a Bourne-like shell, so we use an approach which
8777;; groks newlines.
8778;;
8779;; The approach is simple: we call `shell-quote-argument', then
8780;; massage the newline part of the result.
8781;;
8782;; This function should produce a string which is grokked by a Unix
8783;; shell, even if the Emacs is running on Windows. Since this is the
8784;; kludges section, we bind `system-type' in such a way that
8785;; `shell-quote-arguments' behaves as if on Unix.
8786;;
8787;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8788;; function to work with Bourne-like shells.
8789;;
8790;; CCC: This function should be rewritten so that
8791;; `shell-quote-argument' is not used. This way, we are safe from
8792;; changes in `shell-quote-argument'.
8793(defun tramp-shell-quote-argument (s)
8794 "Similar to `shell-quote-argument', but groks newlines.
8795Only works for Bourne-like shells."
8796 (let ((system-type 'not-windows))
8797 (save-match-data
8798 (let ((result (shell-quote-argument s))
8799 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
8800 (when (and (>= (length result) 2)
8801 (string= (substring result 0 2) "\\~"))
8802 (setq result (substring result 1)))
8803 (while (string-match nl result)
8804 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
8805 t t result)))
8806 result))))
8807
a69c01a0
MA
8808;; Checklist for `tramp-unload-hook'
8809;; - Unload all `tramp-*' packages
8810;; - Reset `file-name-handler-alist'
8811;; - Cleanup hooks where Tramp functions are in
8812;; - Cleanup advised functions
8813;; - Cleanup autoloads
8814;;;###autoload
8815(defun tramp-unload-tramp ()
08b1eb21 8816 "Discard Tramp from loading remote files."
a69c01a0
MA
8817 (interactive)
8818 ;; When Tramp is not loaded yet, its autoloads are still active.
8c04e197 8819 (tramp-unload-file-name-handlers)
a69c01a0 8820 ;; ange-ftp settings must be enabled.
0d5852cf 8821 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
00d6fd04
MA
8822 ;; Maybe its not loaded yet.
8823 (condition-case nil
8824 (unload-feature 'tramp 'force)
a69c01a0
MA
8825 (error nil)))
8826
dea31ca6
MA
8827(when (and load-in-progress
8828 (string-match "Loading tramp..." (or (current-message) "")))
ccb4a481
MA
8829 (message "Loading tramp...done"))
8830
fb7933a3
KG
8831(provide 'tramp)
8832
fb7933a3
KG
8833;;; TODO:
8834
4007ba5b 8835;; * Handle nonlocal exits such as C-g.
00d6fd04
MA
8836;; * But it would probably be better to use with-local-quit at the
8837;; place where it's actually needed: around any potentially
8838;; indefinitely blocking piece of code. In this case it would be
8839;; within Tramp around one of its calls to accept-process-output (or
8840;; around one of the loops that calls accept-process-output)
d037d501 8841;; (Stefan Monnier).
fb7933a3 8842;; * Rewrite `tramp-shell-quote-argument' to abstain from using
b1d06e75 8843;; `shell-quote-argument'.
fb7933a3
KG
8844;; * In Emacs 21, `insert-directory' shows total number of bytes used
8845;; by the files in that directory. Add this here.
8846;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8847;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
fb7933a3 8848;; * Case-insensitive filename completion. (Norbert Goevert.)
fb7933a3
KG
8849;; * Don't use globbing for directories with many files, as this is
8850;; likely to produce long command lines, and some shells choke on
8851;; long command lines.
fb7933a3 8852;; * How to deal with MULE in `insert-file-contents' and `write-region'?
fb7933a3
KG
8853;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8854;; * abbreviate-file-name
8e754ea2 8855;; * Better error checking. At least whenever we see something
fb7933a3
KG
8856;; strange when doing zerop, we should kill the process and start
8857;; again. (Greg Stark)
fb7933a3 8858;; * Remove unneeded parameters from methods.
fb7933a3
KG
8859;; * Make it work for different encodings, and for different file name
8860;; encodings, too. (Daniel Pittman)
fb7933a3
KG
8861;; * Don't search for perl5 and perl. Instead, only search for perl and
8862;; then look if it's the right version (with `perl -v').
8863;; * When editing a remote CVS controlled file as a different user, VC
8864;; gets confused about the file locking status. Try to find out why
8865;; the workaround doesn't work.
3cdaec13 8866;; * Username and hostname completion.
6c4e47fa 8867;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8daea7fc 8868;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
16674e4f 8869;; Code is nearly identical.
cfb5c0db
MA
8870;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8871;; until the last but one hop via `start-file-process'. Apply it
8872;; also for ftp and smb.
00d6fd04
MA
8873;; * WIBNI if we had a command "trampclient"? If I was editing in
8874;; some shell with root priviledges, it would be nice if I could
8875;; just call
8876;; trampclient filename.c
8877;; as an editor, and the _current_ shell would connect to an Emacs
8878;; server and would be used in an existing non-priviledged Emacs
8879;; session for doing the editing in question.
8880;; That way, I need not tell Emacs my password again and be afraid
8881;; that it makes it into core dumps or other ugly stuff (I had Emacs
8882;; once display a just typed password in the context of a keyboard
8883;; sequence prompt for a question immediately following in a shell
8884;; script run within Emacs -- nasty).
8885;; And if I have some ssh session running to a different computer,
8886;; having the possibility of passing a local file there to a local
8887;; Emacs session (in case I can arrange for a connection back) would
8888;; be nice.
a4aeb9a4 8889;; Likely the corresponding Tramp server should not allow the
00d6fd04
MA
8890;; equivalent of the emacsclient -eval option in order to make this
8891;; reasonably unproblematic. And maybe trampclient should have some
8892;; way of passing credentials, like by using an SSL socket or
6e4f5731 8893;; something. (David Kastrup)
00d6fd04 8894;; * Reconnect directly to a compliant shell without first going
6e4f5731 8895;; through the user's default shell. (Pete Forman)
00d6fd04 8896;; * Make `tramp-default-user' obsolete.
11c71217 8897;; * How can I interrupt the remote process with a signal
6e4f5731 8898;; (interrupt-process seems not to work)? (Markus Triska)
2296b54d
MA
8899;; * Avoid the local shell entirely for starting remote processes. If
8900;; so, I think even a signal, when delivered directly to the local
8901;; SSH instance, would correctly be propagated to the remote process
8902;; automatically; possibly SSH would have to be started with
6e4f5731 8903;; "-t". (Markus Triska)
dea31ca6 8904;; * It makes me wonder if tramp couldn't fall back to ssh when scp
6e4f5731
MA
8905;; isn't on the remote host. (Mark A. Hershberger)
8906;; * Use lsh instead of ssh. (Alfred M. Szmidt)
3e2fa353
MA
8907;; * Implement a general server-local-variable mechanism, as there are
8908;; probably other variables that need different values for different
8909;; servers too. The user could then configure a variable (such as
8910;; tramp-server-local-variable-alist) to define any such variables
8911;; that they need to, which would then be let bound as appropriate
6e4f5731 8912;; in tramp functions. (Jason Rumney)
946a5aeb
MA
8913;; * Optimize out-of-band copying, when both methods are scp-like (not
8914;; rsync).
8915;; * Keep a second connection open for out-of-band methods like scp or
8916;; rsync.
7540f029 8917;; * Support ptys in `tramp-handle-start-file-process'. (Bug#4604)
7e5686f0
MA
8918;; * IMHO, it's a drawback that currently Tramp doesn't support
8919;; Unicode in Dired file names by default. Is it possible to
8920;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8921;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
6e4f5731 8922;; expects only English messages? (Juri Linkov)
7e5686f0
MA
8923;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8924;; * Do not handle files with drive letter as remote. (Bug#5447)
b533bc97 8925;; * Load Tramp subpackages only when needed. (Bug#1529, Bug#5448, Bug#5705)
7e5686f0
MA
8926;; * Try telnet+curl as new method. It might be useful for busybox,
8927;; without built-in uuencode/uudecode.
7540f029
MA
8928;; * Let `shell-dynamic-complete-*' and `comint-dynamic-complete' work
8929;; on remote hosts.
8930;; * Use secrets.el for password handling.
5f2b693f 8931;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
fb7933a3
KG
8932
8933;; Functions for file-name-handler-alist:
8934;; diff-latest-backup-file -- in diff.el
fb7933a3 8935
cdd44874 8936;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
fb7933a3 8937;;; tramp.el ends here
57671b72
MA
8938
8939;; Local Variables:
8940;; mode: Emacs-Lisp
8941;; coding: utf-8
8942;; End: