lwlib-Xaw.c (make_dialog): Remove extra arg to XtVaGetSubresources.
[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
MA
336 ("-e" "none")))
337 (tramp-remote-sh "/bin/sh")
338 (tramp-copy-program "scp")
263c02ef
MA
339 (tramp-copy-args (("-P" "%p") ("-p" "%k")
340 ("-q") ("-r")))
00d6fd04 341 (tramp-copy-keep-date t)
263c02ef 342 (tramp-copy-recursive t)
00d6fd04
MA
343 (tramp-password-end-of-line nil)
344 (tramp-gw-args (("-o"
345 "GlobalKnownHostsFile=/dev/null")
346 ("-o" "UserKnownHostsFile=/dev/null")
347 ("-o" "StrictHostKeyChecking=no")))
348 (tramp-default-port 22))
349 ("scp1" (tramp-login-program "ssh")
c04b03f8 350 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
351 ("-1" "-e" "none")))
352 (tramp-remote-sh "/bin/sh")
353 (tramp-copy-program "scp")
354 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
263c02ef 355 ("-q") ("-r")))
00d6fd04 356 (tramp-copy-keep-date t)
263c02ef 357 (tramp-copy-recursive t)
00d6fd04
MA
358 (tramp-password-end-of-line nil)
359 (tramp-gw-args (("-o"
360 "GlobalKnownHostsFile=/dev/null")
361 ("-o" "UserKnownHostsFile=/dev/null")
362 ("-o" "StrictHostKeyChecking=no")))
363 (tramp-default-port 22))
364 ("scp2" (tramp-login-program "ssh")
c04b03f8 365 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
366 ("-2" "-e" "none")))
367 (tramp-remote-sh "/bin/sh")
368 (tramp-copy-program "scp")
369 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
263c02ef 370 ("-q") ("-r")))
00d6fd04 371 (tramp-copy-keep-date t)
263c02ef 372 (tramp-copy-recursive t)
00d6fd04
MA
373 (tramp-password-end-of-line nil)
374 (tramp-gw-args (("-o"
375 "GlobalKnownHostsFile=/dev/null")
376 ("-o" "UserKnownHostsFile=/dev/null")
377 ("-o" "StrictHostKeyChecking=no")))
378 (tramp-default-port 22))
379 ("scp1_old"
380 (tramp-login-program "ssh1")
381 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
382 ("-e" "none")))
383 (tramp-remote-sh "/bin/sh")
384 (tramp-copy-program "scp1")
263c02ef 385 (tramp-copy-args (("-p" "%k") ("-r")))
00d6fd04 386 (tramp-copy-keep-date t)
263c02ef 387 (tramp-copy-recursive t)
00d6fd04
MA
388 (tramp-password-end-of-line nil))
389 ("scp2_old"
390 (tramp-login-program "ssh2")
391 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
392 ("-e" "none")))
393 (tramp-remote-sh "/bin/sh")
394 (tramp-copy-program "scp2")
263c02ef 395 (tramp-copy-args (("-p" "%k") ("-r")))
00d6fd04 396 (tramp-copy-keep-date t)
263c02ef 397 (tramp-copy-recursive t)
00d6fd04
MA
398 (tramp-password-end-of-line nil))
399 ("sftp" (tramp-login-program "ssh")
400 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
401 ("-e" "none")))
402 (tramp-remote-sh "/bin/sh")
403 (tramp-copy-program "sftp")
404 (tramp-copy-args nil)
405 (tramp-copy-keep-date nil)
406 (tramp-password-end-of-line nil))
407 ("rsync" (tramp-login-program "ssh")
408 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
409 ("-e" "none")))
410 (tramp-remote-sh "/bin/sh")
411 (tramp-copy-program "rsync")
263c02ef 412 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
00d6fd04 413 (tramp-copy-keep-date t)
b88f2d0a 414 (tramp-copy-keep-tmpfile t)
263c02ef 415 (tramp-copy-recursive t)
00d6fd04 416 (tramp-password-end-of-line nil))
263c02ef
MA
417 ("rsyncc"
418 (tramp-login-program "ssh")
946a5aeb
MA
419 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
420 ("-o" "ControlPath=%t.%%r@%%h:%%p")
421 ("-o" "ControlMaster=yes")
422 ("-e" "none")))
423 (tramp-remote-sh "/bin/sh")
424 (tramp-copy-program "rsync")
263c02ef 425 (tramp-copy-args (("-t" "%k") ("-r")))
946a5aeb
MA
426 (tramp-copy-env (("RSYNC_RSH")
427 (,(concat
428 "ssh"
429 " -o ControlPath=%t.%%r@%%h:%%p"
430 " -o ControlMaster=auto"))))
431 (tramp-copy-keep-date t)
b88f2d0a 432 (tramp-copy-keep-tmpfile t)
263c02ef 433 (tramp-copy-recursive t)
946a5aeb 434 (tramp-password-end-of-line nil))
00d6fd04
MA
435 ("remcp" (tramp-login-program "remsh")
436 (tramp-login-args (("%h") ("-l" "%u")))
437 (tramp-remote-sh "/bin/sh")
438 (tramp-copy-program "rcp")
439 (tramp-copy-args (("-p" "%k")))
440 (tramp-copy-keep-date t)
441 (tramp-password-end-of-line nil))
442 ("rsh" (tramp-login-program "rsh")
443 (tramp-login-args (("%h") ("-l" "%u")))
444 (tramp-remote-sh "/bin/sh")
445 (tramp-copy-program nil)
446 (tramp-copy-args nil)
447 (tramp-copy-keep-date nil)
448 (tramp-password-end-of-line nil))
449 ("ssh" (tramp-login-program "ssh")
c04b03f8 450 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
451 ("-e" "none")))
452 (tramp-remote-sh "/bin/sh")
453 (tramp-copy-program nil)
454 (tramp-copy-args nil)
455 (tramp-copy-keep-date nil)
456 (tramp-password-end-of-line nil)
457 (tramp-gw-args (("-o"
458 "GlobalKnownHostsFile=/dev/null")
459 ("-o" "UserKnownHostsFile=/dev/null")
460 ("-o" "StrictHostKeyChecking=no")))
461 (tramp-default-port 22))
462 ("ssh1" (tramp-login-program "ssh")
c04b03f8 463 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
464 ("-1" "-e" "none")))
465 (tramp-remote-sh "/bin/sh")
466 (tramp-copy-program nil)
467 (tramp-copy-args nil)
468 (tramp-copy-keep-date nil)
469 (tramp-password-end-of-line nil)
470 (tramp-gw-args (("-o"
471 "GlobalKnownHostsFile=/dev/null")
472 ("-o" "UserKnownHostsFile=/dev/null")
473 ("-o" "StrictHostKeyChecking=no")))
474 (tramp-default-port 22))
475 ("ssh2" (tramp-login-program "ssh")
c04b03f8 476 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
477 ("-2" "-e" "none")))
478 (tramp-remote-sh "/bin/sh")
479 (tramp-copy-program nil)
480 (tramp-copy-args nil)
481 (tramp-copy-keep-date nil)
482 (tramp-password-end-of-line nil)
483 (tramp-gw-args (("-o"
484 "GlobalKnownHostsFile=/dev/null")
485 ("-o" "UserKnownHostsFile=/dev/null")
486 ("-o" "StrictHostKeyChecking=no")))
487 (tramp-default-port 22))
488 ("ssh1_old"
489 (tramp-login-program "ssh1")
490 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
491 ("-e" "none")))
492 (tramp-remote-sh "/bin/sh")
493 (tramp-copy-program nil)
494 (tramp-copy-args nil)
495 (tramp-copy-keep-date nil)
496 (tramp-password-end-of-line nil))
497 ("ssh2_old"
498 (tramp-login-program "ssh2")
499 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
500 ("-e" "none")))
501 (tramp-remote-sh "/bin/sh")
502 (tramp-copy-program nil)
503 (tramp-copy-args nil)
504 (tramp-copy-keep-date nil)
505 (tramp-password-end-of-line nil))
506 ("remsh" (tramp-login-program "remsh")
507 (tramp-login-args (("%h") ("-l" "%u")))
508 (tramp-remote-sh "/bin/sh")
509 (tramp-copy-program nil)
510 (tramp-copy-args nil)
511 (tramp-copy-keep-date nil)
512 (tramp-password-end-of-line nil))
513 ("telnet"
514 (tramp-login-program "telnet")
515 (tramp-login-args (("%h") ("%p")))
516 (tramp-remote-sh "/bin/sh")
517 (tramp-copy-program nil)
518 (tramp-copy-args nil)
519 (tramp-copy-keep-date nil)
520 (tramp-password-end-of-line nil)
521 (tramp-default-port 23))
522 ("su" (tramp-login-program "su")
523 (tramp-login-args (("-") ("%u")))
524 (tramp-remote-sh "/bin/sh")
525 (tramp-copy-program nil)
526 (tramp-copy-args nil)
527 (tramp-copy-keep-date nil)
528 (tramp-password-end-of-line nil))
529 ("sudo" (tramp-login-program "sudo")
530 (tramp-login-args (("-u" "%u")
42bc9b6d 531 ("-s") ("-H") ("-p" "Password:")))
00d6fd04
MA
532 (tramp-remote-sh "/bin/sh")
533 (tramp-copy-program nil)
534 (tramp-copy-args nil)
535 (tramp-copy-keep-date nil)
536 (tramp-password-end-of-line nil))
537 ("scpc" (tramp-login-program "ssh")
c04b03f8 538 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
539 ("-o" "ControlPath=%t.%%r@%%h:%%p")
540 ("-o" "ControlMaster=yes")
541 ("-e" "none")))
542 (tramp-remote-sh "/bin/sh")
543 (tramp-copy-program "scp")
544 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
545 ("-o" "ControlPath=%t.%%r@%%h:%%p")
546 ("-o" "ControlMaster=auto")))
547 (tramp-copy-keep-date t)
548 (tramp-password-end-of-line nil)
549 (tramp-gw-args (("-o"
550 "GlobalKnownHostsFile=/dev/null")
551 ("-o" "UserKnownHostsFile=/dev/null")
552 ("-o" "StrictHostKeyChecking=no")))
553 (tramp-default-port 22))
554 ("scpx" (tramp-login-program "ssh")
c04b03f8 555 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
556 ("-e" "none" "-t" "-t" "/bin/sh")))
557 (tramp-remote-sh "/bin/sh")
558 (tramp-copy-program "scp")
559 (tramp-copy-args (("-p" "%k")))
560 (tramp-copy-keep-date t)
561 (tramp-password-end-of-line nil)
562 (tramp-gw-args (("-o"
563 "GlobalKnownHostsFile=/dev/null")
564 ("-o" "UserKnownHostsFile=/dev/null")
565 ("-o" "StrictHostKeyChecking=no")))
566 (tramp-default-port 22))
567 ("sshx" (tramp-login-program "ssh")
c04b03f8 568 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
00d6fd04
MA
569 ("-e" "none" "-t" "-t" "/bin/sh")))
570 (tramp-remote-sh "/bin/sh")
571 (tramp-copy-program nil)
572 (tramp-copy-args nil)
573 (tramp-copy-keep-date nil)
574 (tramp-password-end-of-line nil)
575 (tramp-gw-args (("-o"
576 "GlobalKnownHostsFile=/dev/null")
577 ("-o" "UserKnownHostsFile=/dev/null")
578 ("-o" "StrictHostKeyChecking=no")))
579 (tramp-default-port 22))
580 ("krlogin"
581 (tramp-login-program "krlogin")
582 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
583 (tramp-remote-sh "/bin/sh")
584 (tramp-copy-program nil)
585 (tramp-copy-args nil)
586 (tramp-copy-keep-date nil)
587 (tramp-password-end-of-line nil))
588 ("plink" (tramp-login-program "plink")
589 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
590 ("-ssh")))
591 (tramp-remote-sh "/bin/sh")
592 (tramp-copy-program nil)
593 (tramp-copy-args nil)
594 (tramp-copy-keep-date nil)
595 (tramp-password-end-of-line "xy") ;see docstring for "xy"
596 (tramp-default-port 22))
597 ("plink1"
598 (tramp-login-program "plink")
599 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
600 ("-1" "-ssh")))
601 (tramp-remote-sh "/bin/sh")
602 (tramp-copy-program nil)
603 (tramp-copy-args nil)
604 (tramp-copy-keep-date nil)
605 (tramp-password-end-of-line "xy") ;see docstring for "xy"
606 (tramp-default-port 22))
607 ("plinkx"
608 (tramp-login-program "plink")
42bc9b6d
MA
609 ;; ("%h") must be a single element, see
610 ;; `tramp-compute-multi-hops'.
611 (tramp-login-args (("-load") ("%h") ("-t")
ce3f516f 612 (,(format
dab816a9
MA
613 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
614 tramp-terminal-type
615 tramp-initial-end-of-output))
00d6fd04
MA
616 ("/bin/sh")))
617 (tramp-remote-sh "/bin/sh")
618 (tramp-copy-program nil)
619 (tramp-copy-args nil)
620 (tramp-copy-keep-date nil)
621 (tramp-password-end-of-line nil))
622 ("pscp" (tramp-login-program "plink")
623 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
624 ("-ssh")))
625 (tramp-remote-sh "/bin/sh")
626 (tramp-copy-program "pscp")
60f2c210 627 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
00d6fd04
MA
628 (tramp-copy-keep-date t)
629 (tramp-password-end-of-line "xy") ;see docstring for "xy"
630 (tramp-default-port 22))
631 ("psftp" (tramp-login-program "plink")
632 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
633 ("-ssh")))
634 (tramp-remote-sh "/bin/sh")
635 (tramp-copy-program "pscp")
60f2c210 636 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
00d6fd04
MA
637 (tramp-copy-keep-date t)
638 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
639 ("fcp" (tramp-login-program "fsh")
640 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
641 (tramp-remote-sh "/bin/sh -i")
642 (tramp-copy-program "fcp")
643 (tramp-copy-args (("-p" "%k")))
644 (tramp-copy-keep-date t)
645 (tramp-password-end-of-line nil)))
fb7933a3
KG
646 "*Alist of methods for remote files.
647This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
648Each NAME stands for a remote access method. Each PARAM is a
649pair of the form (KEY VALUE). The following KEYs are defined:
fb7933a3
KG
650 * `tramp-remote-sh'
651 This specifies the Bourne shell to use on the remote host. This
652 MUST be a Bourne-like shell. It is normally not necessary to set
a4aeb9a4 653 this to any value other than \"/bin/sh\": Tramp wants to use a shell
fb7933a3
KG
654 which groks tilde expansion, but it can search for it. Also note
655 that \"/bin/sh\" exists on all Unixen, this might not be true for
656 the value that you decide to use. You Have Been Warned.
b25a52cc
KG
657 * `tramp-login-program'
658 This specifies the name of the program to use for logging in to the
00d6fd04
MA
659 remote host. This may be the name of rsh or a workalike program,
660 or the name of telnet or a workalike, or the name of su or a workalike.
b25a52cc 661 * `tramp-login-args'
fb7933a3 662 This specifies the list of arguments to pass to the above
00d6fd04 663 mentioned program. Please note that this is a list of list of arguments,
fb7933a3 664 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
00d6fd04
MA
665 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
666 There are some patterns: \"%h\" in this list is replaced by the host
667 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
668 port number, and \"%%\" can be used to obtain a literal percent character.
669 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
670 expansion (i.e. no host or no user specified), this list is not used as
671 argument. By this, arguments like (\"-l\" \"%u\") are optional.
672 \"%t\" is replaced by the temporary file name produced with
673 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
674 parameter of a program, if exists.
b25a52cc
KG
675 * `tramp-copy-program'
676 This specifies the name of the program to use for remotely copying
677 the file; this might be the absolute filename of rcp or the name of
678 a workalike program.
679 * `tramp-copy-args'
fb7933a3 680 This specifies the list of parameters to pass to the above mentioned
b25a52cc 681 program, the hints for `tramp-login-args' also apply here.
00d6fd04
MA
682 * `tramp-copy-keep-date'
683 This specifies whether the copying program when the preserves the
684 timestamp of the original file.
b88f2d0a
MA
685 * `tramp-copy-keep-tmpfile'
686 This specifies whether a temporary local file shall be kept
687 for optimization reasons (useful for \"rsync\" methods).
688 * `tramp-copy-recursive'
689 Whether the operation copies directories recursively.
00d6fd04
MA
690 * `tramp-default-port'
691 The default port of a method is needed in case of gateway connections.
692 Additionally, it is used as indication which method is prepared for
693 passing gateways.
694 * `tramp-gw-args'
695 As the attribute name says, additional arguments are specified here
696 when a method is applied via a gateway.
90f8dc03
KG
697 * `tramp-password-end-of-line'
698 This specifies the string to use for terminating the line after
699 submitting the password. If this method parameter is nil, then the
700 value of the normal variable `tramp-default-password-end-of-line'
701 is used. This parameter is necessary because the \"plink\" program
702 requires any two characters after sending the password. These do
703 not have to be newline or carriage return characters. Other login
704 programs are happy with just one character, the newline character.
705 We use \"xy\" as the value for methods using \"plink\".
b25a52cc
KG
706
707What does all this mean? Well, you should specify `tramp-login-program'
708for all methods; this program is used to log in to the remote site. Then,
709there are two ways to actually transfer the files between the local and the
710remote side. One way is using an additional rcp-like program. If you want
711to do this, set `tramp-copy-program' in the method.
fb7933a3
KG
712
713Another possibility for file transfer is inline transfer, i.e. the
b25a52cc 714file is passed through the same buffer used by `tramp-login-program'. In
fb7933a3 715this case, the file contents need to be protected since the
b25a52cc 716`tramp-login-program' might use escape codes or the connection might not
fb7933a3 717be eight-bit clean. Therefore, file contents are encoded for transit.
00d6fd04
MA
718See the variables `tramp-local-coding-commands' and
719`tramp-remote-coding-commands' for details.
fb7933a3 720
16674e4f 721So, to summarize: if the method is an out-of-band method, then you
b25a52cc 722must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
00d6fd04
MA
723inline method, then these two parameters should be nil. Methods which
724are fit for gateways must have `tramp-default-port' at least.
fb7933a3
KG
725
726Notes:
727
00d6fd04
MA
728When using `su' or `sudo' the phrase `open connection to a remote
729host' sounds strange, but it is used nevertheless, for consistency.
730No connection is opened to a remote host, but `su' or `sudo' is
731started on the local host. You should specify a remote host
732`localhost' or the name of the local host. Another host name is
733useful only in combination with `tramp-default-proxies-alist'.")
fb7933a3 734
a92375d9
MA
735(defun tramp-detect-ssh-controlmaster ()
736 "Call ssh to detect whether it supports the ControlMaster argument.
737This function may return nil when the argument is supported, but
738shouldn't return t when it isn't."
739 (ignore-errors
740 (with-temp-buffer
741 (call-process "ssh" nil t nil "-o" "ControlMaster")
742 (goto-char (point-min))
743 (search-forward-regexp "Missing ControlMaster argument" nil t))))
744
b25a52cc 745(defcustom tramp-default-method
83e20b5c
MA
746 ;; An external copy method seems to be preferred, because it is much
747 ;; more performant for large files, and it hasn't too serious delays
748 ;; for small files. But it must be ensured that there aren't
749 ;; permanent password queries. Either a password agent like
263c02ef
MA
750 ;; "ssh-agent" or "Pageant" shall run, or the optional
751 ;; password-cache.el or auth-sources.el packages shall be active for
a92375d9
MA
752 ;; password caching. "scpc" is chosen if we detect that the user is
753 ;; running OpenSSH 4.0 or newer.
00d6fd04
MA
754 (cond
755 ;; PuTTY is installed.
756 ((executable-find "pscp")
757 (if (or (fboundp 'password-read)
263c02ef 758 (fboundp 'auth-source-user-or-password)
00d6fd04 759 ;; Pageant is running.
70c11b0b 760 (tramp-compat-process-running-p "Pageant"))
00d6fd04
MA
761 "pscp"
762 "plink"))
763 ;; There is an ssh installation.
764 ((executable-find "scp")
a92375d9
MA
765 (cond
766 ((tramp-detect-ssh-controlmaster) "scpc")
767 ((or (fboundp 'password-read)
768 (fboundp 'auth-source-user-or-password)
769 ;; ssh-agent is running.
770 (getenv "SSH_AUTH_SOCK")
771 (getenv "SSH_AGENT_PID"))
772 "scp")
773 (t "ssh")))
00d6fd04
MA
774 ;; Fallback.
775 (t "ftp"))
fb7933a3 776 "*Default method to use for transferring files.
c62c9d08 777See `tramp-methods' for possibilities.
4007ba5b 778Also see `tramp-default-method-alist'."
c62c9d08
KG
779 :group 'tramp
780 :type 'string)
781
505edaeb 782(defcustom tramp-default-method-alist
4007ba5b 783 '(("\\`localhost\\'" "\\`root\\'" "su"))
00d6fd04 784 "*Default method to use for specific host/user pairs.
c62c9d08
KG
785This is an alist of items (HOST USER METHOD). The first matching item
786specifies the method to use for a file name which does not specify a
787method. HOST and USER are regular expressions or nil, which is
788interpreted as a regular expression which always matches. If no entry
789matches, the variable `tramp-default-method' takes effect.
790
791If the file name does not specify the user, lookup is done using the
792empty string for the user name.
793
794See `tramp-methods' for a list of possibilities for METHOD."
795 :group 'tramp
796 :type '(repeat (list (regexp :tag "Host regexp")
797 (regexp :tag "User regexp")
798 (string :tag "Method"))))
799
00d6fd04
MA
800(defcustom tramp-default-user
801 nil
802 "*Default user to use for transferring files.
803It is nil by default; otherwise settings in configuration files like
804\"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
805
806This variable is regarded as obsolete, and will be removed soon."
807 :group 'tramp
808 :type '(choice (const nil) string))
809
810(defcustom tramp-default-user-alist
811 `(("\\`su\\(do\\)?\\'" nil "root")
812 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
813 nil ,(user-login-name)))
814 "*Default user to use for specific method/host pairs.
815This is an alist of items (METHOD HOST USER). The first matching item
816specifies the user to use for a file name which does not specify a
817user. METHOD and USER are regular expressions or nil, which is
818interpreted as a regular expression which always matches. If no entry
819matches, the variable `tramp-default-user' takes effect.
820
821If the file name does not specify the method, lookup is done using the
822empty string for the method name."
823 :group 'tramp
824 :type '(repeat (list (regexp :tag "Method regexp")
825 (regexp :tag "Host regexp")
826 (string :tag "User"))))
827
828(defcustom tramp-default-host
829 (system-name)
830 "*Default host to use for transferring files.
831Useful for su and sudo methods mostly."
832 :group 'tramp
833 :type 'string)
834
835(defcustom tramp-default-proxies-alist nil
836 "*Route to be followed for specific host/user pairs.
837This is an alist of items (HOST USER PROXY). The first matching
838item specifies the proxy to be passed for a file name located on
839a remote target matching USER@HOST. HOST and USER are regular
70c11b0b
MA
840expressions. PROXY must be a Tramp filename without a localname
841part. Method and user name on PROXY are optional, which is
842interpreted with the default values. PROXY can contain the
843patterns %h and %u, which are replaced by the strings matching
844HOST or USER, respectively.
845
846HOST, USER or PROXY could also be Lisp forms, which will be
847evaluated. The result must be a string or nil, which is
848interpreted as a regular expression which always matches."
00d6fd04 849 :group 'tramp
70c11b0b
MA
850 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
851 (choice :tag "User regexp" regexp sexp)
852 (choice :tag "Proxy remote name" string (const nil)))))
00d6fd04 853
b96e6899
MA
854(defconst tramp-local-host-regexp
855 (concat
856 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$")
857 "*Host names which are regarded as local host.")
858
16674e4f 859(defconst tramp-completion-function-alist-rsh
00d6fd04
MA
860 '((tramp-parse-rhosts "/etc/hosts.equiv")
861 (tramp-parse-rhosts "~/.rhosts"))
b25a52cc 862 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
16674e4f 863
16674e4f 864(defconst tramp-completion-function-alist-ssh
00d6fd04
MA
865 '((tramp-parse-rhosts "/etc/hosts.equiv")
866 (tramp-parse-rhosts "/etc/shosts.equiv")
867 (tramp-parse-shosts "/etc/ssh_known_hosts")
868 (tramp-parse-sconfig "/etc/ssh_config")
869 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
870 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
871 (tramp-parse-rhosts "~/.rhosts")
872 (tramp-parse-rhosts "~/.shosts")
873 (tramp-parse-shosts "~/.ssh/known_hosts")
874 (tramp-parse-sconfig "~/.ssh/config")
875 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
876 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
b25a52cc 877 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
16674e4f 878
16674e4f 879(defconst tramp-completion-function-alist-telnet
00d6fd04 880 '((tramp-parse-hosts "/etc/hosts"))
b25a52cc 881 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
16674e4f 882
16674e4f 883(defconst tramp-completion-function-alist-su
00d6fd04 884 '((tramp-parse-passwd "/etc/passwd"))
b25a52cc 885 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
292ffc15 886
00d6fd04
MA
887(defconst tramp-completion-function-alist-putty
888 '((tramp-parse-putty
889 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
890 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
891
5ec2cc41 892(defvar tramp-completion-function-alist nil
16674e4f 893 "*Alist of methods for remote files.
b533bc97 894This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
16674e4f 895Each NAME stands for a remote access method. Each PAIR is of the form
b533bc97 896\(FUNCTION FILE\). FUNCTION is responsible to extract user names and host
16674e4f
KG
897names from FILE for completion. The following predefined FUNCTIONs exists:
898
5ec2cc41
KG
899 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
900 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
901 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
902 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
903 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
904 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
905 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
906 * `tramp-parse-netrc' for \"~/.netrc\" like files.
00d6fd04 907 * `tramp-parse-putty' for PuTTY registry keys.
5ec2cc41
KG
908
909FUNCTION can also be a customer defined function. For more details see
910the info pages.")
911
912(eval-after-load "tramp"
913 '(progn
914 (tramp-set-completion-function
915 "rcp" tramp-completion-function-alist-rsh)
916 (tramp-set-completion-function
917 "scp" tramp-completion-function-alist-ssh)
918 (tramp-set-completion-function
919 "scp1" tramp-completion-function-alist-ssh)
920 (tramp-set-completion-function
921 "scp2" tramp-completion-function-alist-ssh)
922 (tramp-set-completion-function
923 "scp1_old" tramp-completion-function-alist-ssh)
924 (tramp-set-completion-function
925 "scp2_old" tramp-completion-function-alist-ssh)
926 (tramp-set-completion-function
70c11b0b 927 "rsync" tramp-completion-function-alist-ssh)
946a5aeb
MA
928 (tramp-set-completion-function
929 "rsyncc" tramp-completion-function-alist-ssh)
5ec2cc41
KG
930 (tramp-set-completion-function
931 "remcp" tramp-completion-function-alist-rsh)
932 (tramp-set-completion-function
933 "rsh" tramp-completion-function-alist-rsh)
934 (tramp-set-completion-function
935 "ssh" tramp-completion-function-alist-ssh)
936 (tramp-set-completion-function
937 "ssh1" tramp-completion-function-alist-ssh)
938 (tramp-set-completion-function
939 "ssh2" tramp-completion-function-alist-ssh)
940 (tramp-set-completion-function
941 "ssh1_old" tramp-completion-function-alist-ssh)
942 (tramp-set-completion-function
943 "ssh2_old" tramp-completion-function-alist-ssh)
944 (tramp-set-completion-function
945 "remsh" tramp-completion-function-alist-rsh)
946 (tramp-set-completion-function
947 "telnet" tramp-completion-function-alist-telnet)
948 (tramp-set-completion-function
949 "su" tramp-completion-function-alist-su)
950 (tramp-set-completion-function
951 "sudo" tramp-completion-function-alist-su)
bf247b6e 952 (tramp-set-completion-function
5ec2cc41
KG
953 "scpx" tramp-completion-function-alist-ssh)
954 (tramp-set-completion-function
955 "sshx" tramp-completion-function-alist-ssh)
956 (tramp-set-completion-function
957 "krlogin" tramp-completion-function-alist-rsh)
958 (tramp-set-completion-function
959 "plink" tramp-completion-function-alist-ssh)
960 (tramp-set-completion-function
961 "plink1" tramp-completion-function-alist-ssh)
00d6fd04
MA
962 (tramp-set-completion-function
963 "plinkx" tramp-completion-function-alist-putty)
5ec2cc41
KG
964 (tramp-set-completion-function
965 "pscp" tramp-completion-function-alist-ssh)
966 (tramp-set-completion-function
967 "fcp" tramp-completion-function-alist-ssh)))
16674e4f 968
674da028
MA
969(defconst tramp-echo-mark-marker "_echo"
970 "String marker to surround echoed commands.")
971
68712eb6
MA
972(defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
973 "String length of `tramp-echo-mark-marker'.")
974
975(defconst tramp-echo-mark
976 (concat tramp-echo-mark-marker
977 (make-string tramp-echo-mark-marker-length ?\b))
00d6fd04
MA
978 "String mark to be transmitted around shell commands.
979Used to separate their echo from the output they produce. This
980will only be used if we cannot disable remote echo via stty.
981This string must have no effect on the remote shell except for
982producing some echo which can later be detected by
674da028
MA
983`tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
984followed by an equal number of backspaces to erase them will
985usually suffice.")
00d6fd04 986
68712eb6
MA
987(defconst tramp-echoed-echo-mark-regexp
988 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
989 tramp-echo-mark-marker tramp-echo-mark-marker-length)
00d6fd04
MA
990 "Regexp which matches `tramp-echo-mark' as it gets echoed by
991the remote shell.")
992
fb7933a3
KG
993(defcustom tramp-rsh-end-of-line "\n"
994 "*String used for end of line in rsh connections.
995I don't think this ever needs to be changed, so please tell me about it
16674e4f 996if you need to change this.
90f8dc03
KG
997Also see the method parameter `tramp-password-end-of-line' and the normal
998variable `tramp-default-password-end-of-line'."
16674e4f
KG
999 :group 'tramp
1000 :type 'string)
1001
90f8dc03
KG
1002(defcustom tramp-default-password-end-of-line
1003 tramp-rsh-end-of-line
16674e4f 1004 "*String used for end of line after sending a password.
90f8dc03
KG
1005This variable provides the default value for the method parameter
1006`tramp-password-end-of-line', see `tramp-methods' for more details.
1007
16674e4f
KG
1008It seems that people using plink under Windows need to send
1009\"\\r\\n\" (carriage-return, then newline) after a password, but just
1010\"\\n\" after all other lines. This variable can be used for the
1011password, see `tramp-rsh-end-of-line' for the other cases.
1012
1013The default value is to use the same value as `tramp-rsh-end-of-line'."
fb7933a3
KG
1014 :group 'tramp
1015 :type 'string)
1016
00d6fd04
MA
1017;; "getconf PATH" yields:
1018;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
1019;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
0664ff72 1020;; GNU/Linux (Debian, Suse): /bin:/usr/bin
00d6fd04 1021;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
fb7933a3 1022(defcustom tramp-remote-path
00d6fd04
MA
1023 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
1024 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
fb7933a3
KG
1025 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
1026 "*List of directories to search for executables on remote host.
00d6fd04
MA
1027For every remote host, this variable will be set buffer local,
1028keeping the list of existing directories on that host.
fb7933a3
KG
1029
1030You can use `~' in this list, but when searching for a shell which groks
00d6fd04
MA
1031tilde expansion, all directory names starting with `~' will be ignored.
1032
1033`Default Directories' represent the list of directories given by
1034the command \"getconf PATH\". It is recommended to use this
1035entry on top of this list, because these are the default
70c11b0b
MA
1036directories for POSIX compatible commands.
1037
1038`Private Directories' are the settings of the $PATH environment,
1039as given in your `~/.profile'."
00d6fd04
MA
1040 :group 'tramp
1041 :type '(repeat (choice
1042 (const :tag "Default Directories" tramp-default-remote-path)
70c11b0b 1043 (const :tag "Private Directories" tramp-own-remote-path)
00d6fd04
MA
1044 (string :tag "Directory"))))
1045
00d6fd04 1046(defcustom tramp-remote-process-environment
a0a5183a 1047 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
b533bc97 1048 ,(format "TERM=%s" tramp-terminal-type)
97c696d5
MA
1049 "EMACS=t" ;; Deprecated.
1050 ,(format "INSIDE_EMACS=%s,tramp:%s" emacs-version tramp-version)
00d6fd04
MA
1051 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
1052 "autocorrect=" "correct=")
1053
1054 "*List of environment variables to be set on the remote host.
1055
1056Each element should be a string of the form ENVVARNAME=VALUE. An
1057entry ENVVARNAME= diables the corresponding environment variable,
1058which might have been set in the init files like ~/.profile.
1059
1060Special handling is applied to the PATH environment, which should
1061not be set here. Instead of, it should be set via `tramp-remote-path'."
fb7933a3
KG
1062 :group 'tramp
1063 :type '(repeat string))
1064
1065(defcustom tramp-login-prompt-regexp
bc103d00 1066 ".*ogin\\( .*\\)?: *"
fb7933a3 1067 "*Regexp matching login-like prompts.
bc103d00
MA
1068The regexp should match at end of buffer.
1069
1070Sometimes the prompt is reported to look like \"login as:\"."
fb7933a3
KG
1071 :group 'tramp
1072 :type 'regexp)
1073
821e6e36 1074(defcustom tramp-shell-prompt-pattern
aa485f7c
MA
1075 ;; Allow a prompt to start right after a ^M since it indeed would be
1076 ;; displayed at the beginning of the line (and Zsh uses it).
dab816a9 1077 "\\(?:^\\|\r\\)[^#$%>\n]*#?[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
821e6e36
KG
1078 "Regexp to match prompts from remote shell.
1079Normally, Tramp expects you to configure `shell-prompt-pattern'
1080correctly, but sometimes it happens that you are connecting to a
1081remote host which sends a different kind of shell prompt. Therefore,
1082Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
1083and also things matched by this variable. The default value of this
b25a52cc 1084variable is similar to the default value of `shell-prompt-pattern',
dab816a9
MA
1085which should work well in many cases.
1086
1087This regexp must match both `tramp-initial-end-of-output' and
1088`tramp-end-of-output'."
821e6e36
KG
1089 :group 'tramp
1090 :type 'regexp)
1091
fb7933a3 1092(defcustom tramp-password-prompt-regexp
00d6fd04 1093 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
fb7933a3 1094 "*Regexp matching password-like prompts.
ac474af1 1095The regexp should match at end of buffer.
fb7933a3
KG
1096
1097The `sudo' program appears to insert a `^@' character into the prompt."
1098 :group 'tramp
1099 :type 'regexp)
1100
1101(defcustom tramp-wrong-passwd-regexp
b1d06e75
KG
1102 (concat "^.*"
1103 ;; These strings should be on the last line
a4aeb9a4 1104 (regexp-opt '("Permission denied"
b1d06e75
KG
1105 "Login incorrect"
1106 "Login Incorrect"
1107 "Connection refused"
27e813fe 1108 "Connection closed"
7e5686f0 1109 "Timeout, server not responding."
b1d06e75
KG
1110 "Sorry, try again."
1111 "Name or service not known"
00d6fd04 1112 "Host key verification failed."
70c11b0b 1113 "No supported authentication methods left to try!") t)
b1d06e75
KG
1114 ".*"
1115 "\\|"
1116 "^.*\\("
1117 ;; Here comes a list of regexes, separated by \\|
1118 "Received signal [0-9]+"
1119 "\\).*")
fb7933a3 1120 "*Regexp matching a `login failed' message.
ac474af1
KG
1121The regexp should match at end of buffer."
1122 :group 'tramp
1123 :type 'regexp)
1124
1125(defcustom tramp-yesno-prompt-regexp
3cdaec13
KG
1126 (concat
1127 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
1128 "\\s-*")
1129 "Regular expression matching all yes/no queries which need to be confirmed.
ac474af1 1130The confirmation should be done with yes or no.
3cdaec13
KG
1131The regexp should match at end of buffer.
1132See also `tramp-yn-prompt-regexp'."
fb7933a3
KG
1133 :group 'tramp
1134 :type 'regexp)
1135
3cdaec13 1136(defcustom tramp-yn-prompt-regexp
658052a2
MA
1137 (concat
1138 (regexp-opt '("Store key in cache? (y/n)"
1139 "Update cached key? (y/n, Return cancels connection)") t)
1140 "\\s-*")
3cdaec13
KG
1141 "Regular expression matching all y/n queries which need to be confirmed.
1142The confirmation should be done with y or n.
1143The regexp should match at end of buffer.
1144See also `tramp-yesno-prompt-regexp'."
1145 :group 'tramp
1146 :type 'regexp)
487f4fb7
KG
1147
1148(defcustom tramp-terminal-prompt-regexp
1149 (concat "\\("
1150 "TERM = (.*)"
1151 "\\|"
1152 "Terminal type\\? \\[.*\\]"
1153 "\\)\\s-*")
1154 "Regular expression matching all terminal setting prompts.
1155The regexp should match at end of buffer.
1156The answer will be provided by `tramp-action-terminal', which see."
1157 :group 'tramp
1158 :type 'regexp)
3cdaec13 1159
01917a18
MA
1160(defcustom tramp-operation-not-permitted-regexp
1161 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1162 (regexp-opt '("Operation not permitted") t))
1163 "Regular expression matching keep-date problems in (s)cp operations.
1164Copying has been performed successfully already, so this message can
1165be ignored safely."
1166 :group 'tramp
1167 :type 'regexp)
1168
6b2633cc
LH
1169(defcustom tramp-copy-failed-regexp
1170 (concat "\\(.+: "
1171 (regexp-opt '("Permission denied"
1172 "not a regular file"
1173 "is a directory"
1174 "No such file or directory") t)
1175 "\\)\\s-*")
1176 "Regular expression matching copy problems in (s)cp operations."
1177 :group 'tramp
1178 :type 'regexp)
1179
19a87064 1180(defcustom tramp-process-alive-regexp
38c65fca 1181 ""
19a87064 1182 "Regular expression indicating a process has finished.
38c65fca
KG
1183In fact this expression is empty by intention, it will be used only to
1184check regularly the status of the associated process.
07dfe738 1185The answer will be provided by `tramp-action-process-alive',
00d6fd04 1186`tramp-action-out-of-band', which see."
38c65fca
KG
1187 :group 'tramp
1188 :type 'regexp)
1189
fb7933a3
KG
1190(defcustom tramp-temp-name-prefix "tramp."
1191 "*Prefix to use for temporary files.
1192If this is a relative file name (such as \"tramp.\"), it is considered
1193relative to the directory name returned by the function
9e6ab520 1194`tramp-compat-temporary-file-directory' (which see). It may also be an
fb7933a3
KG
1195absolute file name; don't forget to include a prefix for the filename
1196part, though."
1197 :group 'tramp
1198 :type 'string)
1199
2296b54d
MA
1200(defconst tramp-temp-buffer-name " *tramp temp*"
1201 "Buffer name for a temporary buffer.
1202It shall be used in combination with `generate-new-buffer-name'.")
1203
b88f2d0a
MA
1204(defvar tramp-temp-buffer-file-name nil
1205 "File name of a persistent local temporary file.
1206Useful for \"rsync\" like methods.")
1207(make-variable-buffer-local 'tramp-temp-buffer-file-name)
1208
4007ba5b 1209(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
c62c9d08
KG
1210 "*Alist specifying extra arguments to pass to the remote shell.
1211Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1212matching the shell file name and ARGS is a string specifying the
1213arguments.
1214
1215This variable is only used when Tramp needs to start up another shell
1216for tilde expansion. The extra arguments should typically prevent the
1217shell from reading its init file."
1218 :group 'tramp
90f8dc03
KG
1219 ;; This might be the wrong way to test whether the widget type
1220 ;; `alist' is available. Who knows the right way to test it?
1221 :type (if (get 'alist 'widget-type)
1222 '(alist :key-type string :value-type string)
1223 '(repeat (cons string string))))
c62c9d08 1224
00d6fd04
MA
1225;; XEmacs is distributed with few Lisp packages. Further packages are
1226;; installed using EFS. If we use a unified filename format, then
1227;; Tramp is required in addition to EFS. (But why can't Tramp just
1228;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1229;; just like before.) Another reason for using a separate filename
1230;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1231;; Tramp only knows how to deal with `file-name-handler-alist', not
1232;; the other places.
1233
1234;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1235;;;###autoload
1236(defcustom tramp-syntax
1237 (if (featurep 'xemacs) 'sep 'ftp)
1238 "Tramp filename syntax to be used.
1239
1240It can have the following values:
1241
1242 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1243 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1244 'url -- URL-like syntax."
16674e4f 1245 :group 'tramp
00d6fd04
MA
1246 :type (if (featurep 'xemacs)
1247 '(choice (const :tag "EFS" ftp)
1248 (const :tag "XEmacs" sep)
1249 (const :tag "URL" url))
1250 '(choice (const :tag "Ange-FTP" ftp)
1251 (const :tag "URL" url))))
1252
1253(defconst tramp-prefix-format
1254 (cond ((equal tramp-syntax 'ftp) "/")
1255 ((equal tramp-syntax 'sep) "/[")
1256 ((equal tramp-syntax 'url) "/")
1257 (t (error "Wrong `tramp-syntax' defined")))
a4aeb9a4 1258 "*String matching the very beginning of Tramp file names.
00d6fd04 1259Used in `tramp-make-tramp-file-name'.")
16674e4f 1260
00d6fd04 1261(defconst tramp-prefix-regexp
16674e4f 1262 (concat "^" (regexp-quote tramp-prefix-format))
a4aeb9a4 1263 "*Regexp matching the very beginning of Tramp file names.
00d6fd04 1264Should always start with \"^\". Derived from `tramp-prefix-format'.")
16674e4f 1265
00d6fd04 1266(defconst tramp-method-regexp
16674e4f 1267 "[a-zA-Z_0-9-]+"
00d6fd04 1268 "*Regexp matching methods identifiers.")
16674e4f 1269
00d6fd04
MA
1270(defconst tramp-postfix-method-format
1271 (cond ((equal tramp-syntax 'ftp) ":")
1272 ((equal tramp-syntax 'sep) "/")
1273 ((equal tramp-syntax 'url) "://")
1274 (t (error "Wrong `tramp-syntax' defined")))
16674e4f 1275 "*String matching delimeter between method and user or host names.
00d6fd04 1276Used in `tramp-make-tramp-file-name'.")
16674e4f 1277
00d6fd04
MA
1278(defconst tramp-postfix-method-regexp
1279 (regexp-quote tramp-postfix-method-format)
16674e4f 1280 "*Regexp matching delimeter between method and user or host names.
00d6fd04 1281Derived from `tramp-postfix-method-format'.")
16674e4f 1282
00d6fd04
MA
1283(defconst tramp-user-regexp
1284 "[^:/ \t]+"
1285 "*Regexp matching user names.")
16674e4f 1286
b96e6899
MA
1287(defconst tramp-prefix-domain-format "%"
1288 "*String matching delimeter between user and domain names.")
1289
1290(defconst tramp-prefix-domain-regexp
1291 (regexp-quote tramp-prefix-domain-format)
1292 "*Regexp matching delimeter between user and domain names.
1293Derived from `tramp-prefix-domain-format'.")
1294
1295(defconst tramp-domain-regexp
70c11b0b 1296 "[-a-zA-Z0-9_.]+"
b96e6899
MA
1297 "*Regexp matching domain names.")
1298
1299(defconst tramp-user-with-domain-regexp
1300 (concat "\\(" tramp-user-regexp "\\)"
1301 tramp-prefix-domain-regexp
1302 "\\(" tramp-domain-regexp "\\)")
1303 "*Regexp matching user names with domain names.")
1304
00d6fd04 1305(defconst tramp-postfix-user-format
16674e4f
KG
1306 "@"
1307 "*String matching delimeter between user and host names.
00d6fd04 1308Used in `tramp-make-tramp-file-name'.")
16674e4f 1309
00d6fd04 1310(defconst tramp-postfix-user-regexp
16674e4f
KG
1311 (regexp-quote tramp-postfix-user-format)
1312 "*Regexp matching delimeter between user and host names.
00d6fd04
MA
1313Derived from `tramp-postfix-user-format'.")
1314
1315(defconst tramp-host-regexp
1316 "[a-zA-Z0-9_.-]+"
1317 "*Regexp matching host names.")
1318
b96e6899
MA
1319(defconst tramp-prefix-ipv6-format
1320 (cond ((equal tramp-syntax 'ftp) "[")
1321 ((equal tramp-syntax 'sep) "")
1322 ((equal tramp-syntax 'url) "[")
1323 (t (error "Wrong `tramp-syntax' defined")))
1324 "*String matching left hand side of IPv6 addresses.
1325Used in `tramp-make-tramp-file-name'.")
1326
1327(defconst tramp-prefix-ipv6-regexp
1328 (regexp-quote tramp-prefix-ipv6-format)
1329 "*Regexp matching left hand side of IPv6 addresses.
1330Derived from `tramp-prefix-ipv6-format'.")
1331
e0b6e3b9
MA
1332;; The following regexp is a bit sloppy. But it shall serve our
1333;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1334;; "::ffff:192.168.0.1".
b96e6899 1335(defconst tramp-ipv6-regexp
e0b6e3b9 1336 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
b96e6899
MA
1337 "*Regexp matching IPv6 addresses.")
1338
1339(defconst tramp-postfix-ipv6-format
1340 (cond ((equal tramp-syntax 'ftp) "]")
1341 ((equal tramp-syntax 'sep) "")
1342 ((equal tramp-syntax 'url) "]")
1343 (t (error "Wrong `tramp-syntax' defined")))
1344 "*String matching right hand side of IPv6 addresses.
1345Used in `tramp-make-tramp-file-name'.")
1346
1347(defconst tramp-postfix-ipv6-regexp
1348 (regexp-quote tramp-postfix-ipv6-format)
1349 "*Regexp matching right hand side of IPv6 addresses.
1350Derived from `tramp-postfix-ipv6-format'.")
1351
00d6fd04
MA
1352(defconst tramp-prefix-port-format
1353 (cond ((equal tramp-syntax 'ftp) "#")
1354 ((equal tramp-syntax 'sep) "#")
1355 ((equal tramp-syntax 'url) ":")
1356 (t (error "Wrong `tramp-syntax' defined")))
1357 "*String matching delimeter between host names and port numbers.")
1358
1359(defconst tramp-prefix-port-regexp
1360 (regexp-quote tramp-prefix-port-format)
1361 "*Regexp matching delimeter between host names and port numbers.
1362Derived from `tramp-prefix-port-format'.")
1363
1364(defconst tramp-port-regexp
1365 "[0-9]+"
1366 "*Regexp matching port numbers.")
1367
1368(defconst tramp-host-with-port-regexp
1369 (concat "\\(" tramp-host-regexp "\\)"
1370 tramp-prefix-port-regexp
1371 "\\(" tramp-port-regexp "\\)")
1372 "*Regexp matching host names with port numbers.")
1373
1374(defconst tramp-postfix-host-format
1375 (cond ((equal tramp-syntax 'ftp) ":")
1376 ((equal tramp-syntax 'sep) "]")
1377 ((equal tramp-syntax 'url) "")
1378 (t (error "Wrong `tramp-syntax' defined")))
7432277c 1379 "*String matching delimeter between host names and localnames.
00d6fd04 1380Used in `tramp-make-tramp-file-name'.")
16674e4f 1381
00d6fd04 1382(defconst tramp-postfix-host-regexp
16674e4f 1383 (regexp-quote tramp-postfix-host-format)
7432277c 1384 "*Regexp matching delimeter between host names and localnames.
00d6fd04 1385Derived from `tramp-postfix-host-format'.")
16674e4f 1386
00d6fd04 1387(defconst tramp-localname-regexp
16674e4f 1388 ".*$"
00d6fd04 1389 "*Regexp matching localnames.")
16674e4f
KG
1390
1391;; File name format.
505edaeb 1392
00d6fd04 1393(defconst tramp-file-name-structure
16674e4f
KG
1394 (list
1395 (concat
1396 tramp-prefix-regexp
00d6fd04
MA
1397 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
1398 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
b96e6899
MA
1399 "\\(" "\\(" tramp-host-regexp
1400 "\\|"
1401 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1402 tramp-postfix-ipv6-regexp "\\)"
1403 "\\(" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"
00d6fd04
MA
1404 tramp-postfix-host-regexp
1405 "\\(" tramp-localname-regexp "\\)")
b96e6899 1406 2 4 5 8)
16674e4f 1407
fb7933a3 1408 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
a4aeb9a4 1409the Tramp file name structure.
fb7933a3 1410
a4aeb9a4 1411The first element REGEXP is a regular expression matching a Tramp file
fb7933a3
KG
1412name. The regex should contain parentheses around the method name,
1413the user name, the host name, and the file name parts.
1414
1415The second element METHOD is a number, saying which pair of
1416parentheses matches the method name. The third element USER is
1417similar, but for the user name. The fourth element HOST is similar,
1418but for the host name. The fifth element FILE is for the file name.
1419These numbers are passed directly to `match-string', which see. That
1420means the opening parentheses are counted to identify the pair.
1421
00d6fd04 1422See also `tramp-file-name-regexp'.")
fb7933a3
KG
1423
1424;;;###autoload
505edaeb 1425(defconst tramp-file-name-regexp-unified
b96e6899 1426 "\\`/\\([^[/:]+\\|[^/]+]\\):"
505edaeb
KG
1427 "Value for `tramp-file-name-regexp' for unified remoting.
1428Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
00d6fd04 1429Tramp. See `tramp-file-name-structure' for more explanations.")
505edaeb
KG
1430
1431;;;###autoload
1432(defconst tramp-file-name-regexp-separate
1433 "\\`/\\[.*\\]"
1434 "Value for `tramp-file-name-regexp' for separate remoting.
1435XEmacs uses a separate filename syntax for Tramp and EFS.
00d6fd04 1436See `tramp-file-name-structure' for more explanations.")
505edaeb
KG
1437
1438;;;###autoload
00d6fd04
MA
1439(defconst tramp-file-name-regexp-url
1440 "\\`/[^/:]+://"
1441 "Value for `tramp-file-name-regexp' for URL-like remoting.
1442See `tramp-file-name-structure' for more explanations.")
1443
1444;;;###autoload
1445(defconst tramp-file-name-regexp
1446 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
1447 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
1448 ((equal tramp-syntax 'url) tramp-file-name-regexp-url)
1449 (t (error "Wrong `tramp-syntax' defined")))
94be87e8 1450 "*Regular expression matching file names handled by Tramp.
a4aeb9a4 1451This regexp should match Tramp file names but no other file names.
b533bc97 1452When tramp.el is loaded, this regular expression is prepended to
fb7933a3 1453`file-name-handler-alist', and that is searched sequentially. Thus,
a4aeb9a4
MA
1454if the Tramp entry appears rather early in the `file-name-handler-alist'
1455and is a bit too general, then some files might be considered Tramp
00d6fd04 1456files which are not really Tramp files.
fb7933a3
KG
1457
1458Please note that the entry in `file-name-handler-alist' is made when
b533bc97 1459this file \(tramp.el\) is loaded. This means that this variable must be set
fb7933a3
KG
1460before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1461updated after changing this variable.
1462
00d6fd04 1463Also see `tramp-file-name-structure'.")
fb7933a3 1464
16674e4f 1465;;;###autoload
8a798e41 1466(defconst tramp-root-regexp
00d6fd04 1467 (if (memq system-type '(cygwin windows-nt))
aa485f7c
MA
1468 "\\`\\([a-zA-Z]:\\)?/"
1469 "\\`/")
8a798e41 1470 "Beginning of an incomplete Tramp file name.
aa485f7c 1471Usually, it is just \"\\\\`/\". On W32 systems, there might be a
57671b72 1472volume letter, which will be removed by `tramp-drop-volume-letter'.")
8a798e41
MA
1473
1474;;;###autoload
1475(defconst tramp-completion-file-name-regexp-unified
aa485f7c 1476 (concat tramp-root-regexp "[^/]*\\'")
16674e4f 1477 "Value for `tramp-completion-file-name-regexp' for unified remoting.
8a798e41
MA
1478GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1479See `tramp-file-name-structure' for more explanations.")
fb7933a3 1480
16674e4f
KG
1481;;;###autoload
1482(defconst tramp-completion-file-name-regexp-separate
aa485f7c 1483 (concat tramp-root-regexp "\\([[][^]]*\\)?\\'")
16674e4f
KG
1484 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1485XEmacs uses a separate filename syntax for Tramp and EFS.
00d6fd04 1486See `tramp-file-name-structure' for more explanations.")
fb7933a3 1487
16674e4f 1488;;;###autoload
00d6fd04 1489(defconst tramp-completion-file-name-regexp-url
aa485f7c 1490 (concat tramp-root-regexp "[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
00d6fd04
MA
1491 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1492See `tramp-file-name-structure' for more explanations.")
1493
1494;;;###autoload
1495(defconst tramp-completion-file-name-regexp
1496 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
1497 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
1498 ((equal tramp-syntax 'url) tramp-completion-file-name-regexp-url)
1499 (t (error "Wrong `tramp-syntax' defined")))
a4aeb9a4
MA
1500 "*Regular expression matching file names handled by Tramp completion.
1501This regexp should match partial Tramp file names only.
16674e4f
KG
1502
1503Please note that the entry in `file-name-handler-alist' is made when
1504this file (tramp.el) is loaded. This means that this variable must be set
1505before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1506updated after changing this variable.
1507
00d6fd04 1508Also see `tramp-file-name-structure'.")
fb7933a3 1509
00d6fd04
MA
1510(defconst tramp-actions-before-shell
1511 '((tramp-login-prompt-regexp tramp-action-login)
1512 (tramp-password-prompt-regexp tramp-action-password)
1513 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
ac474af1 1514 (shell-prompt-pattern tramp-action-succeed)
821e6e36 1515 (tramp-shell-prompt-pattern tramp-action-succeed)
3cdaec13 1516 (tramp-yesno-prompt-regexp tramp-action-yesno)
487f4fb7 1517 (tramp-yn-prompt-regexp tramp-action-yn)
19a87064
MA
1518 (tramp-terminal-prompt-regexp tramp-action-terminal)
1519 (tramp-process-alive-regexp tramp-action-process-alive))
ac474af1
KG
1520 "List of pattern/action pairs.
1521Whenever a pattern matches, the corresponding action is performed.
1522Each item looks like (PATTERN ACTION).
1523
1524The PATTERN should be a symbol, a variable. The value of this
1525variable gives the regular expression to search for. Note that the
1526regexp must match at the end of the buffer, \"\\'\" is implicitly
1527appended to it.
1528
1529The ACTION should also be a symbol, but a function. When the
00d6fd04 1530corresponding PATTERN matches, the ACTION function is called.")
ac474af1 1531
00d6fd04 1532(defconst tramp-actions-copy-out-of-band
38c65fca
KG
1533 '((tramp-password-prompt-regexp tramp-action-password)
1534 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
00d6fd04 1535 (tramp-copy-failed-regexp tramp-action-permission-denied)
19a87064 1536 (tramp-process-alive-regexp tramp-action-out-of-band))
38c65fca
KG
1537 "List of pattern/action pairs.
1538This list is used for copying/renaming with out-of-band methods.
90f8dc03 1539
00d6fd04
MA
1540See `tramp-actions-before-shell' for more info.")
1541
1542;; Chunked sending kludge. We set this to 500 for black-listed constellations
7432277c 1543;; known to have a bug in `process-send-string'; some ssh connections appear
7177e2a3
MA
1544;; to drop bytes when data is sent too quickly. There is also a connection
1545;; buffer local variable, which is computed depending on remote host properties
1546;; when `tramp-chunksize' is zero or nil.
7432277c
KG
1547(defcustom tramp-chunksize
1548 (when (and (not (featurep 'xemacs))
1549 (memq system-type '(hpux)))
1550 500)
55880756
MA
1551;; Parentheses in docstring starting at beginning of line are escaped.
1552;; Fontification is messed up when
1553;; `open-paren-in-column-0-is-defun-start' set to t.
7432277c
KG
1554 "*If non-nil, chunksize for sending input to local process.
1555It is necessary only on systems which have a buggy `process-send-string'
1556implementation. The necessity, whether this variable must be set, can be
1557checked via the following code:
1558
1559 (with-temp-buffer
11948172
MA
1560 (let* ((user \"xxx\") (host \"yyy\")
1561 (init 0) (step 50)
1562 (sent init) (received init))
1563 (while (= sent received)
1564 (setq sent (+ sent step))
1565 (erase-buffer)
1566 (let ((proc (start-process (buffer-name) (current-buffer)
1567 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1568 (when (memq (process-status proc) '(run open))
1569 (process-send-string proc (make-string sent ?\\ ))
1570 (process-send-eof proc)
1571 (process-send-eof proc))
1572 (while (not (progn (goto-char (point-min))
1573 (re-search-forward \"\\\\w+\" (point-max) t)))
1574 (accept-process-output proc 1))
1575 (when (memq (process-status proc) '(run open))
1576 (setq received (string-to-number (match-string 0)))
1577 (delete-process proc)
1578 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1579 (sit-for 0))))
1580 (if (> sent (+ init step))
1581 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1582 (- sent step))
1583 (message \"Test does not work\")
1584 (display-buffer (current-buffer))
1585 (sit-for 30))))
1586
1587In the Emacs normally running Tramp, evaluate the above code
55880756 1588\(replace \"xxx\" and \"yyy\" by the remote user and host name,
b533bc97 1589respectively\). You can do this, for example, by pasting it into
11948172
MA
1590the `*scratch*' buffer and then hitting C-j with the cursor after the
1591last closing parenthesis. Note that it works only if you have configured
b533bc97 1592\"ssh\" to run without password query, see ssh-agent\(1\).
11948172
MA
1593
1594You will see the number of bytes sent successfully to the remote host.
1595If that number exceeds 1000, you can stop the execution by hitting
1596C-g, because your Emacs is likely clean.
1597
11948172 1598When it is necessary to set `tramp-chunksize', you might consider to
b533bc97
MA
1599use an out-of-the-band method \(like \"scp\"\) instead of an internal one
1600\(like \"ssh\"\), because setting `tramp-chunksize' to non-nil decreases
11948172 1601performance.
c951aecb 1602
00d6fd04
MA
1603If your Emacs is buggy, the code stops and gives you an indication
1604about the value `tramp-chunksize' should be set. Maybe you could just
1605experiment a bit, e.g. changing the values of `init' and `step'
1606in the third line of the code.
1607
7432277c
KG
1608Please raise a bug report via \"M-x tramp-bug\" if your system needs
1609this variable to be set as well."
1610 :group 'tramp
b1a2b924 1611 :type '(choice (const nil) integer))
7432277c 1612
5ec2cc41
KG
1613;; Logging in to a remote host normally requires obtaining a pty. But
1614;; Emacs on MacOS X has process-connection-type set to nil by default,
1615;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1616;; for an override of the system default.
1617(defcustom tramp-process-connection-type t
1618 "Overrides `process-connection-type' for connections from Tramp.
1619Tramp binds process-connection-type to the value given here before
1620opening a connection to a remote host."
1621 :group 'tramp
1622 :type '(choice (const nil) (const t) (const pty)))
1623
b50dd0d2
MA
1624(defcustom tramp-completion-reread-directory-timeout 10
1625 "Defines seconds since last remote command before rereading a directory.
1626A remote directory might have changed its contents. In order to
1627make it visible during file name completion in the minibuffer,
1628Tramp flushes its cache and rereads the directory contents when
1629more than `tramp-completion-reread-directory-timeout' seconds
1630have been gone since last remote command execution. A value of 0
1631would require an immediate reread during filename completion, nil
1632means to use always cached values for the directory contents."
1633 :group 'tramp
1634 :type '(choice (const nil) integer))
1635
fb7933a3
KG
1636;;; Internal Variables:
1637
fb7933a3 1638(defvar tramp-current-method nil
00d6fd04 1639 "Connection method for this *tramp* buffer.")
fb7933a3
KG
1640
1641(defvar tramp-current-user nil
00d6fd04 1642 "Remote login name for this *tramp* buffer.")
fb7933a3
KG
1643
1644(defvar tramp-current-host nil
00d6fd04
MA
1645 "Remote host for this *tramp* buffer.")
1646
1647(defconst tramp-uudecode
1648 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
fabf2143 1649cat /tmp/tramp.$$
00d6fd04 1650rm -f /tmp/tramp.$$"
fabf2143 1651 "Shell function to implement `uudecode' to standard output.
c08e6004
MA
1652Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1653for this or `uudecode -p', but some systems don't, and for them
1654we have this shell function.")
fabf2143 1655
293c24f9
MA
1656(defconst tramp-perl-file-truename
1657 "%s -e '
1658use File::Spec;
1659use Cwd \"realpath\";
1660
1661sub recursive {
1662 my ($volume, @dirs) = @_;
1663 my $real = realpath(File::Spec->catpath(
1664 $volume, File::Spec->catdir(@dirs), \"\"));
1665 if ($real) {
1666 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1667 return ($vol, File::Spec->splitdir($dir));
1668 }
1669 else {
1670 my $last = pop(@dirs);
1671 ($volume, @dirs) = recursive($volume, @dirs);
1672 push(@dirs, $last);
1673 return ($volume, @dirs);
1674 }
1675}
1676
1677$result = realpath($ARGV[0]);
1678if (!$result) {
1679 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1680 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1681
1682 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1683}
1684
1685if ($ARGV[0] =~ /\\/$/) {
1686 $result = $result . \"/\";
1687}
1688
1689print \"\\\"$result\\\"\\n\";
1690' \"$1\" 2>/dev/null"
1691 "Perl script to produce output suitable for use with `file-truename'
1692on the remote file system.
1693Escape sequence %s is replaced with name of Perl binary.
1694This string is passed to `format', so percent characters need to be doubled.")
1695
1696(defconst tramp-perl-file-name-all-completions
1697 "%s -e 'sub case {
1698 my $str = shift;
1699 if ($ARGV[2]) {
1700 return lc($str);
1701 }
1702 else {
1703 return $str;
1704 }
1705}
1706opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1707@files = readdir(d); closedir(d);
1708foreach $f (@files) {
1709 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1710 if (-d \"$ARGV[0]/$f\") {
1711 print \"$f/\\n\";
1712 }
1713 else {
1714 print \"$f\\n\";
1715 }
1716 }
1717}
1718print \"ok\\n\"
1719' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1720 "Perl script to produce output suitable for use with
1721`file-name-all-completions' on the remote file system. Escape
1722sequence %s is replaced with name of Perl binary. This string is
1723passed to `format', so percent characters need to be doubled.")
1724
fabf2143
KG
1725;; Perl script to implement `file-attributes' in a Lisp `read'able
1726;; output. If you are hacking on this, note that you get *no* output
1727;; unless this spits out a complete line, including the '\n' at the
1728;; end.
8daea7fc 1729;; The device number is returned as "-1", because there will be a virtual
b946a456 1730;; device number set in `tramp-handle-file-attributes'.
00d6fd04
MA
1731(defconst tramp-perl-file-attributes
1732 "%s -e '
c82c5727 1733@stat = lstat($ARGV[0]);
680db9ac
MA
1734if (!@stat) {
1735 print \"nil\\n\";
1736 exit 0;
1737}
c82c5727
LH
1738if (($stat[2] & 0170000) == 0120000)
1739{
1740 $type = readlink($ARGV[0]);
1741 $type = \"\\\"$type\\\"\";
1742}
1743elsif (($stat[2] & 0170000) == 040000)
1744{
1745 $type = \"t\";
1746}
1747else
1748{
1749 $type = \"nil\"
1750};
1751$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1752$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1753printf(
d4443a0d 1754 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
c82c5727
LH
1755 $type,
1756 $stat[3],
1757 $uid,
1758 $gid,
1759 $stat[8] >> 16 & 0xffff,
1760 $stat[8] & 0xffff,
1761 $stat[9] >> 16 & 0xffff,
1762 $stat[9] & 0xffff,
1763 $stat[10] >> 16 & 0xffff,
1764 $stat[10] & 0xffff,
1765 $stat[7],
1766 $stat[2],
1767 $stat[1] >> 16 & 0xffff,
1768 $stat[1] & 0xffff
00d6fd04 1769);' \"$1\" \"$2\" \"$3\" 2>/dev/null"
fb7933a3 1770 "Perl script to produce output suitable for use with `file-attributes'
00d6fd04
MA
1771on the remote file system.
1772Escape sequence %s is replaced with name of Perl binary.
1773This string is passed to `format', so percent characters need to be doubled.")
fb7933a3 1774
00d6fd04
MA
1775(defconst tramp-perl-directory-files-and-attributes
1776 "%s -e '
8cb0a559
LH
1777chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1778opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
c82c5727
LH
1779@list = readdir(DIR);
1780closedir(DIR);
1781$n = scalar(@list);
1782printf(\"(\\n\");
1783for($i = 0; $i < $n; $i++)
1784{
1785 $filename = $list[$i];
1786 @stat = lstat($filename);
1787 if (($stat[2] & 0170000) == 0120000)
1788 {
1789 $type = readlink($filename);
1790 $type = \"\\\"$type\\\"\";
1791 }
1792 elsif (($stat[2] & 0170000) == 040000)
1793 {
1794 $type = \"t\";
1795 }
1796 else
1797 {
1798 $type = \"nil\"
1799 };
1800 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1801 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1802 printf(
b946a456 1803 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
c82c5727
LH
1804 $filename,
1805 $type,
1806 $stat[3],
1807 $uid,
1808 $gid,
1809 $stat[8] >> 16 & 0xffff,
1810 $stat[8] & 0xffff,
1811 $stat[9] >> 16 & 0xffff,
1812 $stat[9] & 0xffff,
1813 $stat[10] >> 16 & 0xffff,
1814 $stat[10] & 0xffff,
1815 $stat[7],
1816 $stat[2],
1817 $stat[1] >> 16 & 0xffff,
1818 $stat[1] & 0xffff,
1819 $stat[0] >> 16 & 0xffff,
1820 $stat[0] & 0xffff);
1821}
00d6fd04 1822printf(\")\\n\");' \"$1\" \"$2\" \"$3\" 2>/dev/null"
c82c5727 1823 "Perl script implementing `directory-files-attributes' as Lisp `read'able
00d6fd04
MA
1824output.
1825Escape sequence %s is replaced with name of Perl binary.
1826This string is passed to `format', so percent characters need to be doubled.")
c82c5727 1827
ac474af1
KG
1828;; ;; These two use uu encoding.
1829;; (defvar tramp-perl-encode "%s -e'\
1830;; print qq(begin 644 xxx\n);
1831;; my $s = q();
1832;; my $res = q();
1833;; while (read(STDIN, $s, 45)) {
1834;; print pack(q(u), $s);
1835;; }
1836;; print qq(`\n);
1837;; print qq(end\n);
1838;; '"
1839;; "Perl program to use for encoding a file.
1840;; Escape sequence %s is replaced with name of Perl binary.")
1841
1842;; (defvar tramp-perl-decode "%s -ne '
1843;; print unpack q(u), $_;
1844;; '"
1845;; "Perl program to use for decoding a file.
1846;; Escape sequence %s is replaced with name of Perl binary.")
1847
1848;; These two use base64 encoding.
00d6fd04
MA
1849(defconst tramp-perl-encode-with-module
1850 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
ac474af1 1851 "Perl program to use for encoding a file.
b1d06e75 1852Escape sequence %s is replaced with name of Perl binary.
89509ea0 1853This string is passed to `format', so percent characters need to be doubled.
b1d06e75
KG
1854This implementation requires the MIME::Base64 Perl module to be installed
1855on the remote host.")
1856
00d6fd04
MA
1857(defconst tramp-perl-decode-with-module
1858 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
b1d06e75
KG
1859 "Perl program to use for decoding a file.
1860Escape sequence %s is replaced with name of Perl binary.
89509ea0 1861This string is passed to `format', so percent characters need to be doubled.
b1d06e75
KG
1862This implementation requires the MIME::Base64 Perl module to be installed
1863on the remote host.")
1864
00d6fd04 1865(defconst tramp-perl-encode
b1d06e75
KG
1866 "%s -e '
1867# This script contributed by Juanma Barranquero <lektu@terra.es>.
114f9c96 1868# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
cbd12ed7 1869# Free Software Foundation, Inc.
b1d06e75
KG
1870use strict;
1871
fa32e96a 1872my %%trans = do {
b1d06e75
KG
1873 my $i = 0;
1874 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1875 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1876};
1877
36541701 1878binmode(\\*STDIN);
b1d06e75
KG
1879
1880# We read in chunks of 54 bytes, to generate output lines
1881# of 72 chars (plus end of line)
36541701 1882$/ = \\54;
b1d06e75
KG
1883
1884while (my $data = <STDIN>) {
1885 my $pad = q();
1886
1887 # Only for the last chunk, and only if did not fill the last three-byte packet
1888 if (eof) {
fa32e96a 1889 my $mod = length($data) %% 3;
b1d06e75
KG
1890 $pad = q(=) x (3 - $mod) if $mod;
1891 }
1892
1893 # Not the fastest method, but it is simple: unpack to binary string, split
1894 # by groups of 6 bits and convert back from binary to byte; then map into
1895 # the translation table
1896 print
1897 join q(),
1898 map($trans{$_},
1899 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1900 $pad,
36541701 1901 qq(\\n);
00d6fd04 1902}' 2>/dev/null"
b1d06e75 1903 "Perl program to use for encoding a file.
fa32e96a 1904Escape sequence %s is replaced with name of Perl binary.
ccf29586 1905This string is passed to `format', so percent characters need to be doubled.")
ac474af1 1906
00d6fd04 1907(defconst tramp-perl-decode
b1d06e75
KG
1908 "%s -e '
1909# This script contributed by Juanma Barranquero <lektu@terra.es>.
114f9c96 1910# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
cbd12ed7 1911# Free Software Foundation, Inc.
b1d06e75
KG
1912use strict;
1913
fa32e96a 1914my %%trans = do {
b1d06e75 1915 my $i = 0;
16674e4f 1916 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
b1d06e75
KG
1917 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1918};
1919
fa32e96a 1920my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
b1d06e75 1921
36541701 1922binmode(\\*STDOUT);
b1d06e75
KG
1923
1924# We are going to accumulate into $pending to accept any line length
1925# (we do not check they are <= 76 chars as the RFC says)
1926my $pending = q();
1927
1928while (my $data = <STDIN>) {
1929 chomp $data;
1930
1931 # If we find one or two =, we have reached the end and
1932 # any following data is to be discarded
1933 my $finished = $data =~ s/(==?).*/$1/;
1934 $pending .= $data;
1935
1936 my $len = length($pending);
16674e4f 1937 my $chunk = substr($pending, 0, $len & ~3);
414da5ab 1938 $pending = substr($pending, $len & ~3 + 1);
b1d06e75
KG
1939
1940 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1941 # split in 8-bit chunks and convert back to char.
1942 print join q(),
1943 map $bytes{$_},
1944 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1945
1946 last if $finished;
00d6fd04 1947}' 2>/dev/null"
ac474af1 1948 "Perl program to use for decoding a file.
fa32e96a 1949Escape sequence %s is replaced with name of Perl binary.
ccf29586 1950This string is passed to `format', so percent characters need to be doubled.")
fb7933a3 1951
946a5aeb
MA
1952(defconst tramp-vc-registered-read-file-names
1953 "echo \"(\"
1954for file in \"$@\"; do
1955 if %s $file; then
1956 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1957 else
1958 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1959 fi
1960 if %s $file; then
1961 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1962 else
1963 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1964 fi
1965done
1966echo \")\""
1967 "Script to check existence of VC related files.
1968It must be send formatted with two strings; the tests for file
1969existence, and file readability.")
1970
9ce8462a
MA
1971(defconst tramp-file-mode-type-map
1972 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1973 (1 . "p") ; fifo
1974 (2 . "c") ; character device
1975 (3 . "m") ; multiplexed character device (v7)
1976 (4 . "d") ; directory
1977 (5 . "?") ; Named special file (XENIX)
1978 (6 . "b") ; block device
1979 (7 . "?") ; multiplexed block device (v7)
1980 (8 . "-") ; regular file
1981 (9 . "n") ; network special file (HP-UX)
1982 (10 . "l") ; symlink
1983 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1984 (12 . "s") ; socket
1985 (13 . "D") ; door special (Solaris)
1986 (14 . "w")) ; whiteout (BSD)
fb7933a3
KG
1987 "A list of file types returned from the `stat' system call.
1988This is used to map a mode number to a permission string.")
1989
fb7933a3 1990;; New handlers should be added here. The following operations can be
c0fc6170
MA
1991;; handled using the normal primitives: file-name-sans-versions,
1992;; get-file-buffer.
fb7933a3 1993(defconst tramp-file-name-handler-alist
00d6fd04 1994 '((load . tramp-handle-load)
fb7933a3 1995 (make-symbolic-link . tramp-handle-make-symbolic-link)
c0fc6170 1996 (file-name-as-directory . tramp-handle-file-name-as-directory)
fb7933a3
KG
1997 (file-name-directory . tramp-handle-file-name-directory)
1998 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1999 (file-truename . tramp-handle-file-truename)
2000 (file-exists-p . tramp-handle-file-exists-p)
2001 (file-directory-p . tramp-handle-file-directory-p)
2002 (file-executable-p . tramp-handle-file-executable-p)
fb7933a3
KG
2003 (file-readable-p . tramp-handle-file-readable-p)
2004 (file-regular-p . tramp-handle-file-regular-p)
2005 (file-symlink-p . tramp-handle-file-symlink-p)
2006 (file-writable-p . tramp-handle-file-writable-p)
2007 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
2008 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
2009 (file-attributes . tramp-handle-file-attributes)
2010 (file-modes . tramp-handle-file-modes)
fb7933a3 2011 (directory-files . tramp-handle-directory-files)
c82c5727 2012 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
fb7933a3
KG
2013 (file-name-all-completions . tramp-handle-file-name-all-completions)
2014 (file-name-completion . tramp-handle-file-name-completion)
2015 (add-name-to-file . tramp-handle-add-name-to-file)
2016 (copy-file . tramp-handle-copy-file)
263c02ef 2017 (copy-directory . tramp-handle-copy-directory)
fb7933a3
KG
2018 (rename-file . tramp-handle-rename-file)
2019 (set-file-modes . tramp-handle-set-file-modes)
ce3f516f 2020 (set-file-times . tramp-handle-set-file-times)
fb7933a3
KG
2021 (make-directory . tramp-handle-make-directory)
2022 (delete-directory . tramp-handle-delete-directory)
2023 (delete-file . tramp-handle-delete-file)
2024 (directory-file-name . tramp-handle-directory-file-name)
00d6fd04
MA
2025 ;; `executable-find' is not official yet.
2026 (executable-find . tramp-handle-executable-find)
2027 (start-file-process . tramp-handle-start-file-process)
0457dd55 2028 (process-file . tramp-handle-process-file)
00d6fd04 2029 (shell-command . tramp-handle-shell-command)
fb7933a3
KG
2030 (insert-directory . tramp-handle-insert-directory)
2031 (expand-file-name . tramp-handle-expand-file-name)
00d6fd04 2032 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
fb7933a3 2033 (file-local-copy . tramp-handle-file-local-copy)
19a87064 2034 (file-remote-p . tramp-handle-file-remote-p)
fb7933a3 2035 (insert-file-contents . tramp-handle-insert-file-contents)
94be87e8
MA
2036 (insert-file-contents-literally
2037 . tramp-handle-insert-file-contents-literally)
fb7933a3 2038 (write-region . tramp-handle-write-region)
38c65fca 2039 (find-backup-file-name . tramp-handle-find-backup-file-name)
c1105d05 2040 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
fb7933a3 2041 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
5ec2cc41 2042 (dired-compress-file . tramp-handle-dired-compress-file)
fb7933a3
KG
2043 (dired-recursive-delete-directory
2044 . tramp-handle-dired-recursive-delete-directory)
70c11b0b 2045 (dired-uncache . tramp-handle-dired-uncache)
fb7933a3 2046 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
49096407 2047 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
632c5478
MA
2048 (file-selinux-context . tramp-handle-file-selinux-context)
2049 (set-file-selinux-context . tramp-handle-set-file-selinux-context)
49096407 2050 (vc-registered . tramp-handle-vc-registered))
c1105d05 2051 "Alist of handler functions.
fb7933a3
KG
2052Operations not mentioned here will be handled by the normal Emacs functions.")
2053
a4aeb9a4 2054;; Handlers for partial Tramp file names. For Emacs just
41c8e348 2055;; `file-name-all-completions' is needed.
a01b1e22 2056;;;###autoload
16674e4f 2057(defconst tramp-completion-file-name-handler-alist
a01b1e22 2058 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
41c8e348 2059 (file-name-completion . tramp-completion-handle-file-name-completion))
16674e4f
KG
2060 "Alist of completion handler functions.
2061Used for file names matching `tramp-file-name-regexp'. Operations not
2062mentioned here will be handled by `tramp-file-name-handler-alist' or the
2063normal Emacs functions.")
2064
4007ba5b 2065;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
ea9d1443
KG
2066(defvar tramp-foreign-file-name-handler-alist
2067 ;; (identity . tramp-sh-file-name-handler) should always be the last
b88f2d0a 2068 ;; entry, because `identity' always matches.
ea9d1443 2069 '((identity . tramp-sh-file-name-handler))
4007ba5b
KG
2070 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2071If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2072calling HANDLER.")
2073
0664ff72 2074;;; Internal functions which must come first:
fb7933a3 2075
00d6fd04
MA
2076(defsubst tramp-debug-message (vec fmt-string &rest args)
2077 "Append message to debug buffer.
2078Message is formatted with FMT-STRING as control string and the remaining
2079ARGS to actually emit the message (if applicable)."
2080 (when (get-buffer (tramp-buffer-name vec))
2081 (with-current-buffer (tramp-get-debug-buffer vec)
2082 (goto-char (point-max))
70c11b0b
MA
2083 ;; Headline.
2084 (when (bobp)
2085 (insert
2086 (format
2087 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
2088 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
2089 emacs-version tramp-version)))
00d6fd04
MA
2090 (unless (bolp)
2091 (insert "\n"))
70c11b0b 2092 ;; Timestamp.
736ac90f
MA
2093 (let ((now (current-time)))
2094 (insert (format-time-string "%T." now))
2095 (insert (format "%06d " (nth 2 now))))
70c11b0b 2096 ;; Calling function.
00d6fd04
MA
2097 (let ((btn 1) btf fn)
2098 (while (not fn)
2099 (setq btf (nth 1 (backtrace-frame btn)))
2100 (if (not btf)
2101 (setq fn "")
2102 (when (symbolp btf)
2103 (setq fn (symbol-name btf))
2104 (unless (and (string-match "^tramp" fn)
2105 (not (string-match
2106 "^tramp\\(-debug\\)?\\(-message\\|-error\\)$"
2107 fn)))
2108 (setq fn nil)))
2109 (setq btn (1+ btn))))
2110 ;; The following code inserts filename and line number.
2111 ;; Should be deactivated by default, because it is time
2112 ;; consuming.
2113; (let ((ffn (find-function-noselect (intern fn))))
2114; (insert
2115; (format
2116; "%s:%d: "
2117; (file-name-nondirectory (buffer-file-name (car ffn)))
2118; (with-current-buffer (car ffn)
2119; (1+ (count-lines (point-min) (cdr ffn)))))))
2120 (insert (format "%s " fn)))
70c11b0b 2121 ;; The message.
00d6fd04
MA
2122 (insert (apply 'format fmt-string args)))))
2123
946a5aeb
MA
2124(defvar tramp-message-show-message t
2125 "Show Tramp message in the minibuffer.
2126This variable is used to disable messages from `tramp-error'.
2127The messages are visible anyway, because an error is raised.")
2128
00d6fd04 2129(defsubst tramp-message (vec-or-proc level fmt-string &rest args)
fb7933a3 2130 "Emit a message depending on verbosity level.
a4aeb9a4 2131VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
00d6fd04
MA
2132vector or a process. LEVEL says to be quiet if `tramp-verbose' is
2133less than LEVEL. The message is emitted only if `tramp-verbose' is
2134greater than or equal to LEVEL.
2135
2136The message is also logged into the debug buffer when `tramp-verbose'
2137is greater than or equal 4.
2138
2139Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2140control string and the remaining ARGS to actually emit the message (if
2141applicable)."
2142 (condition-case nil
2143 (when (<= level tramp-verbose)
2144 ;; Match data must be preserved!
2145 (save-match-data
2146 ;; Display only when there is a minimum level.
946a5aeb 2147 (when (and tramp-message-show-message (<= level 3))
00d6fd04
MA
2148 (apply 'message
2149 (concat
2150 (cond
2151 ((= level 0) "")
2152 ((= level 1) "")
2153 ((= level 2) "Warning: ")
2154 (t "Tramp: "))
2155 fmt-string)
2156 args))
2157 ;; Log only when there is a minimum level.
2158 (when (>= tramp-verbose 4)
2159 (when (and vec-or-proc
2160 (processp vec-or-proc)
2161 (buffer-name (process-buffer vec-or-proc)))
2162 (with-current-buffer (process-buffer vec-or-proc)
2163 ;; Translate proc to vec.
2164 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
2165 (when (and vec-or-proc (vectorp vec-or-proc))
2166 (apply 'tramp-debug-message
2167 vec-or-proc
2168 (concat (format "(%d) # " level) fmt-string)
2169 args)))))
2170 ;; Suppress all errors.
2171 (error nil)))
2172
2173(defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
2174 "Emit an error.
2175VEC-OR-PROC identifies the connection to use, SIGNAL is the
2176signal identifier to be raised, remaining args passed to
2177`tramp-message'. Finally, signal SIGNAL is raised."
946a5aeb
MA
2178 (let (tramp-message-show-message)
2179 (tramp-message
2180 vec-or-proc 1 "%s"
2181 (error-message-string
2182 (list signal
2183 (get signal 'error-message)
2184 (apply 'format fmt-string args))))
2185 (signal signal (list (apply 'format fmt-string args)))))
00d6fd04
MA
2186
2187(defsubst tramp-error-with-buffer
2188 (buffer vec-or-proc signal fmt-string &rest args)
2189 "Emit an error, and show BUFFER.
2190If BUFFER is nil, show the connection buffer. Wait for 30\", or until
2191an input event arrives. The other arguments are passed to `tramp-error'."
2192 (save-window-excursion
2193 (unwind-protect
2194 (apply 'tramp-error vec-or-proc signal fmt-string args)
4874f5e6
MA
2195 (when (and vec-or-proc
2196 (not (zerop tramp-verbose))
2197 (not (tramp-completion-mode-p)))
00d6fd04
MA
2198 (let ((enable-recursive-minibuffers t))
2199 (pop-to-buffer
2200 (or (and (bufferp buffer) buffer)
2201 (and (processp vec-or-proc) (process-buffer vec-or-proc))
2202 (tramp-get-buffer vec-or-proc)))
2203 (sit-for 30))))))
fb7933a3 2204
c62c9d08
KG
2205(defmacro with-parsed-tramp-file-name (filename var &rest body)
2206 "Parse a Tramp filename and make components available in the body.
2207
2208First arg FILENAME is evaluated and dissected into its components.
2209Second arg VAR is a symbol. It is used as a variable name to hold
2210the filename structure. It is also used as a prefix for the variables
2211holding the components. For example, if VAR is the symbol `foo', then
00d6fd04
MA
2212`foo' will be bound to the whole structure, `foo-method' will be bound to
2213the method component, and so on for `foo-user', `foo-host', `foo-localname'.
c62c9d08
KG
2214
2215Remaining args are Lisp expressions to be evaluated (inside an implicit
2216`progn').
2217
00d6fd04
MA
2218If VAR is nil, then we bind `v' to the structure and `method', `user',
2219`host', `localname' to the components."
c62c9d08 2220 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
c62c9d08
KG
2221 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2222 (tramp-file-name-method ,(or var 'v)))
2223 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2224 (tramp-file-name-user ,(or var 'v)))
2225 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2226 (tramp-file-name-host ,(or var 'v)))
7432277c
KG
2227 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2228 (tramp-file-name-localname ,(or var 'v))))
c62c9d08
KG
2229 ,@body))
2230
2231(put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
00d6fd04 2232(put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
9e6ab520 2233(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
c62c9d08 2234
00d6fd04
MA
2235(defmacro with-file-property (vec file property &rest body)
2236 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2237FILE must be a local file name on a connection identified via VEC."
2238 `(if (file-name-absolute-p ,file)
2239 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
2240 (when (eq value 'undef)
2241 ;; We cannot pass @body as parameter to
2242 ;; `tramp-set-file-property' because it mangles our
2243 ;; debug messages.
2244 (setq value (progn ,@body))
2245 (tramp-set-file-property ,vec ,file ,property value))
2246 value)
2247 ,@body))
9ce8462a 2248
00d6fd04
MA
2249(put 'with-file-property 'lisp-indent-function 3)
2250(put 'with-file-property 'edebug-form-spec t)
9e6ab520 2251(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>"))
00d6fd04
MA
2252
2253(defmacro with-connection-property (key property &rest body)
a94d821f 2254 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
00d6fd04
MA
2255 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
2256 (when (eq value 'undef)
2257 ;; We cannot pass ,@body as parameter to
2258 ;; `tramp-set-connection-property' because it mangles our debug
2259 ;; messages.
2260 (setq value (progn ,@body))
2261 (tramp-set-connection-property ,key ,property value))
2262 value))
9ce8462a 2263
00d6fd04
MA
2264(put 'with-connection-property 'lisp-indent-function 2)
2265(put 'with-connection-property 'edebug-form-spec t)
9e6ab520 2266(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>"))
00d6fd04 2267
a94d821f
MA
2268(defmacro with-progress-reporter (vec level message &rest body)
2269 "Executes BODY, spinning a progress reporter with MESSAGE."
2270 `(let (pr tm)
2271 (tramp-message ,vec ,level "%s..." ,message)
2272 ;; We start a pulsing progress reporter after 3 seconds. Feature
2273 ;; introduced in Emacs 24.1.
2274 (when (<= ,level tramp-verbose)
2275 (condition-case nil
0d5852cf
MA
2276 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message)
2277 tm (if pr (run-at-time 3 0.1 'progress-reporter-update pr)))
a94d821f
MA
2278 (error nil)))
2279 (unwind-protect
2280 ;; Execute the body.
2281 (progn ,@body)
2282 ;; Stop progress reporter.
0d5852cf 2283 (if tm (tramp-compat-funcall 'cancel-timer tm))
a94d821f
MA
2284 (tramp-message ,vec ,level "%s...done" ,message))))
2285
2286(put 'with-progress-reporter 'lisp-indent-function 3)
2287(put 'with-progress-reporter 'edebug-form-spec t)
2288(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-progress-reporter\\>"))
2289
2290(eval-and-compile ;; Silence compiler.
628c97b2
GM
2291 (if (memq system-type '(cygwin windows-nt))
2292 (defun tramp-drop-volume-letter (name)
2293 "Cut off unnecessary drive letter from file NAME.
2294The function `tramp-handle-expand-file-name' calls `expand-file-name'
2295locally on a remote file name. When the local system is a W32 system
2296but the remote system is Unix, this introduces a superfluous drive
2297letter into the file name. This function removes it."
2298 (save-match-data
2299 (if (string-match tramp-root-regexp name)
2300 (replace-match "/" nil t name)
2301 name)))
2302
2303 (defalias 'tramp-drop-volume-letter 'identity)))
2304
9c13938d 2305(defsubst tramp-make-tramp-temp-file (vec)
a6e96327 2306 "Create a temporary file on the remote host identified by VEC.
9c13938d
MA
2307Return the local name of the temporary file."
2308 (let ((prefix
2309 (tramp-make-tramp-file-name
2310 (tramp-file-name-method vec)
2311 (tramp-file-name-user vec)
2312 (tramp-file-name-host vec)
113e2a84
MA
2313 (tramp-drop-volume-letter
2314 (expand-file-name
2315 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
9c13938d
MA
2316 result)
2317 (while (not result)
2318 ;; `make-temp-file' would be the natural choice for
2319 ;; implementation. But it calls `write-region' internally,
2320 ;; which also needs a temporary file - we would end in an
2321 ;; infinite loop.
2322 (setq result (make-temp-name prefix))
2323 (if (file-exists-p result)
2324 (setq result nil)
2325 ;; This creates the file by side effect.
2326 (set-file-times result)
2327 (set-file-modes result (tramp-octal-to-decimal "0700"))))
2328
2329 ;; Return the local part.
2330 (with-parsed-tramp-file-name result nil localname)))
8a4438b6
MA
2331
2332
16674e4f
KG
2333;;; Config Manipulation Functions:
2334
2335(defun tramp-set-completion-function (method function-list)
2336 "Sets the list of completion functions for METHOD.
2337FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2338The FUNCTION is intended to parse FILE according its syntax.
2339It might be a predefined FUNCTION, or a user defined FUNCTION.
2340Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
8fc29035 2341`tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
8daea7fc
KG
2342and `tramp-parse-netrc'.
2343
16674e4f
KG
2344Example:
2345
2346 (tramp-set-completion-function
2347 \"ssh\"
8daea7fc
KG
2348 '((tramp-parse-sconfig \"/etc/ssh_config\")
2349 (tramp-parse-sconfig \"~/.ssh/config\")))"
16674e4f 2350
5ec2cc41
KG
2351 (let ((r function-list)
2352 (v function-list))
2353 (setq tramp-completion-function-alist
2354 (delete (assoc method tramp-completion-function-alist)
2355 tramp-completion-function-alist))
2356
2357 (while v
00d6fd04 2358 ;; Remove double entries.
5ec2cc41
KG
2359 (when (member (car v) (cdr v))
2360 (setcdr v (delete (car v) (cdr v))))
00d6fd04 2361 ;; Check for function and file or registry key.
5ec2cc41 2362 (unless (and (functionp (nth 0 (car v)))
00d6fd04
MA
2363 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
2364 ;; Windows registry.
2365 (and (memq system-type '(cygwin windows-nt))
a4aeb9a4
MA
2366 (zerop
2367 (tramp-local-call-process
2368 "reg" nil nil nil "query" (nth 1 (car v)))))
00d6fd04
MA
2369 ;; Configuration file.
2370 (file-exists-p (nth 1 (car v)))))
5ec2cc41
KG
2371 (setq r (delete (car v) r)))
2372 (setq v (cdr v)))
2373
2374 (when r
4007ba5b 2375 (add-to-list 'tramp-completion-function-alist
5ec2cc41 2376 (cons method r)))))
16674e4f
KG
2377
2378(defun tramp-get-completion-function (method)
00d6fd04 2379 "Returns a list of completion functions for METHOD.
16674e4f 2380For definition of that list see `tramp-set-completion-function'."
00d6fd04
MA
2381 (cons
2382 ;; Hosts visited once shall be remembered.
2383 `(tramp-parse-connection-properties ,method)
2384 ;; The method related defaults.
2385 (cdr (assoc method tramp-completion-function-alist))))
16674e4f 2386
d037d501 2387
0664ff72 2388;;; Fontification of `read-file-name':
d037d501 2389
0664ff72 2390;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
d037d501
MA
2391(defvar tramp-rfn-eshadow-overlay)
2392(make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
2393
2394(defun tramp-rfn-eshadow-setup-minibuffer ()
2395 "Set up a minibuffer for `file-name-shadow-mode'.
2396Adds another overlay hiding filename parts according to Tramp's
2397special handling of `substitute-in-file-name'."
9ce8462a 2398 (when (symbol-value 'minibuffer-completing-file-name)
d037d501 2399 (setq tramp-rfn-eshadow-overlay
0d5852cf
MA
2400 (tramp-compat-funcall
2401 'make-overlay
2402 (tramp-compat-funcall 'minibuffer-prompt-end)
2403 (tramp-compat-funcall 'minibuffer-prompt-end)))
d037d501 2404 ;; Copy rfn-eshadow-overlay properties.
0d5852cf
MA
2405 (let ((props (tramp-compat-funcall
2406 'overlay-properties (symbol-value 'rfn-eshadow-overlay))))
d037d501 2407 (while props
0d5852cf
MA
2408 (tramp-compat-funcall
2409 'overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))))))
d037d501
MA
2410
2411(when (boundp 'rfn-eshadow-setup-minibuffer-hook)
2412 (add-hook 'rfn-eshadow-setup-minibuffer-hook
48846dc5
MA
2413 'tramp-rfn-eshadow-setup-minibuffer)
2414 (add-hook 'tramp-unload-hook
aa485f7c
MA
2415 (lambda ()
2416 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2417 'tramp-rfn-eshadow-setup-minibuffer))))
d037d501 2418
adcbca53
MA
2419(defconst tramp-rfn-eshadow-update-overlay-regexp
2420 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
2421
d037d501
MA
2422(defun tramp-rfn-eshadow-update-overlay ()
2423 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2424This is intended to be used as a minibuffer `post-command-hook' for
2425`file-name-shadow-mode'; the minibuffer should have already
2426been set up by `rfn-eshadow-setup-minibuffer'."
2427 ;; In remote files name, there is a shadowing just for the local part.
0d5852cf
MA
2428 (let ((end (or (tramp-compat-funcall
2429 'overlay-end (symbol-value 'rfn-eshadow-overlay))
2430 (tramp-compat-funcall 'minibuffer-prompt-end))))
2431 (when
2432 (file-remote-p
2433 (tramp-compat-funcall 'buffer-substring-no-properties end (point-max)))
bd316474
KY
2434 (save-excursion
2435 (save-restriction
2436 (narrow-to-region
adcbca53
MA
2437 (1+ (or (string-match
2438 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
2439 end))
2440 (point-max))
bd316474
KY
2441 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
2442 (rfn-eshadow-update-overlay-hook nil))
0d5852cf
MA
2443 (tramp-compat-funcall
2444 'move-overlay rfn-eshadow-overlay (point-max) (point-max))
2445 (tramp-compat-funcall 'rfn-eshadow-update-overlay)))))))
d037d501
MA
2446
2447(when (boundp 'rfn-eshadow-update-overlay-hook)
2448 (add-hook 'rfn-eshadow-update-overlay-hook
b88f2d0a
MA
2449 'tramp-rfn-eshadow-update-overlay)
2450 (add-hook 'tramp-unload-hook
2451 (lambda ()
2452 (remove-hook 'rfn-eshadow-update-overlay-hook
2453 'tramp-rfn-eshadow-update-overlay))))
d037d501
MA
2454
2455
605a20a9
MA
2456;;; Integration of eshell.el:
2457
2458(eval-when-compile
2459 (defvar eshell-path-env))
2460
2461;; eshell.el keeps the path in `eshell-path-env'. We must change it
2462;; when `default-directory' points to another host.
2463(defun tramp-eshell-directory-change ()
2464 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2465 (setq eshell-path-env
2466 (if (file-remote-p default-directory)
2467 (with-parsed-tramp-file-name default-directory nil
2468 (mapconcat
2469 'identity
2470 (tramp-get-remote-path v)
2471 ":"))
2472 (getenv "PATH"))))
2473
2474(eval-after-load "esh-util"
2475 '(progn
2476 (tramp-eshell-directory-change)
2477 (add-hook 'eshell-directory-change-hook
2478 'tramp-eshell-directory-change)
2479 (add-hook 'tramp-unload-hook
2480 (lambda ()
2481 (remove-hook 'eshell-directory-change-hook
2482 'tramp-eshell-directory-change)))))
2483
2484
fb7933a3
KG
2485;;; File Name Handler Functions:
2486
fb7933a3
KG
2487(defun tramp-handle-make-symbolic-link
2488 (filename linkname &optional ok-if-already-exists)
00d6fd04 2489 "Like `make-symbolic-link' for Tramp files.
cebb4ec6 2490If LINKNAME is a non-Tramp file, it is used verbatim as the target of
7432277c 2491the symlink. If LINKNAME is a Tramp file, only the localname component is
cebb4ec6
KG
2492used as the target of the symlink.
2493
7432277c
KG
2494If LINKNAME is a Tramp file and the localname component is relative, then
2495it is expanded first, before the localname component is taken. Note that
cebb4ec6
KG
2496this can give surprising results if the user/host for the source and
2497target of the symlink differ."
c62c9d08 2498 (with-parsed-tramp-file-name linkname l
00d6fd04 2499 (let ((ln (tramp-get-remote-ln l))
87bdd2c7
MA
2500 (cwd (tramp-run-real-handler
2501 'file-name-directory (list l-localname))))
c62c9d08 2502 (unless ln
00d6fd04
MA
2503 (tramp-error
2504 l 'file-error
2505 "Making a symbolic link. ln(1) does not exist on the remote host."))
c62c9d08
KG
2506
2507 ;; Do the 'confirm if exists' thing.
cebb4ec6 2508 (when (file-exists-p linkname)
c62c9d08
KG
2509 ;; What to do?
2510 (if (or (null ok-if-already-exists) ; not allowed to exist
2511 (and (numberp ok-if-already-exists)
2512 (not (yes-or-no-p
2513 (format
2514 "File %s already exists; make it a link anyway? "
7432277c 2515 l-localname)))))
00d6fd04
MA
2516 (tramp-error
2517 l 'file-already-exists "File %s already exists" l-localname)
66bdc868 2518 (tramp-compat-delete-file linkname 'force)))
cebb4ec6 2519
7432277c 2520 ;; If FILENAME is a Tramp name, use just the localname component.
cebb4ec6 2521 (when (tramp-tramp-file-p filename)
1834b39f
MA
2522 (setq filename
2523 (tramp-file-name-localname
2524 (tramp-dissect-file-name (expand-file-name filename)))))
bf247b6e 2525
c62c9d08
KG
2526 ;; Right, they are on the same host, regardless of user, method, etc.
2527 ;; We now make the link on the remote machine. This will occur as the user
2528 ;; that FILENAME belongs to.
2529 (zerop
2530 (tramp-send-command-and-check
b593f105
MA
2531 l
2532 (format
2533 "cd %s && %s -sf %s %s"
2534 (tramp-shell-quote-argument cwd)
2535 ln
2536 (tramp-shell-quote-argument filename)
2537 (tramp-shell-quote-argument l-localname))
2538 t)))))
fb7933a3 2539
fb7933a3 2540(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
00d6fd04
MA
2541 "Like `load' for Tramp files."
2542 (with-parsed-tramp-file-name (expand-file-name file) nil
c62c9d08
KG
2543 (unless nosuffix
2544 (cond ((file-exists-p (concat file ".elc"))
2545 (setq file (concat file ".elc")))
2546 ((file-exists-p (concat file ".el"))
2547 (setq file (concat file ".el")))))
2548 (when must-suffix
2549 ;; The first condition is always true for absolute file names.
2550 ;; Included for safety's sake.
2551 (unless (or (file-name-directory file)
2552 (string-match "\\.elc?\\'" file))
00d6fd04
MA
2553 (tramp-error
2554 v 'file-error
2555 "File `%s' does not include a `.el' or `.elc' suffix" file)))
c62c9d08
KG
2556 (unless noerror
2557 (when (not (file-exists-p file))
00d6fd04 2558 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
c62c9d08
KG
2559 (if (not (file-exists-p file))
2560 nil
00d6fd04 2561 (unless nomessage (tramp-message v 0 "Loading %s..." file))
c62c9d08
KG
2562 (let ((local-copy (file-local-copy file)))
2563 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
ce2cc728
MA
2564 (unwind-protect
2565 (load local-copy noerror t t)
66bdc868 2566 (tramp-compat-delete-file local-copy 'force)))
00d6fd04 2567 (unless nomessage (tramp-message v 0 "Loading %s...done" file))
c62c9d08 2568 t)))
fb7933a3 2569
a4aeb9a4 2570;; Localname manipulation functions that grok Tramp localnames...
c0fc6170
MA
2571(defun tramp-handle-file-name-as-directory (file)
2572 "Like `file-name-as-directory' but aware of Tramp files."
2573 ;; `file-name-as-directory' would be sufficient except localname is
2574 ;; the empty string.
2575 (let ((v (tramp-dissect-file-name file t)))
2576 ;; Run the command on the localname portion only.
2577 (tramp-make-tramp-file-name
2578 (tramp-file-name-method v)
2579 (tramp-file-name-user v)
2580 (tramp-file-name-host v)
2581 (tramp-run-real-handler
2582 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2583
fb7933a3 2584(defun tramp-handle-file-name-directory (file)
00d6fd04 2585 "Like `file-name-directory' but aware of Tramp files."
9ce8462a
MA
2586 ;; Everything except the last filename thing is the directory. We
2587 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2588 ;; the remote file name parts. This is a problem when we are in
2589 ;; file name completion.
2590 (let ((v (tramp-dissect-file-name file t)))
a01b1e22
MA
2591 ;; Run the command on the localname portion only.
2592 (tramp-make-tramp-file-name
9ce8462a
MA
2593 (tramp-file-name-method v)
2594 (tramp-file-name-user v)
2595 (tramp-file-name-host v)
87bdd2c7
MA
2596 (tramp-run-real-handler
2597 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
fb7933a3
KG
2598
2599(defun tramp-handle-file-name-nondirectory (file)
00d6fd04 2600 "Like `file-name-nondirectory' but aware of Tramp files."
c62c9d08 2601 (with-parsed-tramp-file-name file nil
87bdd2c7 2602 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
fb7933a3
KG
2603
2604(defun tramp-handle-file-truename (filename &optional counter prev-dirs)
00d6fd04 2605 "Like `file-truename' for Tramp files."
48ddd622 2606 (with-parsed-tramp-file-name (expand-file-name filename) nil
00d6fd04 2607 (with-file-property v localname "file-truename"
293c24f9 2608 (let ((result nil)) ; result steps in reverse order
00d6fd04 2609 (tramp-message v 4 "Finding true name for `%s'" filename)
293c24f9
MA
2610 (cond
2611 ;; Use GNU readlink --canonicalize-missing where available.
2612 ((tramp-get-remote-readlink v)
2613 (setq result
2614 (tramp-send-command-and-read
2615 v
2616 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2617 (tramp-get-remote-readlink v)
2618 (tramp-shell-quote-argument localname)))))
2619
2620 ;; Use Perl implementation.
2621 ((and (tramp-get-remote-perl v)
2622 (tramp-get-connection-property v "perl-file-spec" nil)
2623 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2624 (tramp-maybe-send-script
2625 v tramp-perl-file-truename "tramp_perl_file_truename")
2626 (setq result
2627 (tramp-send-command-and-read
2628 v
2629 (format "tramp_perl_file_truename %s"
2630 (tramp-shell-quote-argument localname)))))
2631
2632 ;; Do it yourself. We bind `directory-sep-char' here for
2633 ;; XEmacs on Windows, which would otherwise use backslash.
2634 (t (let* ((directory-sep-char ?/)
2635 (steps (tramp-compat-split-string localname "/"))
2636 (localnamedir (tramp-run-real-handler
2637 'file-name-as-directory (list localname)))
2638 (is-dir (string= localname localnamedir))
2639 (thisstep nil)
2640 (numchase 0)
2641 ;; Don't make the following value larger than
2642 ;; necessary. People expect an error message in a
2643 ;; timely fashion when something is wrong;
2644 ;; otherwise they might think that Emacs is hung.
2645 ;; Of course, correctness has to come first.
2646 (numchase-limit 20)
2647 symlink-target)
2648 (while (and steps (< numchase numchase-limit))
2649 (setq thisstep (pop steps))
2650 (tramp-message
2651 v 5 "Check %s"
2652 (mapconcat 'identity
2653 (append '("") (reverse result) (list thisstep))
2654 "/"))
2655 (setq symlink-target
2656 (nth 0 (file-attributes
2657 (tramp-make-tramp-file-name
2658 method user host
2659 (mapconcat 'identity
2660 (append '("")
2661 (reverse result)
2662 (list thisstep))
2663 "/")))))
2664 (cond ((string= "." thisstep)
2665 (tramp-message v 5 "Ignoring step `.'"))
2666 ((string= ".." thisstep)
2667 (tramp-message v 5 "Processing step `..'")
2668 (pop result))
2669 ((stringp symlink-target)
2670 ;; It's a symlink, follow it.
2671 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2672 (setq numchase (1+ numchase))
2673 (when (file-name-absolute-p symlink-target)
2674 (setq result nil))
2675 ;; If the symlink was absolute, we'll get a string like
2676 ;; "/user@host:/some/target"; extract the
2677 ;; "/some/target" part from it.
2678 (when (tramp-tramp-file-p symlink-target)
2679 (unless (tramp-equal-remote filename symlink-target)
2680 (tramp-error
2681 v 'file-error
2682 "Symlink target `%s' on wrong host" symlink-target))
2683 (setq symlink-target localname))
2684 (setq steps
2685 (append (tramp-compat-split-string
2686 symlink-target "/")
2687 steps)))
2688 (t
2689 ;; It's a file.
2690 (setq result (cons thisstep result)))))
2691 (when (>= numchase numchase-limit)
2692 (tramp-error
2693 v 'file-error
2694 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2695 (setq result (reverse result))
2696 ;; Combine list to form string.
2697 (setq result
2698 (if result
2699 (mapconcat 'identity (cons "" result) "/")
00d6fd04 2700 "/"))
293c24f9
MA
2701 (when (and is-dir (or (string= "" result)
2702 (not (string= (substring result -1) "/"))))
2703 (setq result (concat result "/"))))))
2704
2705 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2706 (tramp-make-tramp-file-name method user host result)))))
fb7933a3
KG
2707
2708;; Basic functions.
2709
2710(defun tramp-handle-file-exists-p (filename)
00d6fd04 2711 "Like `file-exists-p' for Tramp files."
c62c9d08 2712 (with-parsed-tramp-file-name filename nil
00d6fd04 2713 (with-file-property v localname "file-exists-p"
293c24f9
MA
2714 (or (not (null (tramp-get-file-property
2715 v localname "file-attributes-integer" nil)))
2716 (not (null (tramp-get-file-property
2717 v localname "file-attributes-string" nil)))
2718 (zerop (tramp-send-command-and-check
2719 v
2720 (format
2721 "%s %s"
2722 (tramp-get-file-exists-command v)
2723 (tramp-shell-quote-argument localname))))))))
fb7933a3 2724
00d6fd04
MA
2725;; Inodes don't exist for some file systems. Therefore we must
2726;; generate virtual ones. Used in `find-buffer-visiting'. The method
2727;; applied might be not so efficient (Ange-FTP uses hashes). But
2728;; performance isn't the major issue given that file transfer will
2729;; take time.
2730(defvar tramp-inodes nil
2731 "Keeps virtual inodes numbers.")
2732
8daea7fc
KG
2733;; Devices must distinguish physical file systems. The device numbers
2734;; provided by "lstat" aren't unique, because we operate on different hosts.
2735;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2736;; EFS use device number "-1". In order to be different, we use device number
b946a456 2737;; (-1 . x), whereby "x" is unique for a given (method user host).
8daea7fc
KG
2738(defvar tramp-devices nil
2739 "Keeps virtual device numbers.")
2740
fb7933a3
KG
2741;; CCC: This should check for an error condition and signal failure
2742;; when something goes wrong.
2743;; Daniel Pittman <daniel@danann.net>
c951aecb 2744(defun tramp-handle-file-attributes (filename &optional id-format)
00d6fd04
MA
2745 "Like `file-attributes' for Tramp files."
2746 (unless id-format (setq id-format 'integer))
aa485f7c
MA
2747 ;; Don't modify `last-coding-system-used' by accident.
2748 (let ((last-coding-system-used last-coding-system-used))
2749 (with-parsed-tramp-file-name (expand-file-name filename) nil
2750 (with-file-property v localname (format "file-attributes-%s" id-format)
7f49fe46
MA
2751 (save-excursion
2752 (tramp-convert-file-attributes
2753 v
2754 (cond
2755 ((tramp-get-remote-stat v)
2756 (tramp-do-file-attributes-with-stat v localname id-format))
2757 ((tramp-get-remote-perl v)
2758 (tramp-do-file-attributes-with-perl v localname id-format))
2759 (t
2760 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2761
2762(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
00d6fd04 2763 "Implement `file-attributes' for Tramp files using the ls(1) command."
fb7933a3
KG
2764 (let (symlinkp dirp
2765 res-inode res-filemodes res-numlinks
2766 res-uid res-gid res-size res-symlink-target)
00d6fd04 2767 (tramp-message vec 5 "file attributes with ls: %s" localname)
fb7933a3 2768 (tramp-send-command
00d6fd04 2769 vec
680db9ac
MA
2770 (format "(%s %s || %s -h %s) && %s %s %s"
2771 (tramp-get-file-exists-command vec)
2772 (tramp-shell-quote-argument localname)
2773 (tramp-get-test-command vec)
2774 (tramp-shell-quote-argument localname)
00d6fd04 2775 (tramp-get-ls-command vec)
c82c5727 2776 (if (eq id-format 'integer) "-ildn" "-ild")
7432277c 2777 (tramp-shell-quote-argument localname)))
fb7933a3 2778 ;; parse `ls -l' output ...
00d6fd04 2779 (with-current-buffer (tramp-get-buffer vec)
680db9ac
MA
2780 (when (> (buffer-size) 0)
2781 (goto-char (point-min))
2782 ;; ... inode
2783 (setq res-inode
2784 (condition-case err
2785 (read (current-buffer))
2786 (invalid-read-syntax
2787 (when (and (equal (cadr err)
2788 "Integer constant overflow in reader")
2789 (string-match
2790 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2791 (car (cddr err))))
2792 (let* ((big (read (substring (car (cddr err)) 0
2793 (match-beginning 1))))
2794 (small (read (match-string 1 (car (cddr err)))))
2795 (twiddle (/ small 65536)))
2796 (cons (+ big twiddle)
2797 (- small (* twiddle 65536))))))))
2798 ;; ... file mode flags
2799 (setq res-filemodes (symbol-name (read (current-buffer))))
2800 ;; ... number links
2801 (setq res-numlinks (read (current-buffer)))
2802 ;; ... uid and gid
2803 (setq res-uid (read (current-buffer)))
2804 (setq res-gid (read (current-buffer)))
2805 (if (eq id-format 'integer)
2806 (progn
2807 (unless (numberp res-uid) (setq res-uid -1))
2808 (unless (numberp res-gid) (setq res-gid -1)))
2809 (progn
2810 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
2811 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
2812 ;; ... size
2813 (setq res-size (read (current-buffer)))
2814 ;; From the file modes, figure out other stuff.
2815 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2816 (setq dirp (eq ?d (aref res-filemodes 0)))
2817 ;; if symlink, find out file name pointed to
2818 (when symlinkp
2819 (search-forward "-> ")
2820 (setq res-symlink-target
2821 (buffer-substring (point) (tramp-compat-line-end-position))))
2822 ;; return data gathered
2823 (list
2824 ;; 0. t for directory, string (name linked to) for symbolic
2825 ;; link, or nil.
2826 (or dirp res-symlink-target)
2827 ;; 1. Number of links to file.
2828 res-numlinks
2829 ;; 2. File uid.
2830 res-uid
2831 ;; 3. File gid.
2832 res-gid
2833 ;; 4. Last access time, as a list of two integers. First
2834 ;; integer has high-order 16 bits of time, second has low 16
2835 ;; bits.
2836 ;; 5. Last modification time, likewise.
2837 ;; 6. Last status change time, likewise.
2838 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2839 ;; 7. Size in bytes (-1, if number is out of range).
2840 res-size
2841 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2842 res-filemodes
2843 ;; 9. t if file's gid would change if file were deleted and
2844 ;; recreated. Will be set in `tramp-convert-file-attributes'
2845 t
2846 ;; 10. inode number.
2847 res-inode
2848 ;; 11. Device number. Will be replaced by a virtual device number.
2849 -1
2850 )))))
fb7933a3 2851
7f49fe46 2852(defun tramp-do-file-attributes-with-perl
00d6fd04
MA
2853 (vec localname &optional id-format)
2854 "Implement `file-attributes' for Tramp files using a Perl script."
2855 (tramp-message vec 5 "file attributes with perl: %s" localname)
2856 (tramp-maybe-send-script
2857 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
2858 (tramp-send-command-and-read
2859 vec
2860 (format "tramp_perl_file_attributes %s %s"
2861 (tramp-shell-quote-argument localname) id-format)))
2862
7f49fe46 2863(defun tramp-do-file-attributes-with-stat
00d6fd04
MA
2864 (vec localname &optional id-format)
2865 "Implement `file-attributes' for Tramp files using stat(1) command."
2866 (tramp-message vec 5 "file attributes with stat: %s" localname)
2867 (tramp-send-command-and-read
2868 vec
2869 (format
94ecf5da
MA
2870 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
2871 ;; parse correctly the sequence "((". Therefore, we add a space.
2872 "( (%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
2873 (tramp-get-file-exists-command vec)
2874 (tramp-shell-quote-argument localname)
2875 (tramp-get-test-command vec)
2876 (tramp-shell-quote-argument localname)
00d6fd04
MA
2877 (tramp-get-remote-stat vec)
2878 (if (eq id-format 'integer) "%u" "\"%U\"")
2879 (if (eq id-format 'integer) "%g" "\"%G\"")
2880 (tramp-shell-quote-argument localname))))
8daea7fc 2881
fb7933a3 2882(defun tramp-handle-set-visited-file-modtime (&optional time-list)
00d6fd04 2883 "Like `set-visited-file-modtime' for Tramp files."
fb7933a3
KG
2884 (unless (buffer-file-name)
2885 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2886 (buffer-name)))
48ddd622
MA
2887 (if time-list
2888 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
11948172
MA
2889 (let ((f (buffer-file-name))
2890 coding-system-used)
48ddd622
MA
2891 (with-parsed-tramp-file-name f nil
2892 (let* ((attr (file-attributes f))
2893 ;; '(-1 65535) means file doesn't exists yet.
2894 (modtime (or (nth 5 attr) '(-1 65535))))
11948172
MA
2895 (when (boundp 'last-coding-system-used)
2896 (setq coding-system-used (symbol-value 'last-coding-system-used)))
48ddd622 2897 ;; We use '(0 0) as a don't-know value. See also
7f49fe46 2898 ;; `tramp-do-file-attributes-with-ls'.
48ddd622
MA
2899 (if (not (equal modtime '(0 0)))
2900 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
00d6fd04 2901 (progn
48ddd622 2902 (tramp-send-command
00d6fd04 2903 v
48ddd622 2904 (format "%s -ild %s"
00d6fd04 2905 (tramp-get-ls-command v)
48ddd622 2906 (tramp-shell-quote-argument localname)))
48ddd622
MA
2907 (setq attr (buffer-substring (point)
2908 (progn (end-of-line) (point)))))
00d6fd04
MA
2909 (tramp-set-file-property
2910 v localname "visited-file-modtime-ild" attr))
11948172
MA
2911 (when (boundp 'last-coding-system-used)
2912 (set 'last-coding-system-used coding-system-used))
d2a2c17f 2913 nil)))))
fb7933a3 2914
c62c9d08
KG
2915;; This function makes the same assumption as
2916;; `tramp-handle-set-visited-file-modtime'.
2917(defun tramp-handle-verify-visited-file-modtime (buf)
00d6fd04 2918 "Like `verify-visited-file-modtime' for Tramp files.
c08e6004
MA
2919At the time `verify-visited-file-modtime' calls this function, we
2920already know that the buffer is visiting a file and that
2921`visited-file-modtime' does not return 0. Do not call this
2922function directly, unless those two cases are already taken care
2923of."
c62c9d08 2924 (with-current-buffer buf
94ecf5da
MA
2925 (let ((f (buffer-file-name)))
2926 ;; There is no file visiting the buffer, or the buffer has no
2927 ;; recorded last modification time, or there is no established
2928 ;; connection.
2929 (if (or (not f)
2930 (eq (visited-file-modtime) 0)
2931 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
2932 t
b15d0c4c 2933 (with-parsed-tramp-file-name f nil
bce04fee 2934 (tramp-flush-file-property v localname)
b15d0c4c
MA
2935 (let* ((attr (file-attributes f))
2936 (modtime (nth 5 attr))
2937 (mt (visited-file-modtime)))
bf247b6e 2938
70c11b0b
MA
2939 (cond
2940 ;; File exists, and has a known modtime.
b15d0c4c
MA
2941 ((and attr (not (equal modtime '(0 0))))
2942 (< (abs (tramp-time-diff
2943 modtime
2944 ;; For compatibility, deal with both the old
70c11b0b
MA
2945 ;; (HIGH . LOW) and the new (HIGH LOW) return
2946 ;; values of `visited-file-modtime'.
b15d0c4c
MA
2947 (if (atom (cdr mt))
2948 (list (car mt) (cdr mt))
2949 mt)))
2950 2))
70c11b0b 2951 ;; Modtime has the don't know value.
b15d0c4c 2952 (attr
00d6fd04
MA
2953 (tramp-send-command
2954 v
2955 (format "%s -ild %s"
2956 (tramp-get-ls-command v)
2957 (tramp-shell-quote-argument localname)))
2958 (with-current-buffer (tramp-get-buffer v)
b15d0c4c
MA
2959 (setq attr (buffer-substring
2960 (point) (progn (end-of-line) (point)))))
00d6fd04
MA
2961 (equal
2962 attr
2963 (tramp-get-file-property
2964 v localname "visited-file-modtime-ild" "")))
70c11b0b
MA
2965 ;; If file does not exist, say it is not modified if and
2966 ;; only if that agrees with the buffer's record.
b15d0c4c 2967 (t (equal mt '(-1 65535))))))))))
c62c9d08 2968
fb7933a3 2969(defun tramp-handle-set-file-modes (filename mode)
00d6fd04 2970 "Like `set-file-modes' for Tramp files."
c62c9d08 2971 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
2972 (tramp-flush-file-property v localname)
2973 (unless (zerop (tramp-send-command-and-check
2974 v
2975 (format "chmod %s %s"
2976 (tramp-decimal-to-octal mode)
2977 (tramp-shell-quote-argument localname))))
2978 ;; FIXME: extract the proper text from chmod's stderr.
2979 (tramp-error
2980 v 'file-error "Error while changing file's mode %s" filename))))
fb7933a3 2981
ce3f516f
MA
2982(defun tramp-handle-set-file-times (filename &optional time)
2983 "Like `set-file-times' for Tramp files."
2984 (zerop
9e6ab520 2985 (if (file-remote-p filename)
ce3f516f 2986 (with-parsed-tramp-file-name filename nil
8d60099b 2987 (tramp-flush-file-property v localname)
ce3f516f
MA
2988 (let ((time (if (or (null time) (equal time '(0 0)))
2989 (current-time)
2990 time))
94ecf5da
MA
2991 ;; With GNU Emacs, `format-time-string' has an optional
2992 ;; parameter UNIVERSAL. This is preferred, because we
2993 ;; could handle the case when the remote host is
2994 ;; located in a different time zone as the local host.
2995 (utc (not (featurep 'xemacs))))
ce3f516f
MA
2996 (tramp-send-command-and-check
2997 v (format "%s touch -t %s %s"
2998 (if utc "TZ=UTC; export TZ;" "")
2999 (if utc
3000 (format-time-string "%Y%m%d%H%M.%S" time t)
3001 (format-time-string "%Y%m%d%H%M.%S" time))
3002 (tramp-shell-quote-argument localname)))))
8d60099b 3003
ce3f516f
MA
3004 ;; We handle also the local part, because in older Emacsen,
3005 ;; without `set-file-times', this function is an alias for this.
3006 ;; We are local, so we don't need the UTC settings.
a4aeb9a4 3007 (tramp-local-call-process
ce3f516f
MA
3008 "touch" nil nil nil "-t"
3009 (format-time-string "%Y%m%d%H%M.%S" time)
3010 (tramp-shell-quote-argument filename)))))
3011
8d60099b
MA
3012(defun tramp-set-file-uid-gid (filename &optional uid gid)
3013 "Set the ownership for FILENAME.
3014If UID and GID are provided, these values are used; otherwise uid
3015and gid of the corresponding user is taken. Both parameters must be integers."
70c11b0b
MA
3016 ;; Modern Unices allow chown only for root. So we might need
3017 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3018 ;; working with su(do)? when it is needed, so it shall succeed in
3019 ;; the majority of cases.
aa485f7c
MA
3020 ;; Don't modify `last-coding-system-used' by accident.
3021 (let ((last-coding-system-used last-coding-system-used))
3022 (if (file-remote-p filename)
3023 (with-parsed-tramp-file-name filename nil
3024 (if (and (zerop (user-uid)) (tramp-local-host-p v))
3025 ;; If we are root on the local host, we can do it directly.
3026 (tramp-set-file-uid-gid localname uid gid)
3027 (let ((uid (or (and (integerp uid) uid)
3028 (tramp-get-remote-uid v 'integer)))
3029 (gid (or (and (integerp gid) gid)
3030 (tramp-get-remote-gid v 'integer))))
3031 (tramp-send-command
3032 v (format
3033 "chown %d:%d %s" uid gid
3034 (tramp-shell-quote-argument localname))))))
3035
3036 ;; We handle also the local part, because there doesn't exist
3037 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3038 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
3039 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
3040 (tramp-local-call-process
3041 "chown" nil nil nil
3042 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
8d60099b 3043
7ba94701
MA
3044(defun tramp-remote-selinux-p (vec)
3045 "Check, whether SELINUX is enabled on the remote host."
3046 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
3047 (let ((result (tramp-find-executable
3048 vec "getenforce" (tramp-get-remote-path vec) t t)))
3049 (and result
3050 (string-equal
3051 (tramp-send-command-and-read
3052 vec (format "echo \\\"`%S`\\\"" result))
3053 "Enforcing")))))
3054
632c5478
MA
3055(defun tramp-handle-file-selinux-context (filename)
3056 "Like `file-selinux-context' for Tramp files."
3057 (with-parsed-tramp-file-name filename nil
3058 (with-file-property v localname "file-selinux-context"
3059 (let ((context '(nil nil nil nil))
3060 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3061 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
7ba94701
MA
3062 (when (and (tramp-remote-selinux-p v)
3063 (zerop (tramp-send-command-and-check
3064 v (format
3065 "%s -d -Z %s"
3066 (tramp-get-ls-command v)
3067 (tramp-shell-quote-argument localname)))))
632c5478
MA
3068 (with-current-buffer (tramp-get-connection-buffer v)
3069 (goto-char (point-min))
3070 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
3071 (setq context (list (match-string 1) (match-string 2)
3072 (match-string 3) (match-string 4))))))
3073 ;; Return the context.
3074 context))))
3075
3076(defun tramp-handle-set-file-selinux-context (filename context)
3077 "Like `set-file-selinux-context' for Tramp files."
3078 (with-parsed-tramp-file-name filename nil
3079 (if (and (consp context)
7ba94701 3080 (tramp-remote-selinux-p v)
632c5478
MA
3081 (zerop (tramp-send-command-and-check
3082 v (format "chcon %s %s %s %s %s"
3083 (if (stringp (nth 0 context))
3084 (format "--user=%s" (nth 0 context)) "")
3085 (if (stringp (nth 1 context))
3086 (format "--role=%s" (nth 1 context)) "")
3087 (if (stringp (nth 2 context))
3088 (format "--type=%s" (nth 2 context)) "")
3089 (if (stringp (nth 3 context))
3090 (format "--range=%s" (nth 3 context)) "")
3091 (tramp-shell-quote-argument localname)))))
3092 (tramp-set-file-property v localname "file-selinux-context" context)
3093 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
3094 ;; We always return nil.
3095 nil)
3096
fb7933a3
KG
3097;; Simple functions using the `test' command.
3098
3099(defun tramp-handle-file-executable-p (filename)
00d6fd04 3100 "Like `file-executable-p' for Tramp files."
c62c9d08 3101 (with-parsed-tramp-file-name filename nil
00d6fd04 3102 (with-file-property v localname "file-executable-p"
293c24f9
MA
3103 ;; Examine `file-attributes' cache to see if request can be
3104 ;; satisfied without remote operation.
3105 (or (tramp-check-cached-permissions v ?x)
3106 (zerop (tramp-run-test "-x" filename))))))
fb7933a3
KG
3107
3108(defun tramp-handle-file-readable-p (filename)
00d6fd04 3109 "Like `file-readable-p' for Tramp files."
c62c9d08 3110 (with-parsed-tramp-file-name filename nil
00d6fd04 3111 (with-file-property v localname "file-readable-p"
293c24f9
MA
3112 ;; Examine `file-attributes' cache to see if request can be
3113 ;; satisfied without remote operation.
3114 (or (tramp-check-cached-permissions v ?r)
3115 (zerop (tramp-run-test "-r" filename))))))
fb7933a3
KG
3116
3117;; When the remote shell is started, it looks for a shell which groks
3118;; tilde expansion. Here, we assume that all shells which grok tilde
3119;; expansion will also provide a `test' command which groks `-nt' (for
3120;; newer than). If this breaks, tell me about it and I'll try to do
3121;; something smarter about it.
3122(defun tramp-handle-file-newer-than-file-p (file1 file2)
00d6fd04 3123 "Like `file-newer-than-file-p' for Tramp files."
fb7933a3
KG
3124 (cond ((not (file-exists-p file1))
3125 nil)
3126 ((not (file-exists-p file2))
3127 t)
91879624 3128 ;; We are sure both files exist at this point.
fb7933a3
KG
3129 (t
3130 (save-excursion
91879624
KG
3131 ;; We try to get the mtime of both files. If they are not
3132 ;; equal to the "dont-know" value, then we subtract the times
3133 ;; and obtain the result.
3134 (let ((fa1 (file-attributes file1))
3135 (fa2 (file-attributes file2)))
3136 (if (and (not (equal (nth 5 fa1) '(0 0)))
3137 (not (equal (nth 5 fa2) '(0 0))))
01917a18 3138 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
91879624
KG
3139 ;; If one of them is the dont-know value, then we can
3140 ;; still try to run a shell command on the remote host.
3141 ;; However, this only works if both files are Tramp
3142 ;; files and both have the same method, same user, same
3143 ;; host.
00d6fd04
MA
3144 (unless (tramp-equal-remote file1 file2)
3145 (with-parsed-tramp-file-name
3146 (if (tramp-tramp-file-p file1) file1 file2) nil
3147 (tramp-error
3148 v 'file-error
3149 "Files %s and %s must have same method, user, host"
3150 file1 file2)))
3151 (with-parsed-tramp-file-name file1 nil
3152 (zerop (tramp-run-test2
3153 (tramp-get-test-nt-command v) file1 file2)))))))))
fb7933a3
KG
3154
3155;; Functions implemented using the basic functions above.
3156
3157(defun tramp-handle-file-modes (filename)
00d6fd04 3158 "Like `file-modes' for Tramp files."
5da24108
MA
3159 (let ((truename (or (file-truename filename) filename)))
3160 (when (file-exists-p truename)
3161 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
fb7933a3 3162
b86c1cd8
MA
3163(defun tramp-default-file-modes (filename)
3164 "Return file modes of FILENAME as integer.
3165If the file modes of FILENAME cannot be determined, return the
974647ac
MA
3166value of `default-file-modes', without execute permissions."
3167 (or (file-modes filename)
3168 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
b86c1cd8 3169
fb7933a3 3170(defun tramp-handle-file-directory-p (filename)
00d6fd04 3171 "Like `file-directory-p' for Tramp files."
fb7933a3
KG
3172 ;; Care must be taken that this function returns `t' for symlinks
3173 ;; pointing to directories. Surely the most obvious implementation
3174 ;; would be `test -d', but that returns false for such symlinks.
3175 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3176 ;; I now think he's right. So we could be using `test -d', couldn't
3177 ;; we?
3178 ;;
3179 ;; Alternatives: `cd %s', `test -d %s'
c62c9d08 3180 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3181 (with-file-property v localname "file-directory-p"
3182 (zerop (tramp-run-test "-d" filename)))))
fb7933a3
KG
3183
3184(defun tramp-handle-file-regular-p (filename)
00d6fd04
MA
3185 "Like `file-regular-p' for Tramp files."
3186 (and (file-exists-p filename)
3187 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
fb7933a3
KG
3188
3189(defun tramp-handle-file-symlink-p (filename)
00d6fd04 3190 "Like `file-symlink-p' for Tramp files."
c62c9d08 3191 (with-parsed-tramp-file-name filename nil
c951aecb 3192 (let ((x (car (file-attributes filename))))
b25a52cc
KG
3193 (when (stringp x)
3194 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3195 ;; might do weird things.
3196 (if (file-name-absolute-p x)
00d6fd04 3197 (tramp-make-tramp-file-name method user host x)
b25a52cc 3198 x)))))
fb7933a3
KG
3199
3200(defun tramp-handle-file-writable-p (filename)
00d6fd04 3201 "Like `file-writable-p' for Tramp files."
c62c9d08 3202 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3203 (with-file-property v localname "file-writable-p"
3204 (if (file-exists-p filename)
293c24f9
MA
3205 ;; Examine `file-attributes' cache to see if request can be
3206 ;; satisfied without remote operation.
3207 (or (tramp-check-cached-permissions v ?w)
3208 (zerop (tramp-run-test "-w" filename)))
00d6fd04
MA
3209 ;; If file doesn't exist, check if directory is writable.
3210 (and (zerop (tramp-run-test
3211 "-d" (file-name-directory filename)))
3212 (zerop (tramp-run-test
3213 "-w" (file-name-directory filename))))))))
fb7933a3
KG
3214
3215(defun tramp-handle-file-ownership-preserved-p (filename)
00d6fd04 3216 "Like `file-ownership-preserved-p' for Tramp files."
c62c9d08 3217 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
3218 (with-file-property v localname "file-ownership-preserved-p"
3219 (let ((attributes (file-attributes filename)))
3220 ;; Return t if the file doesn't exist, since it's true that no
3221 ;; information would be lost by an (attempted) delete and create.
3222 (or (null attributes)
3223 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
fb7933a3
KG
3224
3225;; Other file name ops.
3226
fb7933a3 3227(defun tramp-handle-directory-file-name (directory)
00d6fd04 3228 "Like `directory-file-name' for Tramp files."
7432277c
KG
3229 ;; If localname component of filename is "/", leave it unchanged.
3230 ;; Otherwise, remove any trailing slash from localname component.
8daea7fc
KG
3231 ;; Method, host, etc, are unchanged. Does it make sense to try
3232 ;; to avoid parsing the filename?
c62c9d08 3233 (with-parsed-tramp-file-name directory nil
7432277c
KG
3234 (if (and (not (zerop (length localname)))
3235 (eq (aref localname (1- (length localname))) ?/)
3236 (not (string= localname "/")))
8daea7fc
KG
3237 (substring directory 0 -1)
3238 directory)))
fb7933a3
KG
3239
3240;; Directory listings.
3241
00d6fd04
MA
3242(defun tramp-handle-directory-files
3243 (directory &optional full match nosort files-only)
3244 "Like `directory-files' for Tramp files."
3245 ;; FILES-ONLY is valid for XEmacs only.
3246 (when (file-directory-p directory)
73a37a69 3247 (setq directory (file-name-as-directory (expand-file-name directory)))
00d6fd04
MA
3248 (let ((temp (nreverse (file-name-all-completions "" directory)))
3249 result item)
3250
3251 (while temp
3252 (setq item (directory-file-name (pop temp)))
3253 (when (and (or (null match) (string-match match item))
3254 (or (null files-only)
73a37a69 3255 ;; Files only.
00d6fd04 3256 (and (equal files-only t) (file-regular-p item))
73a37a69 3257 ;; Directories only.
00d6fd04 3258 (file-directory-p item)))
73a37a69 3259 (push (if full (concat directory item) item)
00d6fd04 3260 result)))
73a37a69 3261 (if nosort result (sort result 'string<)))))
c62c9d08 3262
c82c5727
LH
3263(defun tramp-handle-directory-files-and-attributes
3264 (directory &optional full match nosort id-format)
00d6fd04
MA
3265 "Like `directory-files-and-attributes' for Tramp files."
3266 (unless id-format (setq id-format 'integer))
3267 (when (file-directory-p directory)
3268 (setq directory (expand-file-name directory))
3269 (let* ((temp
b533bc97 3270 (copy-tree
00d6fd04
MA
3271 (with-parsed-tramp-file-name directory nil
3272 (with-file-property
3273 v localname
3274 (format "directory-files-and-attributes-%s" id-format)
3275 (save-excursion
3276 (mapcar
aa485f7c
MA
3277 (lambda (x)
3278 (cons (car x)
3279 (tramp-convert-file-attributes v (cdr x))))
7f49fe46
MA
3280 (cond
3281 ((tramp-get-remote-stat v)
3282 (tramp-do-directory-files-and-attributes-with-stat
3283 v localname id-format))
3284 ((tramp-get-remote-perl v)
3285 (tramp-do-directory-files-and-attributes-with-perl
3286 v localname id-format)))))))))
00d6fd04
MA
3287 result item)
3288
3289 (while temp
3290 (setq item (pop temp))
3291 (when (or (null match) (string-match match (car item)))
3292 (when full
3293 (setcar item (expand-file-name (car item) directory)))
3294 (push item result)))
3295
3296 (if nosort
3297 result
3298 (sort result (lambda (x y) (string< (car x) (car y))))))))
3299
7f49fe46 3300(defun tramp-do-directory-files-and-attributes-with-perl
00d6fd04
MA
3301 (vec localname &optional id-format)
3302 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3303 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3304 (tramp-maybe-send-script
3305 vec tramp-perl-directory-files-and-attributes
3306 "tramp_perl_directory_files_and_attributes")
3307 (let ((object
3308 (tramp-send-command-and-read
3309 vec
3310 (format "tramp_perl_directory_files_and_attributes %s %s"
3311 (tramp-shell-quote-argument localname) id-format))))
3312 (when (stringp object) (tramp-error vec 'file-error object))
3313 object))
3314
7f49fe46 3315(defun tramp-do-directory-files-and-attributes-with-stat
00d6fd04
MA
3316 (vec localname &optional id-format)
3317 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3318 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3319 (tramp-send-command-and-read
3320 vec
3321 (format
3322 (concat
70c11b0b
MA
3323 ;; We must care about filenames with spaces, or starting with
3324 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3325 ;; but it does not work on all remote systems. Therefore, we
3326 ;; quote the filenames via sed.
3327 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
d4443a0d 3328 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
00d6fd04
MA
3329 "echo \")\"")
3330 (tramp-shell-quote-argument localname)
3331 (tramp-get-ls-command vec)
3332 (tramp-get-remote-stat vec)
3333 (if (eq id-format 'integer) "%u" "\"%U\"")
3334 (if (eq id-format 'integer) "%g" "\"%G\""))))
c82c5727 3335
c62c9d08 3336;; This function should return "foo/" for directories and "bar" for
00d6fd04 3337;; files.
c62c9d08 3338(defun tramp-handle-file-name-all-completions (filename directory)
00d6fd04
MA
3339 "Like `file-name-all-completions' for Tramp files."
3340 (unless (save-match-data (string-match "/" filename))
9c13938d 3341 (with-parsed-tramp-file-name (expand-file-name directory) nil
b50dd0d2 3342
00d6fd04
MA
3343 (all-completions
3344 filename
3345 (mapcar
3346 'list
293c24f9
MA
3347 (or
3348 ;; Try cache first
3349 (and
3350 ;; Ignore if expired
3351 (or (not (integerp tramp-completion-reread-directory-timeout))
3352 (<= (tramp-time-diff
3353 (current-time)
3354 (tramp-get-file-property
3355 v localname "last-completion" '(0 0 0)))
3356 tramp-completion-reread-directory-timeout))
3357
3358 ;; Try cache entries for filename, filename with last
3359 ;; character removed, filename with last two characters
3360 ;; removed, ..., and finally the empty string - all
3361 ;; concatenated to the local directory name
3362
3363 ;; This is inefficient for very long filenames, pity
3364 ;; `reduce' is not available...
3365 (car
3366 (apply
3367 'append
3368 (mapcar
3369 (lambda (x)
3370 (let ((cache-hit
3371 (tramp-get-file-property
3372 v
3373 (concat localname (substring filename 0 x))
3374 "file-name-all-completions"
3375 nil)))
3376 (when cache-hit (list cache-hit))))
3377 (tramp-compat-number-sequence (length filename) 0 -1)))))
3378
3379 ;; Cache expired or no matching cache entry found so we need
3380 ;; to perform a remote operation
3381 (let (result)
3382 ;; Get a list of directories and files, including reliably
3383 ;; tagging the directories with a trailing '/'. Because I
3384 ;; rock. --daniel@danann.net
3385
3386 ;; Changed to perform `cd' in the same remote op and only
3387 ;; get entries starting with `filename'. Capture any `cd'
3388 ;; error messages. Ensure any `cd' and `echo' aliases are
3389 ;; ignored.
3390 (tramp-send-command
3391 v
3392 (if (tramp-get-remote-perl v)
3393 (progn
3394 (tramp-maybe-send-script
3395 v tramp-perl-file-name-all-completions
3396 "tramp_perl_file_name_all_completions")
3397 (format "tramp_perl_file_name_all_completions %s %s %d"
3398 (tramp-shell-quote-argument localname)
3399 (tramp-shell-quote-argument filename)
3400 (if (symbol-value
b533bc97
MA
3401 ;; `read-file-name-completion-ignore-case'
3402 ;; is introduced with Emacs 22.1.
3403 (if (boundp
3404 'read-file-name-completion-ignore-case)
3405 'read-file-name-completion-ignore-case
3406 'completion-ignore-case))
293c24f9
MA
3407 1 0)))
3408
3409 (format (concat
3410 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3411 ;; `ls' with wildcard might fail with `Argument
3412 ;; list too long' error in some corner cases; if
3413 ;; `ls' fails after `cd' succeeded, chances are
3414 ;; that's the case, so let's retry without
3415 ;; wildcard. This will return "too many" entries
3416 ;; but that isn't harmful.
3417 " || %s -a 2>/dev/null)"
3418 " | while read f; do"
3419 " if %s -d \"$f\" 2>/dev/null;"
3420 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3421 " && \\echo ok) || \\echo fail")
3422 (tramp-shell-quote-argument localname)
3423 (tramp-get-ls-command v)
3424 ;; When `filename' is empty, just `ls' without
3425 ;; filename argument is more efficient than `ls *'
3426 ;; for very large directories and might avoid the
3427 ;; `Argument list too long' error.
3428 ;;
3429 ;; With and only with wildcard, we need to add
3430 ;; `-d' to prevent `ls' from descending into
3431 ;; sub-directories.
3432 (if (zerop (length filename))
3433 "."
3434 (concat (tramp-shell-quote-argument filename) "* -d"))
3435 (tramp-get-ls-command v)
3436 (tramp-get-test-command v))))
3437
3438 ;; Now grab the output.
3439 (with-current-buffer (tramp-get-buffer v)
3440 (goto-char (point-max))
3441
3442 ;; Check result code, found in last line of output
3443 (forward-line -1)
3444 (if (looking-at "^fail$")
3445 (progn
3446 ;; Grab error message from line before last line
3447 ;; (it was put there by `cd 2>&1')
3448 (forward-line -1)
3449 (tramp-error
3450 v 'file-error
3451 "tramp-handle-file-name-all-completions: %s"
3452 (buffer-substring
3453 (point) (tramp-compat-line-end-position))))
3454 ;; For peace of mind, if buffer doesn't end in `fail'
3455 ;; then it should end in `ok'. If neither are in the
3456 ;; buffer something went seriously wrong on the remote
3457 ;; side.
3458 (unless (looking-at "^ok$")
3459 (tramp-error
3460 v 'file-error
3461 "\
3462tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3463 (tramp-shell-quote-argument localname) (buffer-string))))
3464
3465 (while (zerop (forward-line -1))
3466 (push (buffer-substring
3467 (point) (tramp-compat-line-end-position))
3468 result)))
3469
3470 ;; Because the remote op went through OK we know the
3471 ;; directory we `cd'-ed to exists
3472 (tramp-set-file-property
3473 v localname "file-exists-p" t)
3474
3475 ;; Because the remote op went through OK we know every
3476 ;; file listed by `ls' exists.
3477 (mapc (lambda (entry)
3478 (tramp-set-file-property
3479 v (concat localname entry) "file-exists-p" t))
3480 result)
3481
3482 (tramp-set-file-property
3483 v localname "last-completion" (current-time))
3484
3485 ;; Store result in the cache
3486 (tramp-set-file-property
3487 v (concat localname filename)
3488 "file-name-all-completions"
3489 result))))))))
fb7933a3 3490
e1e17cae
MA
3491(defun tramp-handle-file-name-completion
3492 (filename directory &optional predicate)
00d6fd04 3493 "Like `file-name-completion' for Tramp files."
fb7933a3
KG
3494 (unless (tramp-tramp-file-p directory)
3495 (error
3496 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3497 directory))
83e20b5c
MA
3498 (try-completion
3499 filename
3500 (mapcar 'list (file-name-all-completions filename directory))
3501 (when predicate
3502 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
fb7933a3
KG
3503
3504;; cp, mv and ln
3505
3506(defun tramp-handle-add-name-to-file
3507 (filename newname &optional ok-if-already-exists)
00d6fd04
MA
3508 "Like `add-name-to-file' for Tramp files."
3509 (unless (tramp-equal-remote filename newname)
3510 (with-parsed-tramp-file-name
3511 (if (tramp-tramp-file-p filename) filename newname) nil
3512 (tramp-error
3513 v 'file-error
3514 "add-name-to-file: %s"
3515 "only implemented for same method, same user, same host")))
c62c9d08
KG
3516 (with-parsed-tramp-file-name filename v1
3517 (with-parsed-tramp-file-name newname v2
00d6fd04 3518 (let ((ln (when v1 (tramp-get-remote-ln v1))))
c62c9d08
KG
3519 (when (and (not ok-if-already-exists)
3520 (file-exists-p newname)
3521 (not (numberp ok-if-already-exists))
3522 (y-or-n-p
3523 (format
3524 "File %s already exists; make it a new name anyway? "
3525 newname)))
00d6fd04
MA
3526 (tramp-error
3527 v2 'file-error
3528 "add-name-to-file: file %s already exists" newname))
aac0b0f2 3529 (tramp-flush-file-property v2 (file-name-directory v2-localname))
00d6fd04 3530 (tramp-flush-file-property v2 v2-localname)
c62c9d08 3531 (tramp-barf-unless-okay
00d6fd04 3532 v1
7432277c
KG
3533 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3534 (tramp-shell-quote-argument v2-localname))
c62c9d08
KG
3535 "error with add-name-to-file, see buffer `%s' for details"
3536 (buffer-name))))))
fb7933a3
KG
3537
3538(defun tramp-handle-copy-file
25f14cdb
MA
3539 (filename newname &optional ok-if-already-exists keep-date
3540 preserve-uid-gid preserve-selinux-context)
00d6fd04 3541 "Like `copy-file' for Tramp files."
fb7933a3
KG
3542 (setq filename (expand-file-name filename))
3543 (setq newname (expand-file-name newname))
9e6ab520 3544 (cond
a4aeb9a4 3545 ;; At least one file a Tramp file?
9e6ab520
MA
3546 ((or (tramp-tramp-file-p filename)
3547 (tramp-tramp-file-p newname))
3548 (tramp-do-copy-or-rename-file
632c5478
MA
3549 'copy filename newname ok-if-already-exists keep-date
3550 preserve-uid-gid preserve-selinux-context))
9e6ab520 3551 ;; Compat section.
632c5478
MA
3552 (preserve-selinux-context
3553 (tramp-run-real-handler
3554 'copy-file
3555 (list filename newname ok-if-already-exists keep-date
3556 preserve-uid-gid preserve-selinux-context)))
9e6ab520 3557 (preserve-uid-gid
fb7933a3 3558 (tramp-run-real-handler
8d60099b 3559 'copy-file
9e6ab520
MA
3560 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3561 (t
3562 (tramp-run-real-handler
3563 'copy-file (list filename newname ok-if-already-exists keep-date)))))
fb7933a3 3564
263c02ef
MA
3565(defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3566 "Like `copy-directory' for Tramp files."
3567 (let ((t1 (tramp-tramp-file-p dirname))
3568 (t2 (tramp-tramp-file-p newname)))
3569 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3570 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3571 ;; When DIRNAME and NEWNAME are remote, they must have
3572 ;; the same method.
3573 (or (null t1) (null t2)
b000a6e2
MA
3574 (string-equal
3575 (tramp-file-name-method (tramp-dissect-file-name dirname))
3576 (tramp-file-name-method (tramp-dissect-file-name newname)))))
263c02ef
MA
3577 ;; scp or rsync DTRT.
3578 (progn
3579 (setq dirname (directory-file-name (expand-file-name dirname))
3580 newname (directory-file-name (expand-file-name newname)))
3581 (if (and (file-directory-p newname)
3582 (not (string-equal (file-name-nondirectory dirname)
3583 (file-name-nondirectory newname))))
3584 (setq newname
3585 (expand-file-name
3586 (file-name-nondirectory dirname) newname)))
3587 (if (not (file-directory-p (file-name-directory newname)))
3588 (make-directory (file-name-directory newname) parents))
3589 (tramp-do-copy-or-rename-file-out-of-band
3590 'copy dirname newname keep-date))
3591 ;; We must do it file-wise.
3592 (tramp-run-real-handler
aac0b0f2
MA
3593 'copy-directory (list dirname newname keep-date parents)))
3594
3595 ;; When newname did exist, we have wrong cached values.
3596 (when t2
3597 (with-parsed-tramp-file-name newname nil
3598 (tramp-flush-file-property v (file-name-directory localname))
3599 (tramp-flush-file-property v localname))))))
263c02ef 3600
fb7933a3
KG
3601(defun tramp-handle-rename-file
3602 (filename newname &optional ok-if-already-exists)
00d6fd04 3603 "Like `rename-file' for Tramp files."
fb7933a3 3604 ;; Check if both files are local -- invoke normal rename-file.
a4aeb9a4 3605 ;; Otherwise, use Tramp from local system.
fb7933a3
KG
3606 (setq filename (expand-file-name filename))
3607 (setq newname (expand-file-name newname))
a4aeb9a4 3608 ;; At least one file a Tramp file?
fb7933a3
KG
3609 (if (or (tramp-tramp-file-p filename)
3610 (tramp-tramp-file-p newname))
3611 (tramp-do-copy-or-rename-file
8d60099b 3612 'rename filename newname ok-if-already-exists t t)
00d6fd04
MA
3613 (tramp-run-real-handler
3614 'rename-file (list filename newname ok-if-already-exists))))
fb7933a3
KG
3615
3616(defun tramp-do-copy-or-rename-file
632c5478
MA
3617 (op filename newname &optional ok-if-already-exists keep-date
3618 preserve-uid-gid preserve-selinux-context)
fb7933a3
KG
3619 "Copy or rename a remote file.
3620OP must be `copy' or `rename' and indicates the operation to perform.
3621FILENAME specifies the file to copy or rename, NEWNAME is the name of
3622the new file (for copy) or the new name of the file (for rename).
3623OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3624KEEP-DATE means to make sure that NEWNAME has the same timestamp
8d60099b
MA
3625as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3626the uid and gid if both files are on the same host.
632c5478 3627PRESERVE-SELINUX-CONTEXT activates selinux commands.
fb7933a3
KG
3628
3629This function is invoked by `tramp-handle-copy-file' and
3630`tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3631and `rename'. FILENAME and NEWNAME must be absolute file names."
3632 (unless (memq op '(copy rename))
3633 (error "Unknown operation `%s', must be `copy' or `rename'" op))
90dc758d 3634 (let ((t1 (tramp-tramp-file-p filename))
b6bbb65e 3635 (t2 (tramp-tramp-file-p newname))
632c5478
MA
3636 (context (and preserve-selinux-context
3637 (apply 'file-selinux-context (list filename))))
b6bbb65e 3638 pr tm)
5ec2cc41 3639
da1975d7
MA
3640 (when (and (not ok-if-already-exists) (file-exists-p newname))
3641 (with-parsed-tramp-file-name (if t1 filename newname) nil
3642 (tramp-error
3643 v 'file-already-exists "File %s already exists" newname)))
5ec2cc41 3644
905fb90e 3645 (with-parsed-tramp-file-name (if t1 filename newname) nil
a94d821f
MA
3646 (with-progress-reporter
3647 v 0 (format "Transferring %s to %s" filename newname)
905fb90e 3648
a94d821f
MA
3649 (cond
3650 ;; Both are Tramp files.
3651 ((and t1 t2)
3652 (with-parsed-tramp-file-name filename v1
3653 (with-parsed-tramp-file-name newname v2
3654 (cond
3655 ;; Shortcut: if method, host, user are the same for both
3656 ;; files, we invoke `cp' or `mv' on the remote host
3657 ;; directly.
3658 ((tramp-equal-remote filename newname)
3659 (tramp-do-copy-or-rename-file-directly
3660 op filename newname
3661 ok-if-already-exists keep-date preserve-uid-gid))
3662
3663 ;; Try out-of-band operation.
3664 ((tramp-method-out-of-band-p
3665 v1 (nth 7 (file-attributes filename)))
3666 (tramp-do-copy-or-rename-file-out-of-band
3667 op filename newname keep-date))
3668
3669 ;; No shortcut was possible. So we copy the
3670 ;; file first. If the operation was `rename', we go
3671 ;; back and delete the original file (if the copy was
3672 ;; successful). The approach is simple-minded: we
3673 ;; create a new buffer, insert the contents of the
3674 ;; source file into it, then write out the buffer to
3675 ;; the target file. The advantage is that it doesn't
3676 ;; matter which filename handlers are used for the
3677 ;; source and target file.
3678 (t
3679 (tramp-do-copy-or-rename-file-via-buffer
3680 op filename newname keep-date))))))
3681
3682 ;; One file is a Tramp file, the other one is local.
3683 ((or t1 t2)
3684 (cond
3685 ;; Fast track on local machine.
3686 ((tramp-local-host-p v)
3687 (tramp-do-copy-or-rename-file-directly
3688 op filename newname
3689 ok-if-already-exists keep-date preserve-uid-gid))
3690
3691 ;; If the Tramp file has an out-of-band method, the corresponding
3692 ;; copy-program can be invoked.
3693 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3694 (tramp-do-copy-or-rename-file-out-of-band
3695 op filename newname keep-date))
3696
3697 ;; Use the inline method via a Tramp buffer.
3698 (t (tramp-do-copy-or-rename-file-via-buffer
3699 op filename newname keep-date))))
3700
3701 (t
3702 ;; One of them must be a Tramp file.
3703 (error "Tramp implementation says this cannot happen")))
3704
632c5478
MA
3705 ;; Handle `preserve-selinux-context'.
3706 (when context (apply 'set-file-selinux-context (list newname context)))
3707
a94d821f
MA
3708 ;; In case of `rename', we must flush the cache of the source file.
3709 (when (and t1 (eq op 'rename))
3710 (with-parsed-tramp-file-name filename v1
3711 (tramp-flush-file-property v1 (file-name-directory localname))
3712 (tramp-flush-file-property v1 localname)))
3713
3714 ;; When newname did exist, we have wrong cached values.
3715 (when t2
3716 (with-parsed-tramp-file-name newname v2
3717 (tramp-flush-file-property v2 (file-name-directory localname))
3718 (tramp-flush-file-property v2 localname)))))))
7432277c 3719
38c65fca 3720(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
90dc758d
KG
3721 "Use an Emacs buffer to copy or rename a file.
3722First arg OP is either `copy' or `rename' and indicates the operation.
3723FILENAME is the source file, NEWNAME the target file.
3724KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
8a798e41
MA
3725 (with-temp-buffer
3726 ;; We must disable multibyte, because binary data shall not be
3727 ;; converted.
3728 (set-buffer-multibyte nil)
3729 (let ((coding-system-for-read 'binary)
3730 (jka-compr-inhibit t))
3731 (insert-file-contents-literally filename))
3732 ;; We don't want the target file to be compressed, so we let-bind
3733 ;; `jka-compr-inhibit' to t.
3734 (let ((coding-system-for-write 'binary)
3735 (jka-compr-inhibit t))
3736 (write-region (point-min) (point-max) newname)))
3737 ;; KEEP-DATE handling.
3738 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3739 ;; Set the mode.
b86c1cd8 3740 (set-file-modes newname (tramp-default-file-modes filename))
8a798e41 3741 ;; If the operation was `rename', delete the original file.
66bdc868 3742 (unless (eq op 'copy) (tramp-compat-delete-file filename 'force)))
fb7933a3
KG
3743
3744(defun tramp-do-copy-or-rename-file-directly
8d60099b 3745 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
fb7933a3
KG
3746 "Invokes `cp' or `mv' on the remote system.
3747OP must be one of `copy' or `rename', indicating `cp' or `mv',
8d60099b
MA
3748respectively. FILENAME specifies the file to copy or rename,
3749NEWNAME is the name of the new file (for copy) or the new name of
3750the file (for rename). Both files must reside on the same host.
3751KEEP-DATE means to make sure that NEWNAME has the same timestamp
3752as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3753the uid and gid from FILENAME."
8a4438b6 3754 (let ((t1 (tramp-tramp-file-p filename))
4f4126e6
MA
3755 (t2 (tramp-tramp-file-p newname))
3756 (file-times (nth 5 (file-attributes filename)))
3757 (file-modes (tramp-default-file-modes filename)))
8a4438b6
MA
3758 (with-parsed-tramp-file-name (if t1 filename newname) nil
3759 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3760 ((eq op 'copy) "cp -f")
3761 ((eq op 'rename) "mv -f")
3762 (t (tramp-error
3763 v 'file-error
3764 "Unknown operation `%s', must be `copy' or `rename'"
3765 op))))
3766 (localname1
b533bc97
MA
3767 (if t1
3768 (tramp-file-name-handler 'file-remote-p filename 'localname)
3769 filename))
8a4438b6 3770 (localname2
b533bc97
MA
3771 (if t2
3772 (tramp-file-name-handler 'file-remote-p newname 'localname)
3773 newname))
293c24f9
MA
3774 (prefix (file-remote-p (if t1 filename newname)))
3775 cmd-result)
8d60099b 3776
8d60099b 3777 (cond
8a4438b6
MA
3778 ;; Both files are on a remote host, with same user.
3779 ((and t1 t2)
293c24f9
MA
3780 (setq cmd-result
3781 (tramp-send-command-and-check
3782 v
3783 (format "%s %s %s" cmd
3784 (tramp-shell-quote-argument localname1)
3785 (tramp-shell-quote-argument localname2))))
8a4438b6
MA
3786 (with-current-buffer (tramp-get-buffer v)
3787 (goto-char (point-min))
3788 (unless
3789 (or
3790 (and keep-date
3791 ;; Mask cp -f error.
3792 (re-search-forward
3793 tramp-operation-not-permitted-regexp nil t))
293c24f9 3794 (zerop cmd-result))
8a4438b6
MA
3795 (tramp-error-with-buffer
3796 nil v 'file-error
3797 "Copying directly failed, see buffer `%s' for details."
3798 (buffer-name)))))
3799
3800 ;; We are on the local host.
3801 ((or t1 t2)
8d60099b 3802 (cond
8a4438b6 3803 ;; We can do it directly.
87bdd2c7
MA
3804 ((let (file-name-handler-alist)
3805 (and (file-readable-p localname1)
3806 (file-writable-p (file-name-directory localname2))
3807 (or (file-directory-p localname2)
3808 (file-writable-p localname2))))
8d60099b 3809 (if (eq op 'copy)
9e6ab520
MA
3810 (tramp-compat-copy-file
3811 localname1 localname2 ok-if-already-exists
3812 keep-date preserve-uid-gid)
87bdd2c7
MA
3813 (tramp-run-real-handler
3814 'rename-file (list localname1 localname2 ok-if-already-exists))))
8a4438b6
MA
3815
3816 ;; We can do it directly with `tramp-send-command'
946a5aeb
MA
3817 ((and (file-readable-p (concat prefix localname1))
3818 (file-writable-p
3819 (file-name-directory (concat prefix localname2)))
3820 (or (file-directory-p (concat prefix localname2))
3821 (file-writable-p (concat prefix localname2))))
8a4438b6
MA
3822 (tramp-do-copy-or-rename-file-directly
3823 op (concat prefix localname1) (concat prefix localname2)
3824 ok-if-already-exists keep-date t)
3825 ;; We must change the ownership to the local user.
8d60099b 3826 (tramp-set-file-uid-gid
8a4438b6
MA
3827 (concat prefix localname2)
3828 (tramp-get-local-uid 'integer)
3829 (tramp-get-local-gid 'integer)))
8d60099b 3830
8a4438b6
MA
3831 ;; We need a temporary file in between.
3832 (t
2c418c5b
MA
3833 ;; Create the temporary file.
3834 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
917b89a6 3835 (unwind-protect
2c418c5b
MA
3836 (progn
3837 (cond
3838 (t1
917b89a6
MA
3839 (or
3840 (zerop
3841 (tramp-send-command-and-check
3842 v (format
3843 "%s %s %s" cmd
3844 (tramp-shell-quote-argument localname1)
3845 (tramp-shell-quote-argument tmpfile))))
3846 (tramp-error-with-buffer
3847 nil v 'file-error
3848 "Copying directly failed, see buffer `%s' for details."
3849 (tramp-get-buffer v)))
2c418c5b 3850 ;; We must change the ownership as remote user.
917b89a6
MA
3851 ;; Since this does not work reliable, we also
3852 ;; give read permissions.
3853 (set-file-modes
3854 (concat prefix tmpfile) (tramp-octal-to-decimal "0777"))
2c418c5b
MA
3855 (tramp-set-file-uid-gid
3856 (concat prefix tmpfile)
3857 (tramp-get-local-uid 'integer)
3858 (tramp-get-local-gid 'integer)))
3859 (t2
3860 (if (eq op 'copy)
3861 (tramp-compat-copy-file
5ab38c3c 3862 localname1 tmpfile t
2c418c5b
MA
3863 keep-date preserve-uid-gid)
3864 (tramp-run-real-handler
3865 'rename-file
5ab38c3c 3866 (list localname1 tmpfile t)))
2c418c5b 3867 ;; We must change the ownership as local user.
917b89a6
MA
3868 ;; Since this does not work reliable, we also
3869 ;; give read permissions.
3870 (set-file-modes tmpfile (tramp-octal-to-decimal "0777"))
2c418c5b
MA
3871 (tramp-set-file-uid-gid
3872 tmpfile
3873 (tramp-get-remote-uid v 'integer)
3874 (tramp-get-remote-gid v 'integer))))
3875
3876 ;; Move the temporary file to its destination.
3877 (cond
3878 (t2
917b89a6
MA
3879 (or
3880 (zerop
3881 (tramp-send-command-and-check
3882 v (format
3883 "cp -f -p %s %s"
3884 (tramp-shell-quote-argument tmpfile)
3885 (tramp-shell-quote-argument localname2))))
3886 (tramp-error-with-buffer
3887 nil v 'file-error
3888 "Copying directly failed, see buffer `%s' for details."
3889 (tramp-get-buffer v))))
2c418c5b 3890 (t1
ce2cc728
MA
3891 (tramp-run-real-handler
3892 'rename-file
2c418c5b
MA
3893 (list tmpfile localname2 ok-if-already-exists)))))
3894
917b89a6
MA
3895 ;; Save exit.
3896 (condition-case nil
66bdc868 3897 (tramp-compat-delete-file tmpfile 'force)
917b89a6 3898 (error)))))))))
8d60099b
MA
3899
3900 ;; Set the time and mode. Mask possible errors.
8d60099b 3901 (condition-case nil
1f107aed 3902 (when keep-date
4f4126e6
MA
3903 (set-file-times newname file-times)
3904 (set-file-modes newname file-modes))
8d60099b
MA
3905 (error)))))
3906
5ec2cc41 3907(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
7432277c 3908 "Invoke rcp program to copy.
905fb90e 3909The method used must be an out-of-band method."
38c65fca 3910 (let ((t1 (tramp-tramp-file-p filename))
5ec2cc41 3911 (t2 (tramp-tramp-file-p newname))
946a5aeb 3912 copy-program copy-args copy-env copy-keep-date port spec
00d6fd04
MA
3913 source target)
3914
3915 (with-parsed-tramp-file-name (if t1 filename newname) nil
905fb90e 3916 (if (and t1 t2)
00d6fd04 3917
905fb90e
MA
3918 ;; Both are Tramp files. We shall optimize it, when the
3919 ;; methods for filename and newname are the same.
9cf3544e
MA
3920 (let* ((dir-flag (file-directory-p filename))
3921 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
3922 (if dir-flag
3923 (setq tmpfile
3924 (expand-file-name
3925 (file-name-nondirectory newname) tmpfile)))
905fb90e
MA
3926 (unwind-protect
3927 (progn
3928 (tramp-do-copy-or-rename-file-out-of-band
3929 op filename tmpfile keep-date)
3930 (tramp-do-copy-or-rename-file-out-of-band
3931 'rename tmpfile newname keep-date))
3932 ;; Save exit.
3933 (condition-case nil
9cf3544e 3934 (if dir-flag
b533bc97 3935 (tramp-compat-delete-directory
9cf3544e 3936 (expand-file-name ".." tmpfile) 'recursive)
66bdc868 3937 (tramp-compat-delete-file tmpfile 'force))
905fb90e
MA
3938 (error))))
3939
3940 ;; Expand hops. Might be necessary for gateway methods.
3941 (setq v (car (tramp-compute-multi-hops v)))
3942 (aset v 3 localname)
3943
3944 ;; Check which ones of source and target are Tramp files.
3945 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
263c02ef
MA
3946 target (funcall
3947 (if (and (file-directory-p filename)
3948 (string-equal
3949 (file-name-nondirectory filename)
3950 (file-name-nondirectory newname)))
3951 'file-name-directory
3952 'identity)
3953 (if t2 (tramp-make-copy-program-file-name v) newname)))
905fb90e
MA
3954
3955 ;; Check for port number. Until now, there's no need for handling
3956 ;; like method, user, host.
3957 (setq host (tramp-file-name-real-host v)
3958 port (tramp-file-name-port v)
3959 port (or (and port (number-to-string port)) ""))
3960
3961 ;; Compose copy command.
b533bc97
MA
3962 (setq spec (format-spec-make
3963 ?h host ?u user ?p port
3964 ?t (tramp-get-connection-property
3965 (tramp-get-connection-process v) "temp-file" "")
3966 ?k (if keep-date " " ""))
905fb90e
MA
3967 copy-program (tramp-get-method-parameter
3968 method 'tramp-copy-program)
3969 copy-keep-date (tramp-get-method-parameter
3970 method 'tramp-copy-keep-date)
3971 copy-args
3972 (delq
3973 nil
3974 (mapcar
aa485f7c
MA
3975 (lambda (x)
3976 (setq
3977 x
3978 ;; " " is indication for keep-date argument.
3979 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
3980 (unless (member "" x) (mapconcat 'identity x " ")))
946a5aeb
MA
3981 (tramp-get-method-parameter method 'tramp-copy-args)))
3982 copy-env
3983 (delq
3984 nil
3985 (mapcar
aa485f7c
MA
3986 (lambda (x)
3987 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
3988 (unless (member "" x) (mapconcat 'identity x " ")))
946a5aeb 3989 (tramp-get-method-parameter method 'tramp-copy-env))))
905fb90e
MA
3990
3991 ;; Check for program.
3992 (when (and (fboundp 'executable-find)
3993 (not (let ((default-directory
3994 (tramp-compat-temporary-file-directory)))
3995 (executable-find copy-program))))
3996 (tramp-error
3997 v 'file-error "Cannot find copy program: %s" copy-program))
00d6fd04 3998
4265deab
MA
3999 ;; Set variables for computing the prompt for reading
4000 ;; password.
4001 (setq tramp-current-method (tramp-file-name-method v)
4002 tramp-current-user (tramp-file-name-user v)
4003 tramp-current-host (tramp-file-name-host v))
4004
905fb90e
MA
4005 (unwind-protect
4006 (with-temp-buffer
4007 ;; The default directory must be remote.
4008 (let ((default-directory
946a5aeb
MA
4009 (file-name-directory (if t1 filename newname)))
4010 (process-environment (copy-sequence process-environment)))
905fb90e
MA
4011 ;; Set the transfer process properties.
4012 (tramp-set-connection-property
4013 v "process-name" (buffer-name (current-buffer)))
4014 (tramp-set-connection-property
4015 v "process-buffer" (current-buffer))
946a5aeb
MA
4016 (while copy-env
4017 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
4018 (setenv (pop copy-env) (pop copy-env)))
905fb90e
MA
4019
4020 ;; Use an asynchronous process. By this, password can
4021 ;; be handled. The default directory must be local, in
4022 ;; order to apply the correct `copy-program'. We don't
4023 ;; set a timeout, because the copying of large files can
4024 ;; last longer than 60 secs.
4025 (let ((p (let ((default-directory
4026 (tramp-compat-temporary-file-directory)))
4027 (apply 'start-process
4028 (tramp-get-connection-property
4029 v "process-name" nil)
4030 (tramp-get-connection-property
4031 v "process-buffer" nil)
4032 copy-program
4033 (append copy-args (list source target))))))
4034 (tramp-message
4035 v 6 "%s" (mapconcat 'identity (process-command p) " "))
4036 (tramp-set-process-query-on-exit-flag p nil)
4037 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
00d6fd04 4038
905fb90e
MA
4039 ;; Reset the transfer process properties.
4040 (tramp-set-connection-property v "process-name" nil)
4041 (tramp-set-connection-property v "process-buffer" nil))
00d6fd04 4042
905fb90e
MA
4043 ;; Handle KEEP-DATE argument.
4044 (when (and keep-date (not copy-keep-date))
4045 (set-file-times newname (nth 5 (file-attributes filename))))
01917a18 4046
905fb90e
MA
4047 ;; Set the mode.
4048 (unless (and keep-date copy-keep-date)
459a5f4b
MA
4049 (ignore-errors
4050 (set-file-modes newname (tramp-default-file-modes filename)))))
5ec2cc41 4051
905fb90e
MA
4052 ;; If the operation was `rename', delete the original file.
4053 (unless (eq op 'copy)
aac0b0f2 4054 (if (file-regular-p filename)
66bdc868 4055 (tramp-compat-delete-file filename 'force)
b533bc97 4056 (tramp-compat-delete-directory filename 'recursive))))))
7432277c 4057
fb7933a3 4058(defun tramp-handle-make-directory (dir &optional parents)
00d6fd04 4059 "Like `make-directory' for Tramp files."
ac474af1 4060 (setq dir (expand-file-name dir))
c62c9d08 4061 (with-parsed-tramp-file-name dir nil
c15cdf02 4062 (tramp-flush-directory-property v (file-name-directory localname))
b1d06e75
KG
4063 (save-excursion
4064 (tramp-barf-unless-okay
00d6fd04 4065 v
9c13938d 4066 (format "%s %s"
b1d06e75 4067 (if parents "mkdir -p" "mkdir")
7432277c 4068 (tramp-shell-quote-argument localname))
b1d06e75 4069 "Couldn't make directory %s" dir))))
fb7933a3 4070
c15cdf02 4071(defun tramp-handle-delete-directory (directory &optional recursive)
00d6fd04 4072 "Like `delete-directory' for Tramp files."
ac474af1 4073 (setq directory (expand-file-name directory))
c62c9d08 4074 (with-parsed-tramp-file-name directory nil
aac0b0f2 4075 (tramp-flush-file-property v (file-name-directory localname))
00d6fd04
MA
4076 (tramp-flush-directory-property v localname)
4077 (unless (zerop (tramp-send-command-and-check
4078 v
c15cdf02
MA
4079 (format
4080 "%s %s"
4081 (if recursive "rm -rf" "rmdir")
4082 (tramp-shell-quote-argument localname))))
00d6fd04 4083 (tramp-error v 'file-error "Couldn't delete %s" directory))))
fb7933a3 4084
66bdc868 4085(defun tramp-handle-delete-file (filename &optional force)
00d6fd04 4086 "Like `delete-file' for Tramp files."
ac474af1 4087 (setq filename (expand-file-name filename))
c62c9d08 4088 (with-parsed-tramp-file-name filename nil
aac0b0f2 4089 (tramp-flush-file-property v (file-name-directory localname))
00d6fd04
MA
4090 (tramp-flush-file-property v localname)
4091 (unless (zerop (tramp-send-command-and-check
4092 v
4093 (format "rm -f %s"
4094 (tramp-shell-quote-argument localname))))
4095 (tramp-error v 'file-error "Couldn't delete %s" filename))))
fb7933a3
KG
4096
4097;; Dired.
4098
4099;; CCC: This does not seem to be enough. Something dies when
a4aeb9a4 4100;; we try and delete two directories under Tramp :/
fb7933a3
KG
4101(defun tramp-handle-dired-recursive-delete-directory (filename)
4102 "Recursively delete the directory given.
00d6fd04 4103This is like `dired-recursive-delete-directory' for Tramp files."
c62c9d08 4104 (with-parsed-tramp-file-name filename nil
00d6fd04 4105 ;; Run a shell command 'rm -r <localname>'
260821d3 4106 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
00d6fd04
MA
4107 (unless (file-exists-p filename)
4108 (tramp-error v 'file-error "No such directory: %s" filename))
fb7933a3 4109 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
00d6fd04
MA
4110 (tramp-send-command
4111 v
9c13938d 4112 (format "rm -rf %s" (tramp-shell-quote-argument localname))
00d6fd04
MA
4113 ;; Don't read the output, do it explicitely.
4114 nil t)
fb7933a3
KG
4115 ;; Wait for the remote system to return to us...
4116 ;; This might take a while, allow it plenty of time.
00d6fd04 4117 (tramp-wait-for-output (tramp-get-connection-process v) 120)
fb7933a3 4118 ;; Make sure that it worked...
aac0b0f2 4119 (tramp-flush-file-property v (file-name-directory localname))
c15cdf02 4120 (tramp-flush-directory-property v localname)
07dfe738 4121 (and (file-exists-p filename)
00d6fd04
MA
4122 (tramp-error
4123 v 'file-error "Failed to recursively delete %s" filename))))
bf247b6e 4124
5ec2cc41 4125(defun tramp-handle-dired-compress-file (file &rest ok-flag)
00d6fd04 4126 "Like `dired-compress-file' for Tramp files."
5ec2cc41
KG
4127 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4128 ;; Code stolen mainly from dired-aux.el.
4129 (with-parsed-tramp-file-name file nil
00d6fd04 4130 (tramp-flush-file-property v localname)
5ec2cc41
KG
4131 (save-excursion
4132 (let ((suffixes
4133 (if (not (featurep 'xemacs))
4134 ;; Emacs case
4135 (symbol-value 'dired-compress-file-suffixes)
4136 ;; XEmacs has `dired-compression-method-alist', which is
4137 ;; transformed into `dired-compress-file-suffixes' structure.
4138 (mapcar
aa485f7c
MA
4139 (lambda (x)
4140 (list (concat (regexp-quote (nth 1 x)) "\\'")
4141 nil
4142 (mapconcat 'identity (nth 3 x) " ")))
5ec2cc41
KG
4143 (symbol-value 'dired-compression-method-alist))))
4144 suffix)
4145 ;; See if any suffix rule matches this file name.
4146 (while suffixes
4147 (let (case-fold-search)
4148 (if (string-match (car (car suffixes)) localname)
4149 (setq suffix (car suffixes) suffixes nil))
4150 (setq suffixes (cdr suffixes))))
4151
4152 (cond ((file-symlink-p file)
4153 nil)
4154 ((and suffix (nth 2 suffix))
4155 ;; We found an uncompression rule.
a94d821f
MA
4156 (with-progress-reporter v 0 (format "Uncompressing %s..." file)
4157 (when (zerop
4158 (tramp-send-command-and-check
4159 v (concat (nth 2 suffix) " "
4160 (tramp-shell-quote-argument localname))))
0d5852cf
MA
4161 ;; `dired-remove-file' is not defined in XEmacs.
4162 (tramp-compat-funcall 'dired-remove-file file)
a94d821f
MA
4163 (string-match (car suffix) file)
4164 (concat (substring file 0 (match-beginning 0))))))
5ec2cc41
KG
4165 (t
4166 ;; We don't recognize the file as compressed, so compress it.
4167 ;; Try gzip.
a94d821f
MA
4168 (with-progress-reporter v 0 (format "Compressing %s..." file)
4169 (when (zerop
4170 (tramp-send-command-and-check
4171 v (concat "gzip -f "
4172 (tramp-shell-quote-argument localname))))
0d5852cf
MA
4173 ;; `dired-remove-file' is not defined in XEmacs.
4174 (tramp-compat-funcall 'dired-remove-file file)
a94d821f
MA
4175 (cond ((file-exists-p (concat file ".gz"))
4176 (concat file ".gz"))
4177 ((file-exists-p (concat file ".z"))
4178 (concat file ".z"))
4179 (t nil))))))))))
fb7933a3 4180
d5b3979c 4181(defun tramp-handle-dired-uncache (dir &optional dir-p)
70c11b0b 4182 "Like `dired-uncache' for Tramp files."
d5b3979c
MA
4183 ;; DIR-P is valid for XEmacs only.
4184 (with-parsed-tramp-file-name
4185 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
70c11b0b
MA
4186 (tramp-flush-file-property v localname)))
4187
fb7933a3
KG
4188;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4189;; not sure at all that this is the right way to do it, but let's hope
4190;; it works for now, and wait for a guru to point out the Right Way to
4191;; achieve this.
4192;;(eval-when-compile
4193;; (unless (fboundp 'dired-insert-set-properties)
4194;; (fset 'dired-insert-set-properties 'ignore)))
4195;; Gerd suggests this:
4196(eval-when-compile (require 'dired))
4197;; Note that dired is required at run-time, too, when it is needed.
4198;; It is only needed on XEmacs for the function
4199;; `dired-insert-set-properties'.
4200
4201(defun tramp-handle-insert-directory
4202 (filename switches &optional wildcard full-directory-p)
00d6fd04
MA
4203 "Like `insert-directory' for Tramp files."
4204 (setq filename (expand-file-name filename))
4205 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
4206 (if (and (featurep 'ls-lisp)
4207 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
4208 (tramp-run-real-handler
4209 'insert-directory (list filename switches wildcard full-directory-p))
0c0b61f1
MA
4210 (when (stringp switches)
4211 (setq switches (split-string switches)))
4212 (when (and (member "--dired" switches)
8e754ea2 4213 (not (tramp-get-ls-command-with-dired v)))
0c0b61f1 4214 (setq switches (delete "--dired" switches)))
c82c5727 4215 (when wildcard
87bdd2c7
MA
4216 (setq wildcard (tramp-run-real-handler
4217 'file-name-nondirectory (list localname)))
4218 (setq localname (tramp-run-real-handler
4219 'file-name-directory (list localname))))
c82c5727 4220 (unless full-directory-p
0c0b61f1
MA
4221 (setq switches (add-to-list 'switches "-d" 'append)))
4222 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
c82c5727 4223 (when wildcard
0c0b61f1
MA
4224 (setq switches (concat switches " " wildcard)))
4225 (tramp-message
4226 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4227 switches filename (if wildcard "yes" "no")
4228 (if full-directory-p "yes" "no"))
00d6fd04
MA
4229 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4230 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4231 (if full-directory-p
4232 (tramp-send-command
4233 v
fe5facd3 4234 (format "%s %s %s 2>/dev/null"
00d6fd04
MA
4235 (tramp-get-ls-command v)
4236 switches
4237 (if wildcard
4238 localname
4239 (tramp-shell-quote-argument (concat localname ".")))))
4240 (tramp-barf-unless-okay
4241 v
4242 (format "cd %s" (tramp-shell-quote-argument
87bdd2c7
MA
4243 (tramp-run-real-handler
4244 'file-name-directory (list localname))))
00d6fd04 4245 "Couldn't `cd %s'"
87bdd2c7
MA
4246 (tramp-shell-quote-argument
4247 (tramp-run-real-handler 'file-name-directory (list localname))))
00d6fd04
MA
4248 (tramp-send-command
4249 v
4250 (format "%s %s %s"
4251 (tramp-get-ls-command v)
4252 switches
4253 (if (or wildcard
87bdd2c7
MA
4254 (zerop (length
4255 (tramp-run-real-handler
4256 'file-name-nondirectory (list localname)))))
00d6fd04
MA
4257 ""
4258 (tramp-shell-quote-argument
87bdd2c7
MA
4259 (tramp-run-real-handler
4260 'file-name-nondirectory (list localname)))))))
8e754ea2
MA
4261 (let ((beg (point)))
4262 ;; We cannot use `insert-buffer-substring' because the Tramp
4263 ;; buffer changes its contents before insertion due to calling
4264 ;; `expand-file' and alike.
4265 (insert
4266 (with-current-buffer (tramp-get-buffer v)
4267 (buffer-string)))
4268
4269 ;; Check for "--dired" output.
8e754ea2 4270 (forward-line -2)
7f4d4a97
MA
4271 (when (looking-at "//SUBDIRED//")
4272 (forward-line -1))
8e754ea2 4273 (when (looking-at "//DIRED//")
7ba1d9c2 4274 (let ((end (tramp-compat-line-end-position))
8e754ea2
MA
4275 (linebeg (point)))
4276 ;; Now read the numeric positions of file names.
4277 (goto-char linebeg)
4278 (forward-word 1)
4279 (forward-char 3)
4280 (while (< (point) end)
4281 (let ((start (+ beg (read (current-buffer))))
4282 (end (+ beg (read (current-buffer)))))
7f49fe46 4283 (if (memq (char-after end) '(?\n ?\ ))
8e754ea2 4284 ;; End is followed by \n or by " -> ".
fe5facd3
MA
4285 (put-text-property start end 'dired-filename t))))))
4286 ;; Remove trailing lines.
4287 (goto-char (tramp-compat-line-beginning-position))
4288 (while (looking-at "//")
4289 (forward-line 1)
4290 (delete-region (match-beginning 0) (point)))
0c0b61f1
MA
4291
4292 ;; The inserted file could be from somewhere else.
4293 (when (and (not wildcard) (not full-directory-p))
4294 (goto-char (point-max))
e5c70c41
MA
4295 (when (file-symlink-p filename)
4296 (goto-char (search-backward "->" beg 'noerror)))
0c0b61f1
MA
4297 (search-backward
4298 (if (zerop (length (file-name-nondirectory filename)))
4299 "."
4300 (file-name-nondirectory filename))
4301 beg 'noerror)
4302 (replace-match (file-relative-name filename) t))
4303
fe5facd3 4304 (goto-char (point-max))))))
fb7933a3 4305
fb7933a3 4306(defun tramp-handle-unhandled-file-name-directory (filename)
00d6fd04 4307 "Like `unhandled-file-name-directory' for Tramp files."
8a798e41
MA
4308 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4309 ;; for backward compatibility.
ce3f516f 4310 (expand-file-name "~/"))
fb7933a3
KG
4311
4312;; Canonicalization of file names.
4313
fb7933a3 4314(defun tramp-handle-expand-file-name (name &optional dir)
00d6fd04 4315 "Like `expand-file-name' for Tramp files.
7432277c
KG
4316If the localname part of the given filename starts with \"/../\" then
4317the result will be a local, non-Tramp, filename."
fb7933a3
KG
4318 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4319 (setq dir (or dir default-directory "/"))
4320 ;; Unless NAME is absolute, concat DIR and NAME.
4321 (unless (file-name-absolute-p name)
4322 (setq name (concat (file-name-as-directory dir) name)))
00d6fd04 4323 ;; If NAME is not a Tramp file, run the real handler.
acd1f317 4324 (if (not (tramp-connectable-p name))
00d6fd04 4325 (tramp-run-real-handler 'expand-file-name (list name nil))
fb7933a3 4326 ;; Dissect NAME.
c62c9d08 4327 (with-parsed-tramp-file-name name nil
87bdd2c7 4328 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
7432277c 4329 (setq localname (concat "~/" localname)))
00d6fd04
MA
4330 ;; Tilde expansion if necessary. This needs a shell which
4331 ;; groks tilde expansion! The function `tramp-find-shell' is
4332 ;; supposed to find such a shell on the remote host. Please
4333 ;; tell me about it when this doesn't work on your system.
4334 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4335 (let ((uname (match-string 1 localname))
4336 (fname (match-string 2 localname)))
4337 ;; We cannot simply apply "~/", because under sudo "~/" is
4338 ;; expanded to the local user home directory but to the
4339 ;; root home directory. On the other hand, using always
4340 ;; the default user name for tilde expansion is not
4341 ;; appropriate either, because ssh and companions might
4342 ;; use a user name from the config file.
4343 (when (and (string-equal uname "~")
4344 (string-match "\\`su\\(do\\)?\\'" method))
4345 (setq uname (concat uname user)))
4346 (setq uname
b593f105
MA
4347 (with-connection-property v uname
4348 (tramp-send-command
4349 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
4350 (with-current-buffer (tramp-get-buffer v)
4351 (goto-char (point-min))
4352 (buffer-substring
4353 (point) (tramp-compat-line-end-position)))))
00d6fd04
MA
4354 (setq localname (concat uname fname))))
4355 ;; There might be a double slash, for example when "~/"
cb85dcd0 4356 ;; expands to "/". Remove this.
00d6fd04
MA
4357 (while (string-match "//" localname)
4358 (setq localname (replace-match "/" t t localname)))
4359 ;; No tilde characters in file name, do normal
a17632c1
MA
4360 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4361 ;; `directory-sep-char' here for XEmacs on Windows, which would
4362 ;; otherwise use backslash. `default-directory' is bound,
4363 ;; because on Windows there would be problems with UNC shares or
4364 ;; Cygwin mounts.
aff67808
MA
4365 (let ((directory-sep-char ?/)
4366 (default-directory (tramp-compat-temporary-file-directory)))
4367 (tramp-make-tramp-file-name
4368 method user host
4369 (tramp-drop-volume-letter
87bdd2c7
MA
4370 (tramp-run-real-handler
4371 'expand-file-name (list localname))))))))
00d6fd04 4372
c23c3394
MA
4373(defun tramp-replace-environment-variables (filename)
4374 "Replace environment variables in FILENAME.
4375Return the string with the replaced variables."
2e271195 4376 (save-match-data
ec5145d6 4377 (let ((idx (string-match "$\\(\\w+\\)" filename)))
2e271195 4378 ;; `$' is coded as `$$'.
ec5145d6
MA
4379 (when (and idx
4380 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
4381 (getenv (match-string 1 filename)))
2e271195
MA
4382 (setq filename
4383 (replace-match
4384 (substitute-in-file-name (match-string 0 filename))
4385 t nil filename)))
4386 filename)))
c23c3394 4387
00d6fd04
MA
4388(defun tramp-handle-substitute-in-file-name (filename)
4389 "Like `substitute-in-file-name' for Tramp files.
4390\"//\" and \"/~\" substitute only in the local filename part.
4391If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4392beginning of local filename are not substituted."
c23c3394
MA
4393 ;; First, we must replace environment variables.
4394 (setq filename (tramp-replace-environment-variables filename))
00d6fd04
MA
4395 (with-parsed-tramp-file-name filename nil
4396 (if (equal tramp-syntax 'url)
4397 ;; We need to check localname only. The other parts cannot contain
4398 ;; "//" or "/~".
4399 (if (and (> (length localname) 1)
4400 (or (string-match "//" localname)
4401 (string-match "/~" localname 1)))
4402 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4403 (tramp-make-tramp-file-name
4404 (when method (substitute-in-file-name method))
4405 (when user (substitute-in-file-name user))
4406 (when host (substitute-in-file-name host))
87bdd2c7
MA
4407 (when localname
4408 (tramp-run-real-handler
4409 'substitute-in-file-name (list localname)))))
00d6fd04
MA
4410 ;; Ignore in LOCALNAME everything before "//" or "/~".
4411 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4412 (setq filename
b08104a0
MA
4413 (concat (file-remote-p filename)
4414 (replace-match "\\1" nil nil localname)))
00d6fd04
MA
4415 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4416 (when (string-match "~$" filename)
4417 (setq filename (concat filename "/"))))
4418 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4419
4420;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4421;; which calls corresponding functions (see minibuf.el).
4422(when (fboundp 'minibuffer-electric-separator)
9e6ab520 4423 (mapc
aa485f7c
MA
4424 (lambda (x)
4425 (eval
4426 `(defadvice ,x
4427 (around ,(intern (format "tramp-advice-%s" x)) activate)
4428 "Invoke `substitute-in-file-name' for Tramp files."
4429 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
4430 (tramp-tramp-file-p (buffer-substring)))
4431 ;; We don't need to handle `last-input-event', because
4432 ;; due to the key map we know it must be ?/ or ?~.
4433 (let ((s (concat (buffer-substring (point-min) (point))
4434 (string last-command-char))))
4435 (delete-region (point-min) (point))
4436 (insert (substitute-in-file-name s))
4437 (setq ad-return-value last-command-char))
d7ec1df7
MA
4438 ad-do-it)))
4439 (eval
4440 `(add-hook
4441 'tramp-unload-hook
4442 (lambda ()
4443 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
4444 (ad-activate ',x)))))
00d6fd04
MA
4445
4446 '(minibuffer-electric-separator
4447 minibuffer-electric-tilde)))
4448
4449
0664ff72 4450;;; Remote commands:
fb7933a3 4451
00d6fd04
MA
4452(defun tramp-handle-executable-find (command)
4453 "Like `executable-find' for Tramp files."
4454 (with-parsed-tramp-file-name default-directory nil
f84638eb 4455 (tramp-find-executable v command (tramp-get-remote-path v) t)))
00d6fd04
MA
4456
4457;; We use BUFFER also as connection buffer during setup. Because of
4458;; this, its original contents must be saved, and restored once
4459;; connection has been setup.
4460(defun tramp-handle-start-file-process (name buffer program &rest args)
4461 "Like `start-file-process' for Tramp files."
4462 (with-parsed-tramp-file-name default-directory nil
9fb2cdc5
GM
4463 (unless (stringp program)
4464 (tramp-error
4465 v 'file-error "pty association is not supported for `%s'" name))
00d6fd04 4466 (unwind-protect
263c02ef
MA
4467 (let ((command (format "cd %s; exec %s"
4468 (tramp-shell-quote-argument localname)
4469 (mapconcat 'tramp-shell-quote-argument
4470 (cons program args) " ")))
4471 (name1 name)
38d63e6a 4472 (i 0))
2296b54d 4473 (unless buffer
6ce63faf 4474 ;; BUFFER can be nil. We use a temporary buffer.
2296b54d 4475 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
38d63e6a
MA
4476 (while (get-process name1)
4477 ;; NAME must be unique as process name.
4478 (setq i (1+ i)
4479 name1 (format "%s<%d>" name i)))
4480 (setq name name1)
00d6fd04
MA
4481 ;; Set the new process properties.
4482 (tramp-set-connection-property v "process-name" name)
2296b54d 4483 (tramp-set-connection-property v "process-buffer" buffer)
00d6fd04 4484 ;; Activate narrowing in order to save BUFFER contents.
11c71217
MA
4485 ;; Clear also the modification time; otherwise we might be
4486 ;; interrupted by `verify-visited-file-modtime'.
00d6fd04 4487 (with-current-buffer (tramp-get-connection-buffer v)
11c71217 4488 (clear-visited-file-modtime)
00d6fd04 4489 (narrow-to-region (point-max) (point-max)))
263c02ef 4490 ;; Send the command. `tramp-send-command' opens a new
00d6fd04 4491 ;; connection.
263c02ef 4492 (tramp-send-command v command nil t) ; nooutput
6ce63faf
MA
4493 ;; Set query flag for this process.
4494 (tramp-set-process-query-on-exit-flag
4495 (tramp-get-connection-process v) t)
00d6fd04
MA
4496 ;; Return process.
4497 (tramp-get-connection-process v))
4498 ;; Save exit.
ce3f516f 4499 (with-current-buffer (tramp-get-connection-buffer v)
6ce63faf
MA
4500 (if (string-match tramp-temp-buffer-name (buffer-name))
4501 (progn
4502 (set-process-buffer (tramp-get-connection-process v) nil)
4503 (kill-buffer (current-buffer)))
4504 (widen)
4505 (goto-char (point-max))))
00d6fd04
MA
4506 (tramp-set-connection-property v "process-name" nil)
4507 (tramp-set-connection-property v "process-buffer" nil))))
4508
4509(defun tramp-handle-process-file
4510 (program &optional infile destination display &rest args)
4511 "Like `process-file' for Tramp files."
4512 ;; The implementation is not complete yet.
4513 (when (and (numberp destination) (zerop destination))
4514 (error "Implementation does not handle immediate return"))
4515
4516 (with-parsed-tramp-file-name default-directory nil
a6e96327 4517 (let (command input tmpinput stderr tmpstderr outbuf ret)
00d6fd04
MA
4518 ;; Compute command.
4519 (setq command (mapconcat 'tramp-shell-quote-argument
4520 (cons program args) " "))
4521 ;; Determine input.
4522 (if (null infile)
4523 (setq input "/dev/null")
4524 (setq infile (expand-file-name infile))
4525 (if (tramp-equal-remote default-directory infile)
4526 ;; INFILE is on the same remote host.
4527 (setq input (with-parsed-tramp-file-name infile nil localname))
4528 ;; INFILE must be copied to remote host.
a6e96327
MA
4529 (setq input (tramp-make-tramp-temp-file v)
4530 tmpinput (tramp-make-tramp-file-name method user host input))
4531 (copy-file infile tmpinput t)))
00d6fd04
MA
4532 (when input (setq command (format "%s <%s" command input)))
4533
4534 ;; Determine output.
4535 (cond
bede3e9f 4536 ;; Just a buffer.
00d6fd04
MA
4537 ((bufferp destination)
4538 (setq outbuf destination))
bede3e9f 4539 ;; A buffer name.
00d6fd04
MA
4540 ((stringp destination)
4541 (setq outbuf (get-buffer-create destination)))
4542 ;; (REAL-DESTINATION ERROR-DESTINATION)
4543 ((consp destination)
bede3e9f 4544 ;; output.
00d6fd04
MA
4545 (cond
4546 ((bufferp (car destination))
4547 (setq outbuf (car destination)))
4548 ((stringp (car destination))
0664ff72
MA
4549 (setq outbuf (get-buffer-create (car destination))))
4550 ((car destination)
4551 (setq outbuf (current-buffer))))
bede3e9f 4552 ;; stderr.
00d6fd04
MA
4553 (cond
4554 ((stringp (cadr destination))
4555 (setcar (cdr destination) (expand-file-name (cadr destination)))
4556 (if (tramp-equal-remote default-directory (cadr destination))
4557 ;; stderr is on the same remote host.
4558 (setq stderr (with-parsed-tramp-file-name
4559 (cadr destination) nil localname))
4560 ;; stderr must be copied to remote host. The temporary
4561 ;; file must be deleted after execution.
a6e96327
MA
4562 (setq stderr (tramp-make-tramp-temp-file v)
4563 tmpstderr (tramp-make-tramp-file-name
4564 method user host stderr))))
bede3e9f 4565 ;; stderr to be discarded.
00d6fd04
MA
4566 ((null (cadr destination))
4567 (setq stderr "/dev/null"))))
4568 ;; 't
4569 (destination
4570 (setq outbuf (current-buffer))))
4571 (when stderr (setq command (format "%s 2>%s" command stderr)))
4572
00d6fd04
MA
4573 ;; Send the command. It might not return in time, so we protect it.
4574 (condition-case nil
4575 (unwind-protect
293c24f9
MA
4576 (setq ret
4577 (tramp-send-command-and-check
4578 v (format "\\cd %s; %s"
4579 (tramp-shell-quote-argument localname)
b593f105
MA
4580 command)
4581 nil t))
00d6fd04
MA
4582 ;; We should show the output anyway.
4583 (when outbuf
293c24f9
MA
4584 (with-current-buffer outbuf
4585 (insert
4586 (with-current-buffer (tramp-get-connection-buffer v)
4587 (buffer-string))))
00d6fd04 4588 (when display (display-buffer outbuf))))
260821d3
MA
4589 ;; When the user did interrupt, we should do it also. We use
4590 ;; return code -1 as marker.
4591 (quit
4592 (kill-buffer (tramp-get-connection-buffer v))
4593 (setq ret -1))
4594 ;; Handle errors.
00d6fd04
MA
4595 (error
4596 (kill-buffer (tramp-get-connection-buffer v))
4597 (setq ret 1)))
a6e96327 4598
a6e96327
MA
4599 ;; Provide error file.
4600 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
263c02ef 4601
260821d3
MA
4602 ;; Cleanup. We remove all file cache values for the connection,
4603 ;; because the remote process could have changed them.
66bdc868 4604 (when tmpinput (tramp-compat-delete-file tmpinput 'force))
946a5aeb
MA
4605
4606 ;; `process-file-side-effects' has been introduced with GNU
4607 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4608 ;; by `program'. If it doesn't exist, we assume its default
4609 ;; value 't'.
4610 (unless (and (boundp 'process-file-side-effects)
4611 (not (symbol-value 'process-file-side-effects)))
4612 (tramp-flush-directory-property v ""))
4613
00d6fd04 4614 ;; Return exit status.
260821d3
MA
4615 (if (equal ret -1)
4616 (keyboard-quit)
4617 ret))))
00d6fd04 4618
a4aeb9a4
MA
4619(defun tramp-local-call-process
4620 (program &optional infile destination display &rest args)
4621 "Calls `call-process' on the local host.
4622This is needed because for some Emacs flavors Tramp has
4623defadviced `call-process' to behave like `process-file'. The
4624Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4625 (let ((default-directory
4626 (if (file-remote-p default-directory)
4627 (tramp-compat-temporary-file-directory)
4628 default-directory)))
4629 (if (executable-find program)
4630 (apply 'call-process program infile destination display args)
4631 1)))
4632
00d6fd04
MA
4633(defun tramp-handle-call-process-region
4634 (start end program &optional delete buffer display &rest args)
4635 "Like `call-process-region' for Tramp files."
258800f8 4636 (let ((tmpfile (tramp-compat-make-temp-file "")))
00d6fd04
MA
4637 (write-region start end tmpfile)
4638 (when delete (delete-region start end))
4639 (unwind-protect
4640 (apply 'call-process program tmpfile buffer display args)
66bdc868 4641 (tramp-compat-delete-file tmpfile 'force))))
00d6fd04
MA
4642
4643(defun tramp-handle-shell-command
4644 (command &optional output-buffer error-buffer)
4645 "Like `shell-command' for Tramp files."
ce3f516f 4646 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
b8bfcf96
MA
4647 ;; We cannot use `shell-file-name' and `shell-command-switch',
4648 ;; they are variables of the local host.
4649 (args (list "/bin/sh" "-c" (substring command 0 asynchronous)))
5d2ebd96 4650 current-buffer-p
ce3f516f 4651 (output-buffer
27e813fe
MA
4652 (cond
4653 ((bufferp output-buffer) output-buffer)
4654 ((stringp output-buffer) (get-buffer-create output-buffer))
5d2ebd96
AS
4655 (output-buffer
4656 (setq current-buffer-p t)
4657 (current-buffer))
42bc9b6d 4658 (t (get-buffer-create
27e813fe
MA
4659 (if asynchronous
4660 "*Async Shell Command*"
4661 "*Shell Command Output*")))))
4662 (error-buffer
4663 (cond
4664 ((bufferp error-buffer) error-buffer)
4665 ((stringp error-buffer) (get-buffer-create error-buffer))))
ce3f516f 4666 (buffer
27e813fe 4667 (if (and (not asynchronous) error-buffer)
ce3f516f
MA
4668 (with-parsed-tramp-file-name default-directory nil
4669 (list output-buffer (tramp-make-tramp-temp-file v)))
42bc9b6d 4670 output-buffer))
cfb5c0db 4671 (p (get-buffer-process output-buffer)))
42bc9b6d
MA
4672
4673 ;; Check whether there is another process running. Tramp does not
4674 ;; support 2 (asynchronous) processes in parallel.
cfb5c0db 4675 (when p
42bc9b6d 4676 (if (yes-or-no-p "A command is running. Kill it? ")
699a11fb
GM
4677 (condition-case nil
4678 (kill-process p)
4679 (error nil))
42bc9b6d
MA
4680 (error "Shell command in progress")))
4681
f34db316
AS
4682 (if current-buffer-p
4683 (progn
4684 (barf-if-buffer-read-only)
4685 (push-mark nil t))
5d2ebd96
AS
4686 (with-current-buffer output-buffer
4687 (setq buffer-read-only nil)
4688 (erase-buffer)))
42bc9b6d 4689
5d2ebd96 4690 (if (and (not current-buffer-p) (integerp asynchronous))
42bc9b6d
MA
4691 (prog1
4692 ;; Run the process.
3412f35d 4693 (apply 'start-file-process "*Async Shell*" buffer args)
42bc9b6d 4694 ;; Display output.
cfb5c0db
MA
4695 (pop-to-buffer output-buffer)
4696 (setq mode-line-process '(":%s"))
4697 (require 'shell) (shell-mode))
42bc9b6d
MA
4698
4699 (prog1
4700 ;; Run the process.
4701 (apply 'process-file (car args) nil buffer nil (cdr args))
4702 ;; Insert error messages if they were separated.
4703 (when (listp buffer)
4704 (with-current-buffer error-buffer
4705 (insert-file-contents (cadr buffer)))
66bdc868 4706 (tramp-compat-delete-file (cadr buffer) 'force))
f34db316
AS
4707 (if current-buffer-p
4708 ;; This is like exchange-point-and-mark, but doesn't
4709 ;; activate the mark. It is cleaner to avoid activation,
4710 ;; even though the command loop would deactivate the mark
4711 ;; because we inserted text.
4712 (goto-char (prog1 (mark t)
4713 (set-marker (mark-marker) (point)
4714 (current-buffer))))
4715 ;; There's some output, display it.
4716 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4717 (if (functionp 'display-message-or-buffer)
0d5852cf 4718 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
f34db316 4719 (pop-to-buffer output-buffer))))))))
00d6fd04
MA
4720
4721;; File Editing.
4722
4723(defvar tramp-handle-file-local-copy-hook nil
4724 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4725
fb7933a3 4726(defun tramp-handle-file-local-copy (filename)
00d6fd04 4727 "Like `file-local-copy' for Tramp files."
0f205eee 4728
c62c9d08 4729 (with-parsed-tramp-file-name filename nil
2988341a
MA
4730 (unless (file-exists-p filename)
4731 (tramp-error
4732 v 'file-error
4733 "Cannot make local copy of non-existing file `%s'" filename))
4734
6a29a838
MA
4735 (let* ((size (nth 7 (file-attributes filename)))
4736 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
4737 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
4738 (tmpfile (tramp-compat-make-temp-file filename)))
5ec2cc41 4739
2988341a
MA
4740 (condition-case err
4741 (cond
4742 ;; `copy-file' handles direct copy and out-of-band methods.
4743 ((or (tramp-local-host-p v)
6a29a838 4744 (tramp-method-out-of-band-p v size))
2988341a
MA
4745 (copy-file filename tmpfile t t))
4746
4747 ;; Use inline encoding for file transfer.
4748 (rem-enc
4749 (save-excursion
4750 (tramp-message v 5 "Encoding remote file %s..." filename)
4751 (tramp-barf-unless-okay
6a29a838 4752 v (format rem-enc (tramp-shell-quote-argument localname))
2988341a
MA
4753 "Encoding remote file failed")
4754 (tramp-message v 5 "Encoding remote file %s...done" filename)
4755
6a29a838 4756 (if (functionp loc-dec)
2988341a
MA
4757 ;; If local decoding is a function, we call it. We
4758 ;; must disable multibyte, because
4759 ;; `uudecode-decode-region' doesn't handle it
4760 ;; correctly.
0f205eee
MA
4761 (with-temp-buffer
4762 (set-buffer-multibyte nil)
4763 (insert-buffer-substring (tramp-get-buffer v))
4764 (tramp-message
4765 v 5 "Decoding remote file %s with function %s..."
4766 filename loc-dec)
4767 (funcall loc-dec (point-min) (point-max))
aa485f7c
MA
4768 ;; Unset `file-name-handler-alist'. Otherwise,
4769 ;; epa-file gets confused.
4770 (let (file-name-handler-alist
4771 (coding-system-for-write 'binary))
2988341a
MA
4772 (write-region (point-min) (point-max) tmpfile)))
4773
4774 ;; If tramp-decoding-function is not defined for this
4775 ;; method, we invoke tramp-decoding-command instead.
4776 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
aa485f7c
MA
4777 ;; Unset `file-name-handler-alist'. Otherwise,
4778 ;; epa-file gets confused.
4779 (let (file-name-handler-alist
4780 (coding-system-for-write 'binary))
2988341a
MA
4781 (write-region (point-min) (point-max) tmpfile2))
4782 (tramp-message
4783 v 5 "Decoding remote file %s with command %s..."
4784 filename loc-dec)
4785 (unwind-protect
4786 (tramp-call-local-coding-command loc-dec tmpfile2 tmpfile)
66bdc868 4787 (tramp-compat-delete-file tmpfile2 'force))))
2988341a
MA
4788
4789 (tramp-message v 5 "Decoding remote file %s...done" filename)
4790 ;; Set proper permissions.
b86c1cd8 4791 (set-file-modes tmpfile (tramp-default-file-modes filename))
2988341a
MA
4792 ;; Set local user ownership.
4793 (tramp-set-file-uid-gid tmpfile)))
4794
4795 ;; Oops, I don't know what to do.
4796 (t (tramp-error
4797 v 'file-error "Wrong method specification for `%s'" method)))
4798
4799 ;; Error handling.
4800 ((error quit)
66bdc868 4801 (tramp-compat-delete-file tmpfile 'force)
2988341a 4802 (signal (car err) (cdr err))))
0f205eee 4803
00d6fd04 4804 (run-hooks 'tramp-handle-file-local-copy-hook)
94be87e8 4805 tmpfile)))
fb7933a3 4806
bce04fee 4807(defun tramp-handle-file-remote-p (filename &optional identification connected)
00d6fd04 4808 "Like `file-remote-p' for Tramp files."
d5b3979c
MA
4809 (let ((tramp-verbose 3))
4810 (when (tramp-tramp-file-p filename)
4811 (let* ((v (tramp-dissect-file-name filename))
4812 (p (tramp-get-connection-process v))
4813 (c (and p (processp p) (memq (process-status p) '(run open)))))
4814 ;; We expand the file name only, if there is already a connection.
4815 (with-parsed-tramp-file-name
4816 (if c (expand-file-name filename) filename) nil
4817 (and (or (not connected) c)
4818 (cond
4819 ((eq identification 'method) method)
4820 ((eq identification 'user) user)
4821 ((eq identification 'host) host)
4822 ((eq identification 'localname) localname)
4823 (t (tramp-make-tramp-file-name method user host "")))))))))
fb7933a3 4824
eb562962
MA
4825(defun tramp-find-file-name-coding-system-alist (filename tmpname)
4826 "Like `find-operation-coding-system' for Tramp filenames.
4827Tramp's `insert-file-contents' and `write-region' work over
4828temporary file names. If `file-coding-system-alist' contains an
4829expression, which matches more than the file name suffix, the
4830coding system might not be determined. This function repairs it."
4831 (let (result)
4832 (dolist (elt file-coding-system-alist result)
4833 (when (and (consp elt) (string-match (car elt) filename))
4834 ;; We found a matching entry in `file-coding-system-alist'.
4835 ;; So we add a similar entry, but with the temporary file name
4836 ;; as regexp.
4837 (add-to-list
4838 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
4839
fb7933a3
KG
4840(defun tramp-handle-insert-file-contents
4841 (filename &optional visit beg end replace)
00d6fd04 4842 "Like `insert-file-contents' for Tramp files."
fb7933a3
KG
4843 (barf-if-buffer-read-only)
4844 (setq filename (expand-file-name filename))
736ac90f 4845 (let (coding-system-used result local-copy remote-copy)
2ac33804
MA
4846 (with-parsed-tramp-file-name filename nil
4847 (unwind-protect
70c11b0b
MA
4848 (if (not (file-exists-p filename))
4849 ;; We don't raise a Tramp error, because it might be
4850 ;; suppressed, like in `find-file-noselect-1'.
4851 (signal 'file-error
4852 (list "File not found on remote host" filename))
4853
4854 (if (and (tramp-local-host-p v)
4855 (let (file-name-handler-alist)
4856 (file-readable-p localname)))
4857 ;; Short track: if we are on the local host, we can
4858 ;; run directly.
4859 (setq result
4860 (tramp-run-real-handler
4861 'insert-file-contents
4862 (list localname visit beg end replace)))
4863
736ac90f
MA
4864 ;; When we shall insert only a part of the file, we copy
4865 ;; this part.
4866 (when (or beg end)
4867 (setq remote-copy (tramp-make-tramp-temp-file v))
4868 (tramp-send-command
4869 v
4870 (cond
4871 ((and beg end)
4872 (format "tail -c +%d %s | head -c +%d >%s"
4873 (1+ beg) (tramp-shell-quote-argument localname)
4874 (- end beg) remote-copy))
4875 (beg
4876 (format "tail -c +%d %s >%s"
4877 (1+ beg) (tramp-shell-quote-argument localname)
4878 remote-copy))
4879 (end
4880 (format "head -c +%d %s >%s"
4881 (1+ end) (tramp-shell-quote-argument localname)
4882 remote-copy)))))
4883
70c11b0b
MA
4884 ;; `insert-file-contents-literally' takes care to avoid
4885 ;; calling jka-compr. By let-binding
4886 ;; `inhibit-file-name-operation', we propagate that care
4887 ;; to the `file-local-copy' operation.
4888 (setq local-copy
4889 (let ((inhibit-file-name-operation
4890 (when (eq inhibit-file-name-operation
4891 'insert-file-contents)
4892 'file-local-copy)))
b88f2d0a
MA
4893 (cond
4894 ((stringp remote-copy)
4895 (file-local-copy
4896 (tramp-make-tramp-file-name
4897 method user host remote-copy)))
4898 ((stringp tramp-temp-buffer-file-name)
4899 (copy-file filename tramp-temp-buffer-file-name 'ok)
4900 tramp-temp-buffer-file-name)
4901 (t (file-local-copy filename)))))
4902
6e4f5731
MA
4903 ;; When the file is not readable for the owner, it
4904 ;; cannot be inserted, even it is redable for the group
4905 ;; or for everybody.
4906 (set-file-modes local-copy (tramp-octal-to-decimal "0600"))
4907
b88f2d0a
MA
4908 (when (and (null remote-copy)
4909 (tramp-get-method-parameter
4910 method 'tramp-copy-keep-tmpfile))
4911 ;; We keep the local file for performance reasons,
4912 ;; useful for "rsync".
b88f2d0a
MA
4913 (setq tramp-temp-buffer-file-name local-copy)
4914 (put 'tramp-temp-buffer-file-name 'permanent-local t))
4915
70c11b0b
MA
4916 (tramp-message
4917 v 4 "Inserting local temp file `%s'..." local-copy)
4918
4919 ;; We must ensure that `file-coding-system-alist'
4920 ;; matches `local-copy'.
4921 (let ((file-coding-system-alist
4922 (tramp-find-file-name-coding-system-alist
4923 filename local-copy)))
4924 (setq result
4925 (insert-file-contents
736ac90f 4926 local-copy nil nil nil replace))
70c11b0b
MA
4927 ;; Now `last-coding-system-used' has right value.
4928 ;; Remember it.
4929 (when (boundp 'last-coding-system-used)
4930 (setq coding-system-used
4931 (symbol-value 'last-coding-system-used))))
8d60099b 4932
70c11b0b
MA
4933 (tramp-message
4934 v 4 "Inserting local temp file `%s'...done" local-copy)
4935 (when (boundp 'last-coding-system-used)
2ac33804 4936 (set 'last-coding-system-used coding-system-used))))
70c11b0b 4937
2ac33804
MA
4938 ;; Save exit.
4939 (progn
4940 (when visit
4941 (setq buffer-file-name filename)
4942 (setq buffer-read-only (not (file-writable-p filename)))
4943 (set-visited-file-modtime)
4944 (set-buffer-modified-p nil))
b88f2d0a
MA
4945 (when (and (stringp local-copy)
4946 (or remote-copy (null tramp-temp-buffer-file-name)))
66bdc868 4947 (tramp-compat-delete-file local-copy 'force))
2ac33804 4948 (when (stringp remote-copy)
66bdc868
MA
4949 (tramp-compat-delete-file
4950 (tramp-make-tramp-file-name method user host remote-copy)
4951 'force)))))
70c11b0b
MA
4952
4953 ;; Result.
4954 (list (expand-file-name filename)
4955 (cadr result))))
fb7933a3 4956
94be87e8
MA
4957;; This is needed for XEmacs only. Code stolen from files.el.
4958(defun tramp-handle-insert-file-contents-literally
4959 (filename &optional visit beg end replace)
4960 "Like `insert-file-contents-literally' for Tramp files."
4961 (let ((format-alist nil)
4962 (after-insert-file-functions nil)
4963 (coding-system-for-read 'no-conversion)
4964 (coding-system-for-write 'no-conversion)
4965 (find-buffer-file-type-function
4966 (if (fboundp 'find-buffer-file-type)
4967 (symbol-function 'find-buffer-file-type)
4968 nil))
4969 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
4970 (inhibit-file-name-operation 'insert-file-contents))
4971 (unwind-protect
4972 (progn
4973 (fset 'find-buffer-file-type (lambda (filename) t))
4974 (insert-file-contents filename visit beg end replace))
70c11b0b 4975 ;; Save exit.
94be87e8
MA
4976 (if find-buffer-file-type-function
4977 (fset 'find-buffer-file-type find-buffer-file-type-function)
4978 (fmakunbound 'find-buffer-file-type)))))
4979
38c65fca 4980(defun tramp-handle-find-backup-file-name (filename)
00d6fd04 4981 "Like `find-backup-file-name' for Tramp files."
07dfe738
KG
4982 (with-parsed-tramp-file-name filename nil
4983 ;; We set both variables. It doesn't matter whether it is
b533bc97 4984 ;; Emacs or XEmacs.
07dfe738 4985 (let ((backup-directory-alist
b533bc97 4986 ;; Emacs case.
07dfe738 4987 (when (boundp 'backup-directory-alist)
b86c1cd8 4988 (if (symbol-value 'tramp-backup-directory-alist)
07dfe738 4989 (mapcar
aa485f7c
MA
4990 (lambda (x)
4991 (cons
4992 (car x)
4993 (if (and (stringp (cdr x))
4994 (file-name-absolute-p (cdr x))
4995 (not (tramp-file-name-p (cdr x))))
4996 (tramp-make-tramp-file-name method user host (cdr x))
4997 (cdr x))))
07dfe738
KG
4998 (symbol-value 'tramp-backup-directory-alist))
4999 (symbol-value 'backup-directory-alist))))
5000
5001 (bkup-backup-directory-info
b533bc97 5002 ;; XEmacs case.
07dfe738 5003 (when (boundp 'bkup-backup-directory-info)
b86c1cd8 5004 (if (symbol-value 'tramp-bkup-backup-directory-info)
07dfe738 5005 (mapcar
aa485f7c
MA
5006 (lambda (x)
5007 (nconc
5008 (list (car x))
5009 (list
5010 (if (and (stringp (car (cdr x)))
5011 (file-name-absolute-p (car (cdr x)))
5012 (not (tramp-file-name-p (car (cdr x)))))
5013 (tramp-make-tramp-file-name
5014 method user host (car (cdr x)))
5015 (car (cdr x))))
5016 (cdr (cdr x))))
07dfe738
KG
5017 (symbol-value 'tramp-bkup-backup-directory-info))
5018 (symbol-value 'bkup-backup-directory-info)))))
5019
5020 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
38c65fca 5021
c1105d05 5022(defun tramp-handle-make-auto-save-file-name ()
00d6fd04 5023 "Like `make-auto-save-file-name' for Tramp files.
c1105d05 5024Returns a file name in `tramp-auto-save-directory' for autosaving this file."
00d6fd04
MA
5025 (let ((tramp-auto-save-directory tramp-auto-save-directory)
5026 (buffer-file-name
5027 (tramp-subst-strs-in-string
5028 '(("_" . "|")
5029 ("/" . "_a")
5030 (":" . "_b")
5031 ("|" . "__")
5032 ("[" . "_l")
5033 ("]" . "_r"))
5034 (buffer-file-name))))
1a762140
MA
5035 ;; File name must be unique. This is ensured with Emacs 22 (see
5036 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5037 ;; all other cases we must do it ourselves.
5038 (when (boundp 'auto-save-file-name-transforms)
9e6ab520 5039 (mapc
aa485f7c
MA
5040 (lambda (x)
5041 (when (and (string-match (car x) buffer-file-name)
5042 (not (car (cddr x))))
5043 (setq tramp-auto-save-directory
5044 (or tramp-auto-save-directory
5045 (tramp-compat-temporary-file-directory)))))
1a762140
MA
5046 (symbol-value 'auto-save-file-name-transforms)))
5047 ;; Create directory.
5048 (when tramp-auto-save-directory
00d6fd04
MA
5049 (setq buffer-file-name
5050 (expand-file-name buffer-file-name tramp-auto-save-directory))
1a762140
MA
5051 (unless (file-exists-p tramp-auto-save-directory)
5052 (make-directory tramp-auto-save-directory t)))
00d6fd04
MA
5053 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5054 ;; it is not a magic file name operation (since Emacs 22).
5055 ;; We must deactivate it temporarily.
5056 (if (not (ad-is-active 'make-auto-save-file-name))
5057 (tramp-run-real-handler 'make-auto-save-file-name nil)
5058 ;; else
5059 (ad-deactivate 'make-auto-save-file-name)
5060 (prog1
5061 (tramp-run-real-handler 'make-auto-save-file-name nil)
5062 (ad-activate 'make-auto-save-file-name)))))
5063
5064(defvar tramp-handle-write-region-hook nil
5065 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5066
b88f2d0a 5067;; CCC grok LOCKNAME
fb7933a3
KG
5068(defun tramp-handle-write-region
5069 (start end filename &optional append visit lockname confirm)
00d6fd04 5070 "Like `write-region' for Tramp files."
fb7933a3 5071 (setq filename (expand-file-name filename))
c62c9d08 5072 (with-parsed-tramp-file-name filename nil
00d6fd04
MA
5073 ;; Following part commented out because we don't know what to do about
5074 ;; file locking, and it does not appear to be a problem to ignore it.
5075 ;; Ange-ftp ignores it, too.
5076 ;; (when (and lockname (stringp lockname))
5077 ;; (setq lockname (expand-file-name lockname)))
5078 ;; (unless (or (eq lockname nil)
5079 ;; (string= lockname filename))
5080 ;; (error
5081 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
8d60099b 5082
94be87e8 5083 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
00d6fd04
MA
5084 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
5085 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
5086 (tramp-error v 'file-error "File not overwritten")))
8d60099b 5087
a4aeb9a4 5088 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
9c13938d 5089 (tramp-get-remote-uid v 'integer)))
a4aeb9a4 5090 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
9c13938d
MA
5091 (tramp-get-remote-gid v 'integer))))
5092
5093 (if (and (tramp-local-host-p v)
93c3eb7c 5094 ;; `file-writable-p' calls `file-expand-file-name'. We
87bdd2c7
MA
5095 ;; cannot use `tramp-run-real-handler' therefore.
5096 (let (file-name-handler-alist)
82f3844e
MA
5097 (and
5098 (file-writable-p (file-name-directory localname))
5099 (or (file-directory-p localname)
5100 (file-writable-p localname)))))
9c13938d 5101 ;; Short track: if we are on the local host, we can run directly.
aac0b0f2
MA
5102 (tramp-run-real-handler
5103 'write-region
5104 (list start end localname append 'no-message lockname confirm))
9c13938d 5105
6a29a838 5106 (let ((modes (save-excursion (tramp-default-file-modes filename)))
9c13938d 5107 ;; We use this to save the value of
6a29a838
MA
5108 ;; `last-coding-system-used' after writing the tmp
5109 ;; file. At the end of the function, we set
9c13938d
MA
5110 ;; `last-coding-system-used' to this saved value. This
5111 ;; way, any intermediary coding systems used while
5112 ;; talking to the remote shell or suchlike won't hose
5113 ;; this variable. This approach was snarfed from
5114 ;; ange-ftp.el.
5115 coding-system-used
5116 ;; Write region into a tmp file. This isn't really
5117 ;; needed if we use an encoding function, but currently
5118 ;; we use it always because this makes the logic
293c24f9
MA
5119 ;; simpler.
5120 (tmpfile (or tramp-temp-buffer-file-name
5121 (tramp-compat-make-temp-file filename))))
5122
5123 ;; If `append' is non-nil, we copy the file locally, and let
5124 ;; the native `write-region' implementation do the job.
5125 (when append (copy-file filename tmpfile 'ok))
9c13938d
MA
5126
5127 ;; We say `no-message' here because we don't want the
5128 ;; visited file modtime data to be clobbered from the temp
5129 ;; file. We call `set-visited-file-modtime' ourselves later
eb562962
MA
5130 ;; on. We must ensure that `file-coding-system-alist'
5131 ;; matches `tmpfile'.
6a29a838
MA
5132 (let (file-name-handler-alist
5133 (file-coding-system-alist
eb562962 5134 (tramp-find-file-name-coding-system-alist filename tmpfile)))
ce2cc728
MA
5135 (condition-case err
5136 (tramp-run-real-handler
5137 'write-region
5138 (list start end tmpfile append 'no-message lockname confirm))
2988341a 5139 ((error quit)
b88f2d0a 5140 (setq tramp-temp-buffer-file-name nil)
66bdc868 5141 (tramp-compat-delete-file tmpfile 'force)
2988341a 5142 (signal (car err) (cdr err))))
ce2cc728 5143
eb562962
MA
5144 ;; Now, `last-coding-system-used' has the right value. Remember it.
5145 (when (boundp 'last-coding-system-used)
5146 (setq coding-system-used
5147 (symbol-value 'last-coding-system-used))))
5148
9c13938d
MA
5149 ;; The permissions of the temporary file should be set. If
5150 ;; filename does not exist (eq modes nil) it has been
5151 ;; renamed to the backup file. This case `save-buffer'
5152 ;; handles permissions.
459a5f4b
MA
5153 ;; Ensure, that it is still readable.
5154 (when modes
5155 (set-file-modes
5156 tmpfile (logior (or modes 0) (tramp-octal-to-decimal "0400"))))
9c13938d
MA
5157
5158 ;; This is a bit lengthy due to the different methods
5159 ;; possible for file transfer. First, we check whether the
5160 ;; method uses an rcp program. If so, we call it.
5161 ;; Otherwise, both encoding and decoding command must be
5162 ;; specified. However, if the method _also_ specifies an
5163 ;; encoding function, then that is used for encoding the
5164 ;; contents of the tmp file.
6a29a838
MA
5165 (let* ((size (nth 7 (file-attributes tmpfile)))
5166 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
5167 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
5168 (cond
5169 ;; `copy-file' handles direct copy and out-of-band methods.
5170 ((or (tramp-local-host-p v)
5171 (tramp-method-out-of-band-p v size))
5172 (if (and (not (stringp start))
5173 (= (or end (point-max)) (point-max))
5174 (= (or start (point-min)) (point-min))
5175 (tramp-get-method-parameter
5176 method 'tramp-copy-keep-tmpfile))
5177 (progn
5178 (setq tramp-temp-buffer-file-name tmpfile)
5179 (condition-case err
5180 ;; We keep the local file for performance
5181 ;; reasons, useful for "rsync".
5182 (copy-file tmpfile filename t)
5183 ((error quit)
5184 (setq tramp-temp-buffer-file-name nil)
66bdc868 5185 (tramp-compat-delete-file tmpfile 'force)
6a29a838
MA
5186 (signal (car err) (cdr err)))))
5187 (setq tramp-temp-buffer-file-name nil)
5188 ;; Don't rename, in order to keep context in SELinux.
5189 (unwind-protect
5190 (copy-file tmpfile filename t)
66bdc868 5191 (tramp-compat-delete-file tmpfile 'force))))
6a29a838
MA
5192
5193 ;; Use inline file transfer.
5194 (rem-dec
5195 ;; Encode tmpfile.
5196 (tramp-message v 5 "Encoding region...")
191bb792 5197 (unwind-protect
6a29a838
MA
5198 (with-temp-buffer
5199 (set-buffer-multibyte nil)
5200 ;; Use encoding function or command.
5201 (if (functionp loc-enc)
5202 (progn
5203 (tramp-message
5204 v 5 "Encoding region using function `%s'..." loc-enc)
5205 (let ((coding-system-for-read 'binary))
5206 (insert-file-contents-literally tmpfile))
5207 ;; The following `let' is a workaround for the
5208 ;; base64.el that comes with pgnus-0.84. If
5209 ;; both of the following conditions are
5210 ;; satisfied, it tries to write to a local
5211 ;; file in default-directory, but at this
5212 ;; point, default-directory is remote.
5213 ;; (`call-process-region' can't write to
5214 ;; remote files, it seems.) The file in
5215 ;; question is a tmp file anyway.
5216 (let ((default-directory
5217 (tramp-compat-temporary-file-directory)))
5218 (funcall loc-enc (point-min) (point-max))))
5219
5220 (tramp-message
5221 v 5 "Encoding region using command `%s'..." loc-enc)
5222 (unless (zerop (tramp-call-local-coding-command
9c13938d 5223 loc-enc tmpfile t))
6a29a838
MA
5224 (tramp-error
5225 v 'file-error
5226 "Cannot write to `%s', local encoding command `%s' failed"
5227 filename loc-enc)))
5228
5229 ;; Send buffer into remote decoding command which
5230 ;; writes to remote file. Because this happens on
5231 ;; the remote host, we cannot use the function.
5232 (goto-char (point-max))
5233 (unless (bolp) (newline))
5234 (tramp-message
5235 v 5 "Decoding region into remote file %s..." filename)
5236 (tramp-send-command
5237 v
5238 (format
5239 (concat rem-dec " <<'EOF'\n%sEOF")
5240 (tramp-shell-quote-argument localname)
5241 (buffer-string)))
5242 (tramp-barf-unless-okay
5243 v nil
5244 "Couldn't write region to `%s', decode using `%s' failed"
5245 filename rem-dec)
5246 ;; When `file-precious-flag' is set, the region is
5247 ;; written to a temporary file. Check that the
5248 ;; checksum is equal to that from the local tmpfile.
5249 (when file-precious-flag
5250 (erase-buffer)
5251 (and
5252 ;; cksum runs locally, if possible.
5253 (zerop (tramp-local-call-process "cksum" tmpfile t))
5254 ;; cksum runs remotely.
5255 (zerop
5256 (tramp-send-command-and-check
5257 v
5258 (format
5259 "cksum <%s" (tramp-shell-quote-argument localname))))
5260 ;; ... they are different.
5261 (not
5262 (string-equal
5263 (buffer-string)
5264 (with-current-buffer (tramp-get-buffer v)
5265 (buffer-string))))
5266 (tramp-error
5267 v 'file-error
5268 (concat "Couldn't write region to `%s',"
5269 " decode using `%s' failed")
5270 filename rem-dec)))
5271 (tramp-message
5272 v 5 "Decoding region into remote file %s...done" filename))
8d60099b 5273
6a29a838 5274 ;; Save exit.
66bdc868 5275 (tramp-compat-delete-file tmpfile 'force)))
8d60099b 5276
6a29a838
MA
5277 ;; That's not expected.
5278 (t
5279 (tramp-error
5280 v 'file-error
5281 (concat "Method `%s' should specify both encoding and "
5282 "decoding command or an rcp program")
5283 method))))
258800f8 5284
9c13938d
MA
5285 ;; Make `last-coding-system-used' have the right value.
5286 (when coding-system-used
5287 (set 'last-coding-system-used coding-system-used))))
0f205eee 5288
aac0b0f2
MA
5289 (tramp-flush-file-property v (file-name-directory localname))
5290 (tramp-flush-file-property v localname)
5291
57671b72
MA
5292 ;; We must protect `last-coding-system-used', now we have set it
5293 ;; to its correct value.
293c24f9 5294 (let (last-coding-system-used (need-chown t))
57671b72
MA
5295 ;; Set file modification time.
5296 (when (or (eq visit t) (stringp visit))
293c24f9
MA
5297 (let ((file-attr (file-attributes filename)))
5298 (set-visited-file-modtime
5299 ;; We must pass modtime explicitely, because filename can
5300 ;; be different from (buffer-file-name), f.e. if
5301 ;; `file-precious-flag' is set.
5302 (nth 5 file-attr))
5303 (when (and (eq (nth 2 file-attr) uid)
5304 (eq (nth 3 file-attr) gid))
5305 (setq need-chown nil))))
57671b72
MA
5306
5307 ;; Set the ownership.
293c24f9
MA
5308 (when need-chown
5309 (tramp-set-file-uid-gid filename uid gid))
57671b72
MA
5310 (when (or (eq visit t) (null visit) (stringp visit))
5311 (tramp-message v 0 "Wrote %s" filename))
5312 (run-hooks 'tramp-handle-write-region-hook)))))
fb7933a3 5313
946a5aeb
MA
5314(defvar tramp-vc-registered-file-names nil
5315 "List used to collect file names, which are checked during `vc-registered'.")
5316
5317;; VC backends check for the existence of various different special
5318;; files. This is very time consuming, because every single check
5319;; requires a remote command (the file cache must be invalidated).
5320;; Therefore, we apply a kind of optimization. We install the file
5321;; name handler `tramp-vc-file-name-handler', which does nothing but
5322;; remembers all file names for which `file-exists-p' or
5323;; `file-readable-p' has been applied. A first run of `vc-registered'
5324;; is performed. Afterwards, a script is applied for all collected
5325;; file names, using just one remote command. The result of this
5326;; script is used to fill the file cache with actual values. Now we
5327;; can reset the file name handlers, and we make a second run of
5328;; `vc-registered', which returns the expected result without sending
5329;; any other remote command.
49096407
MA
5330(defun tramp-handle-vc-registered (file)
5331 "Like `vc-registered' for Tramp files."
946a5aeb 5332 (with-parsed-tramp-file-name file nil
7f49fe46
MA
5333
5334 ;; There could be new files, created by the vc backend. We cannot
5335 ;; reuse the old cache entries, therefore.
946a5aeb
MA
5336 (let (tramp-vc-registered-file-names
5337 (tramp-cache-inhibit-cache (current-time))
5338 (file-name-handler-alist
5339 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5340
5341 ;; Here we collect only file names, which need an operation.
5342 (tramp-run-real-handler 'vc-registered (list file))
5343 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5344
5345 ;; Send just one command, in order to fill the cache.
7f49fe46
MA
5346 (when tramp-vc-registered-file-names
5347 (tramp-maybe-send-script
5348 v
5349 (format tramp-vc-registered-read-file-names
5350 (tramp-get-file-exists-command v)
5351 (format "%s -r" (tramp-get-test-command v)))
5352 "tramp_vc_registered_read_file_names")
5353
5354 (dolist
5355 (elt
5356 (tramp-send-command-and-read
5357 v
5358 (format
5359 "tramp_vc_registered_read_file_names %s"
5360 (mapconcat 'tramp-shell-quote-argument
5361 tramp-vc-registered-file-names
5362 " "))))
946a5aeb 5363
7f49fe46 5364 (tramp-set-file-property v (car elt) (cadr elt) (cadr (cdr elt))))))
946a5aeb 5365
7f49fe46
MA
5366 ;; Second run. Now all `file-exists-p' or `file-readable-p' calls
5367 ;; shall be answered from the file cache.
5368 ;; We unset `process-file-side-effects' in order to keep the cache
5369 ;; when `process-file' calls appear.
946a5aeb
MA
5370 (let (process-file-side-effects)
5371 (tramp-run-real-handler 'vc-registered (list file)))))
49096407 5372
a01b1e22
MA
5373;;;###autoload
5374(progn (defun tramp-run-real-handler (operation args)
fb7933a3 5375 "Invoke normal file name handler for OPERATION.
c62c9d08
KG
5376First arg specifies the OPERATION, second arg is a list of arguments to
5377pass to the OPERATION."
4007ba5b
KG
5378 (let* ((inhibit-file-name-handlers
5379 `(tramp-file-name-handler
946a5aeb 5380 tramp-vc-file-name-handler
4007ba5b
KG
5381 tramp-completion-file-name-handler
5382 cygwin-mount-name-hook-function
5383 cygwin-mount-map-drive-hook-function
5384 .
5385 ,(and (eq inhibit-file-name-operation operation)
5386 inhibit-file-name-handlers)))
5387 (inhibit-file-name-operation operation))
a01b1e22 5388 (apply operation args))))
16674e4f 5389
a01b1e22
MA
5390;;;###autoload
5391(progn (defun tramp-completion-run-real-handler (operation args)
16674e4f
KG
5392 "Invoke `tramp-file-name-handler' for OPERATION.
5393First arg specifies the OPERATION, second arg is a list of arguments to
5394pass to the OPERATION."
4007ba5b
KG
5395 (let* ((inhibit-file-name-handlers
5396 `(tramp-completion-file-name-handler
5397 cygwin-mount-name-hook-function
5398 cygwin-mount-map-drive-hook-function
5399 .
5400 ,(and (eq inhibit-file-name-operation operation)
5401 inhibit-file-name-handlers)))
5402 (inhibit-file-name-operation operation))
a01b1e22 5403 (apply operation args))))
fb7933a3 5404
4007ba5b
KG
5405;; We handle here all file primitives. Most of them have the file
5406;; name as first parameter; nevertheless we check for them explicitly
04bf5b65 5407;; in order to be signaled if a new primitive appears. This
4007ba5b
KG
5408;; scenario is needed because there isn't a way to decide by
5409;; syntactical means whether a foreign method must be called. It would
19a87064 5410;; ease the life if `file-name-handler-alist' would support a decision
4007ba5b
KG
5411;; function as well but regexp only.
5412(defun tramp-file-name-for-operation (operation &rest args)
5413 "Return file name related to OPERATION file primitive.
5414ARGS are the arguments OPERATION has been called with."
5415 (cond
b533bc97 5416 ;; FILE resp DIRECTORY.
4007ba5b
KG
5417 ((member operation
5418 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
5419 'delete-file 'diff-latest-backup-file 'directory-file-name
5420 'directory-files 'directory-files-and-attributes
5421 'dired-compress-file 'dired-uncache
5422 'file-accessible-directory-p 'file-attributes
5423 'file-directory-p 'file-executable-p 'file-exists-p
25f14cdb 5424 'file-local-copy 'file-remote-p 'file-modes
19a87064
MA
5425 'file-name-as-directory 'file-name-directory
5426 'file-name-nondirectory 'file-name-sans-versions
5427 'file-ownership-preserved-p 'file-readable-p
5428 'file-regular-p 'file-symlink-p 'file-truename
5429 'file-writable-p 'find-backup-file-name 'find-file-noselect
5430 'get-file-buffer 'insert-directory 'insert-file-contents
5431 'load 'make-directory 'make-directory-internal
5432 'set-file-modes 'substitute-in-file-name
5433 'unhandled-file-name-directory 'vc-registered
b533bc97 5434 ;; Emacs 22+ only.
ce3f516f 5435 'set-file-times
25f14cdb
MA
5436 ;; Emacs 24+ only.
5437 'file-selinux-context 'set-file-selinux-context
b533bc97 5438 ;; XEmacs only.
4007ba5b
KG
5439 'abbreviate-file-name 'create-file-buffer
5440 'dired-file-modtime 'dired-make-compressed-filename
5441 'dired-recursive-delete-directory 'dired-set-file-modtime
5442 'dired-shell-unhandle-file-name 'dired-uucode-file
5615d63f 5443 'insert-file-contents-literally 'make-temp-name 'recover-file
4007ba5b 5444 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
8daea7fc
KG
5445 (if (file-name-absolute-p (nth 0 args))
5446 (nth 0 args)
5447 (expand-file-name (nth 0 args))))
b533bc97 5448 ;; FILE DIRECTORY resp FILE1 FILE2.
4007ba5b
KG
5449 ((member operation
5450 (list 'add-name-to-file 'copy-file 'expand-file-name
5451 'file-name-all-completions 'file-name-completion
5452 'file-newer-than-file-p 'make-symbolic-link 'rename-file
b533bc97 5453 ;; Emacs 23+ only.
263c02ef 5454 'copy-directory
b533bc97 5455 ;; XEmacs only.
4007ba5b
KG
5456 'dired-make-relative-symlink
5457 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
5458 (save-match-data
5459 (cond
5460 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
5461 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
5462 (t (buffer-file-name (current-buffer))))))
b533bc97 5463 ;; START END FILE.
4007ba5b
KG
5464 ((eq operation 'write-region)
5465 (nth 2 args))
b533bc97 5466 ;; BUFFER.
4007ba5b 5467 ((member operation
00d6fd04 5468 (list 'set-visited-file-modtime 'verify-visited-file-modtime
b533bc97 5469 ;; Emacs 22+ only.
00d6fd04 5470 'make-auto-save-file-name
b533bc97 5471 ;; XEmacs only.
4007ba5b
KG
5472 'backup-buffer))
5473 (buffer-file-name
5474 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
b533bc97 5475 ;; COMMAND.
4007ba5b 5476 ((member operation
b533bc97 5477 (list ;; not in Emacs 23+.
00d6fd04 5478 'dired-call-process
b533bc97 5479 ;; Emacs only.
b71c9e75 5480 'shell-command
b533bc97 5481 ;; Emacs 22+ only.
0457dd55 5482 'process-file
b533bc97 5483 ;; Emacs 23+ only.
00d6fd04 5484 'start-file-process
b533bc97 5485 ;; XEmacs only.
00d6fd04 5486 'dired-print-file 'dired-shell-call-process
b533bc97 5487 ;; nowhere yet.
00d6fd04 5488 'executable-find 'start-process 'call-process))
4007ba5b 5489 default-directory)
b533bc97 5490 ;; Unknown file primitive.
4007ba5b
KG
5491 (t (error "unknown file I/O primitive: %s" operation))))
5492
5493(defun tramp-find-foreign-file-name-handler (filename)
5494 "Return foreign file name handler if exists."
b533bc97 5495 (when (tramp-tramp-file-p filename)
9ce8462a
MA
5496 (let ((v (tramp-dissect-file-name filename t))
5497 (handler tramp-foreign-file-name-handler-alist)
5498 elt res)
5499 ;; When we are not fully sure that filename completion is safe,
5500 ;; we should not return a handler.
5501 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
1834b39f
MA
5502 (and (tramp-file-name-host v)
5503 (not (member (tramp-file-name-host v)
5504 (mapcar 'car tramp-methods))))
9ce8462a
MA
5505 (not (tramp-completion-mode-p)))
5506 (while handler
5507 (setq elt (car handler)
5508 handler (cdr handler))
5509 (when (funcall (car elt) filename)
5510 (setq handler nil
5511 res (cdr elt))))
5512 res))))
4007ba5b 5513
fb7933a3
KG
5514;; Main function.
5515;;;###autoload
5516(defun tramp-file-name-handler (operation &rest args)
ea9d1443 5517 "Invoke Tramp file name handler.
a4aeb9a4 5518Falls back to normal file name handler if no Tramp file name handler exists."
2e271195
MA
5519 (if tramp-mode
5520 (save-match-data
5521 (let* ((filename
5522 (tramp-replace-environment-variables
5523 (apply 'tramp-file-name-for-operation operation args)))
5524 (completion (tramp-completion-mode-p))
5525 (foreign (tramp-find-foreign-file-name-handler filename)))
5526 (with-parsed-tramp-file-name filename nil
4874f5e6
MA
5527 ;; Call the backend function.
5528 (if foreign
5529 (condition-case err
5530 (apply foreign operation args)
5531 (error
5532 (cond
5533 ;; When we are in completion mode, some failed
5534 ;; operations shall return at least a default
5535 ;; value in order to give the user a chance to
5536 ;; correct the file name in the minibuffer.
5537 ((and completion (zerop (length localname))
5538 (memq operation '(file-exists-p file-directory-p)))
5539 t)
5540 ((and completion (zerop (length localname))
5541 (memq operation
5542 '(expand-file-name file-name-as-directory)))
5543 filename)
5544 ;; Propagate the error.
5545 (t (signal (car err) (cdr err))))))
5546 ;; Nothing to do for us.
5547 (tramp-run-real-handler operation args)))))
5548
2e271195
MA
5549 ;; When `tramp-mode' is not enabled, we don't do anything.
5550 (tramp-run-real-handler operation args)))
fb7933a3 5551
07dfe738
KG
5552;; In Emacs, there is some concurrency due to timers. If a timer
5553;; interrupts Tramp and wishes to use the same connection buffer as
5554;; the "main" Emacs, then garbage might occur in the connection
5555;; buffer. Therefore, we need to make sure that a timer does not use
5556;; the same connection buffer as the "main" Emacs. We implement a
5557;; cheap global lock, instead of locking each connection buffer
5558;; separately. The global lock is based on two variables,
5559;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5560;; (with setq) to indicate a lock. But Tramp also calls itself during
5561;; processing of a single file operation, so we need to allow
5562;; recursive calls. That's where the `tramp-locker' variable comes in
5563;; -- it is let-bound to t during the execution of the current
5564;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5565;; then we should just proceed because we have been called
5566;; recursively. But if `tramp-locker' is nil, then we are a timer
5567;; interrupting the "main" Emacs, and then we signal an error.
5568
5569(defvar tramp-locked nil
5570 "If non-nil, then Tramp is currently busy.
5571Together with `tramp-locker', this implements a locking mechanism
5572preventing reentrant calls of Tramp.")
5573
5574(defvar tramp-locker nil
5575 "If non-nil, then a caller has locked Tramp.
5576Together with `tramp-locked', this implements a locking mechanism
5577preventing reentrant calls of Tramp.")
5578
ea9d1443
KG
5579(defun tramp-sh-file-name-handler (operation &rest args)
5580 "Invoke remote-shell Tramp file name handler.
5581Fall back to normal file name handler if no Tramp handler exists."
07dfe738 5582 (when (and tramp-locked (not tramp-locker))
11c71217 5583 (setq tramp-locked nil)
00d6fd04 5584 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
07dfe738
KG
5585 (let ((tl tramp-locked))
5586 (unwind-protect
5587 (progn
5588 (setq tramp-locked t)
5589 (let ((tramp-locker t))
5590 (save-match-data
5591 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5592 (if fn
5593 (apply (cdr fn) args)
5594 (tramp-run-real-handler operation args))))))
5595 (setq tramp-locked tl))))
ea9d1443 5596
946a5aeb
MA
5597(defun tramp-vc-file-name-handler (operation &rest args)
5598 "Invoke special file name handler, which collects files to be handled."
5599 (save-match-data
5600 (let ((filename
5601 (tramp-replace-environment-variables
5602 (apply 'tramp-file-name-for-operation operation args)))
5603 (fn (assoc operation tramp-file-name-handler-alist)))
5604 (with-parsed-tramp-file-name filename nil
5605 (cond
5606 ;; That's what we want: file names, for which checks are
5607 ;; applied. We assume, that VC uses only `file-exists-p' and
5608 ;; `file-readable-p' checks; otherwise we must extend the
5609 ;; list. We do not perform any action, but return nil, in
5610 ;; order to keep `vc-registered' running.
5611 ((and fn (memq operation '(file-exists-p file-readable-p)))
5612 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5613 nil)
5614 ;; Tramp file name handlers like `expand-file-name'. They
5615 ;; must still work.
5616 (fn
5617 (save-match-data (apply (cdr fn) args)))
5618 ;; Default file name handlers, we don't care.
5619 (t (tramp-run-real-handler operation args)))))))
5620
16674e4f 5621;;;###autoload
1ecc6145 5622(progn (defun tramp-completion-file-name-handler (operation &rest args)
a4aeb9a4
MA
5623 "Invoke Tramp file name completion handler.
5624Falls back to normal file name handler if no Tramp file name handler exists."
57671b72
MA
5625 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5626 ;; would otherwise use backslash.
aff67808
MA
5627 (let ((directory-sep-char ?/)
5628 (fn (assoc operation tramp-completion-file-name-handler-alist)))
aa485f7c
MA
5629 (if (and
5630 ;; When `tramp-mode' is not enabled, we don't do anything.
5631 fn tramp-mode
5632 ;; For other syntaxes than `sep', the regexp matches many common
5633 ;; situations where the user doesn't actually want to use Tramp.
5634 ;; So to avoid autoloading Tramp after typing just "/s", we
5635 ;; disable this part of the completion, unless the user implicitly
5636 ;; indicated his interest in using a fancier completion system.
5637 (or (eq tramp-syntax 'sep)
a94d821f
MA
5638 (featurep 'tramp) ;; If it's loaded, we may as well use it.
5639 ;; `partial-completion-mode' does not exist in XEmacs.
5640 ;; It is obsoleted with Emacs 24.1.
0d5852cf
MA
5641 (and (boundp 'partial-completion-mode)
5642 (symbol-value 'partial-completion-mode))
aa485f7c
MA
5643 ;; FIXME: These may have been loaded even if the user never
5644 ;; intended to use them.
5645 (featurep 'ido)
5646 (featurep 'icicles)))
aff67808
MA
5647 (save-match-data (apply (cdr fn) args))
5648 (tramp-completion-run-real-handler operation args)))))
a01b1e22 5649
b25a52cc 5650;;;###autoload
aa485f7c
MA
5651(progn (defun tramp-register-file-name-handlers ()
5652 "Add Tramp file name handlers to `file-name-handler-alist'."
5653 ;; Remove autoloaded handlers from file name handler alist. Useful,
00d6fd04
MA
5654 ;; if `tramp-syntax' has been changed.
5655 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
aa485f7c
MA
5656 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5657 (let ((a1 (rassq
5658 'tramp-completion-file-name-handler file-name-handler-alist)))
5659 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5660 ;; Add the handlers.
a01b1e22
MA
5661 (add-to-list 'file-name-handler-alist
5662 (cons tramp-file-name-regexp 'tramp-file-name-handler))
0c0b61f1 5663 (put 'tramp-file-name-handler 'safe-magic t)
aa485f7c
MA
5664 (add-to-list 'file-name-handler-alist
5665 (cons tramp-completion-file-name-regexp
5666 'tramp-completion-file-name-handler))
5667 (put 'tramp-completion-file-name-handler 'safe-magic t)
5668 ;; If jka-compr or epa-file are already loaded, move them to the
5669 ;; front of `file-name-handler-alist'.
5670 (dolist (fnh '(epa-file-handler jka-compr-handler))
5671 (let ((entry (rassoc fnh file-name-handler-alist)))
5672 (when entry
5673 (setq file-name-handler-alist
5674 (cons entry (delete entry file-name-handler-alist))))))))
69cee873 5675
00d6fd04
MA
5676;; `tramp-file-name-handler' must be registered before evaluation of
5677;; site-start and init files, because there might exist remote files
5678;; already, f.e. files kept via recentf-mode.
aa485f7c
MA
5679;;;###autoload(tramp-register-file-name-handlers)
5680(tramp-register-file-name-handlers)
b25a52cc 5681
fb7933a3 5682;;;###autoload
8c04e197 5683(defun tramp-unload-file-name-handlers ()
a69c01a0
MA
5684 (setq file-name-handler-alist
5685 (delete (rassoc 'tramp-file-name-handler
5686 file-name-handler-alist)
5687 (delete (rassoc 'tramp-completion-file-name-handler
5688 file-name-handler-alist)
5689 file-name-handler-alist))))
5690
8c04e197 5691(add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
a69c01a0 5692
0664ff72 5693;;; File name handler functions for completion mode:
a6e96327
MA
5694
5695(defvar tramp-completion-mode nil
5696 "If non-nil, external packages signal that they are in file name completion.
5697
5698This is necessary, because Tramp uses a heuristic depending on last
5699input event. This fails when external packages use other characters
5700but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5701should never be set globally, the intention is to let-bind it.")
16674e4f
KG
5702
5703;; Necessary because `tramp-file-name-regexp-unified' and
00d6fd04
MA
5704;; `tramp-completion-file-name-regexp-unified' aren't different. If
5705;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5706;; to `tramp-file-name-handler'). Otherwise, it takes
5707;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5708;; risky, because completing a file might require loading other files,
5709;; like "~/.netrc", and for them it shouldn't be decided based on that
5710;; variable. On the other hand, those files shouldn't have partial
a4aeb9a4
MA
5711;; Tramp file name syntax. Maybe another variable should be introduced
5712;; overwriting this check in such cases. Or we change Tramp file name
00d6fd04 5713;; syntax in order to avoid ambiguities, like in XEmacs ...
6c4e47fa 5714(defun tramp-completion-mode-p ()
a94d821f 5715 "Check, whether method / user name / host name completion is active."
6c4e47fa 5716 (or
5f2b693f
MA
5717 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
5718 (and (boundp 'non-essential) (symbol-value 'non-essential))
a6e96327
MA
5719 tramp-completion-mode
5720 ;; Emacs.
94be87e8 5721 (equal last-input-event 'tab)
6c4e47fa 5722 (and (natnump last-input-event)
94be87e8 5723 (or
a6e96327 5724 ;; ?\t has event-modifier 'control.
800a97b8 5725 (equal last-input-event ?\t)
94be87e8 5726 (and (not (event-modifiers last-input-event))
800a97b8
SM
5727 (or (equal last-input-event ?\?)
5728 (equal last-input-event ?\ )))))
a6e96327 5729 ;; XEmacs.
6c4e47fa
MA
5730 (and (featurep 'xemacs)
5731 ;; `last-input-event' might be nil.
5732 (not (null last-input-event))
5733 ;; `last-input-event' may have no character approximation.
0d5852cf 5734 (tramp-compat-funcall 'event-to-character last-input-event)
94be87e8 5735 (or
a6e96327 5736 ;; ?\t has event-modifier 'control.
800a97b8 5737 (equal
0d5852cf 5738 (tramp-compat-funcall 'event-to-character last-input-event) ?\t)
94be87e8 5739 (and (not (event-modifiers last-input-event))
800a97b8 5740 (or (equal
0d5852cf
MA
5741 (tramp-compat-funcall 'event-to-character last-input-event)
5742 ?\?)
800a97b8 5743 (equal
0d5852cf
MA
5744 (tramp-compat-funcall 'event-to-character last-input-event)
5745 ?\ )))))))
16674e4f 5746
acd1f317
MA
5747(defun tramp-connectable-p (filename)
5748 "Check, whether it is possible to connect the remote host w/o side-effects.
5749This is true, if either the remote host is already connected, or if we are
5750not in completion mode."
5751 (and (tramp-tramp-file-p filename)
5752 (with-parsed-tramp-file-name filename nil
5753 (or (get-buffer (tramp-buffer-name v))
5754 (not (tramp-completion-mode-p))))))
5755
16674e4f
KG
5756;; Method, host name and user name completion.
5757;; `tramp-completion-dissect-file-name' returns a list of
5758;; tramp-file-name structures. For all of them we return possible completions.
a01b1e22 5759;;;###autoload
16674e4f 5760(defun tramp-completion-handle-file-name-all-completions (filename directory)
00d6fd04 5761 "Like `file-name-all-completions' for partial Tramp files."
16674e4f 5762
00d6fd04
MA
5763 (let* ((fullname (tramp-drop-volume-letter
5764 (expand-file-name filename directory)))
5765 ;; Possible completion structures.
5766 (v (tramp-completion-dissect-file-name fullname))
5767 result result1)
5768
5769 (while v
5770 (let* ((car (car v))
5771 (method (tramp-file-name-method car))
5772 (user (tramp-file-name-user car))
5773 (host (tramp-file-name-host car))
5774 (localname (tramp-file-name-localname car))
5775 (m (tramp-find-method method user host))
5776 (tramp-current-user user) ; see `tramp-parse-passwd'
5777 all-user-hosts)
5778
5779 (unless localname ;; Nothing to complete.
5780
5781 (if (or user host)
5782
5783 ;; Method dependent user / host combinations.
5784 (progn
9e6ab520 5785 (mapc
00d6fd04
MA
5786 (lambda (x)
5787 (setq all-user-hosts
5788 (append all-user-hosts
5789 (funcall (nth 0 x) (nth 1 x)))))
5790 (tramp-get-completion-function m))
5791
9e6ab520
MA
5792 (setq result
5793 (append result
5794 (mapcar
5795 (lambda (x)
5796 (tramp-get-completion-user-host
5797 method user host (nth 0 x) (nth 1 x)))
5798 (delq nil all-user-hosts)))))
00d6fd04
MA
5799
5800 ;; Possible methods.
5801 (setq result
5802 (append result (tramp-get-completion-methods m)))))
5803
5804 (setq v (cdr v))))
5805
5806 ;; Unify list, remove nil elements.
5807 (while result
5808 (let ((car (car result)))
5809 (when car
5810 (add-to-list
5811 'result1
5812 (substring car (length (tramp-drop-volume-letter directory)))))
5813 (setq result (cdr result))))
5814
5815 ;; Complete local parts.
5816 (append
5817 result1
5818 (condition-case nil
489fe4c2
MA
5819 (apply (if (tramp-connectable-p fullname)
5820 'tramp-completion-run-real-handler
5821 'tramp-run-real-handler)
5822 'file-name-all-completions (list (list filename directory)))
00d6fd04 5823 (error nil)))))
16674e4f
KG
5824
5825;; Method, host name and user name completion for a file.
a01b1e22 5826;;;###autoload
e1e17cae
MA
5827(defun tramp-completion-handle-file-name-completion
5828 (filename directory &optional predicate)
00d6fd04 5829 "Like `file-name-completion' for Tramp files."
e1e17cae
MA
5830 (try-completion
5831 filename
5832 (mapcar 'list (file-name-all-completions filename directory))
acd1f317
MA
5833 (when (and predicate
5834 (tramp-connectable-p (expand-file-name filename directory)))
83e20b5c 5835 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
16674e4f
KG
5836
5837;; I misuse a little bit the tramp-file-name structure in order to handle
5838;; completion possibilities for partial methods / user names / host names.
5839;; Return value is a list of tramp-file-name structures according to possible
00d6fd04 5840;; completions. If "localname" is non-nil it means there
16674e4f
KG
5841;; shouldn't be a completion anymore.
5842
5843;; Expected results:
5844
00d6fd04
MA
5845;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5846;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5847;; [nil "x" nil nil]
5848;; ["x" nil nil nil]
5849
5850;; "/x:" "/x:y" "/x:y:"
5851;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5852;; "/[x/" "/[x/y"
5853;; ["x" nil "" nil] ["x" nil "y" nil]
5854;; ["x" "" nil nil] ["x" "y" nil nil]
5855
5856;; "/x:y@" "/x:y@z" "/x:y@z:"
5857;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5858;; "/[x/y@" "/[x/y@z"
5859;; ["x" nil "y" nil] ["x" "y" "z" nil]
16674e4f
KG
5860(defun tramp-completion-dissect-file-name (name)
5861 "Returns a list of `tramp-file-name' structures.
5862They are collected by `tramp-completion-dissect-file-name1'."
5863
5864 (let* ((result)
4007ba5b 5865 (x-nil "\\|\\(\\)")
b96e6899
MA
5866 (tramp-completion-ipv6-regexp
5867 (format
5868 "[^%s]*"
5869 (if (zerop (length tramp-postfix-ipv6-format))
5870 tramp-postfix-host-format
5871 tramp-postfix-ipv6-format)))
4007ba5b
KG
5872 ;; "/method" "/[method"
5873 (tramp-completion-file-name-structure1
5874 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
5875 1 nil nil nil))
5876 ;; "/user" "/[user"
5877 (tramp-completion-file-name-structure2
5878 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
5879 nil 1 nil nil))
5880 ;; "/host" "/[host"
5881 (tramp-completion-file-name-structure3
5882 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
5883 nil nil 1 nil))
b96e6899 5884 ;; "/[ipv6" "/[ipv6"
4007ba5b 5885 (tramp-completion-file-name-structure4
b96e6899
MA
5886 (list (concat tramp-prefix-regexp
5887 tramp-prefix-ipv6-regexp
5888 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5889 nil nil 1 nil))
5890 ;; "/user@host" "/[user@host"
5891 (tramp-completion-file-name-structure5
4007ba5b
KG
5892 (list (concat tramp-prefix-regexp
5893 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5894 "\\(" tramp-host-regexp x-nil "\\)$")
5895 nil 1 2 nil))
b96e6899
MA
5896 ;; "/user@[ipv6" "/[user@ipv6"
5897 (tramp-completion-file-name-structure6
5898 (list (concat tramp-prefix-regexp
5899 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5900 tramp-prefix-ipv6-regexp
5901 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5902 nil 1 2 nil))
00d6fd04 5903 ;; "/method:user" "/[method/user" "/method://user"
b96e6899 5904 (tramp-completion-file-name-structure7
4007ba5b 5905 (list (concat tramp-prefix-regexp
00d6fd04 5906 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
5907 "\\(" tramp-user-regexp x-nil "\\)$")
5908 1 2 nil nil))
00d6fd04 5909 ;; "/method:host" "/[method/host" "/method://host"
b96e6899 5910 (tramp-completion-file-name-structure8
4007ba5b 5911 (list (concat tramp-prefix-regexp
00d6fd04 5912 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
5913 "\\(" tramp-host-regexp x-nil "\\)$")
5914 1 nil 2 nil))
b96e6899
MA
5915 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5916 (tramp-completion-file-name-structure9
5917 (list (concat tramp-prefix-regexp
5918 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5919 tramp-prefix-ipv6-regexp
5920 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5921 1 nil 2 nil))
00d6fd04 5922 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
b96e6899 5923 (tramp-completion-file-name-structure10
4007ba5b 5924 (list (concat tramp-prefix-regexp
00d6fd04 5925 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
4007ba5b
KG
5926 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5927 "\\(" tramp-host-regexp x-nil "\\)$")
00d6fd04 5928 1 2 3 nil))
b96e6899
MA
5929 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5930 (tramp-completion-file-name-structure11
5931 (list (concat tramp-prefix-regexp
5932 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5933 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5934 tramp-prefix-ipv6-regexp
5935 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5936 1 2 3 nil))
00d6fd04 5937 ;; "/method: "/method:/"
b96e6899 5938 (tramp-completion-file-name-structure12
00d6fd04
MA
5939 (list
5940 (if (equal tramp-syntax 'url)
5941 (concat tramp-prefix-regexp
5942 "\\(" tramp-method-regexp "\\)"
5943 "\\(" (substring tramp-postfix-method-regexp 0 1)
5944 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5945 "\\(" "\\)$")
5946 ;; Should not match if not URL syntax.
5947 (concat tramp-prefix-regexp "/$"))
5948 1 3 nil nil))
5949 ;; "/method: "/method:/"
b96e6899 5950 (tramp-completion-file-name-structure13
00d6fd04
MA
5951 (list
5952 (if (equal tramp-syntax 'url)
5953 (concat tramp-prefix-regexp
5954 "\\(" tramp-method-regexp "\\)"
5955 "\\(" (substring tramp-postfix-method-regexp 0 1)
5956 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5957 "\\(" "\\)$")
5958 ;; Should not match if not URL syntax.
5959 (concat tramp-prefix-regexp "/$"))
5960 1 nil 3 nil)))
4007ba5b 5961
9e6ab520 5962 (mapc (lambda (regexp)
16674e4f
KG
5963 (add-to-list 'result
5964 (tramp-completion-dissect-file-name1 regexp name)))
5965 (list
5966 tramp-completion-file-name-structure1
5967 tramp-completion-file-name-structure2
5968 tramp-completion-file-name-structure3
5969 tramp-completion-file-name-structure4
5970 tramp-completion-file-name-structure5
5971 tramp-completion-file-name-structure6
5972 tramp-completion-file-name-structure7
00d6fd04
MA
5973 tramp-completion-file-name-structure8
5974 tramp-completion-file-name-structure9
b96e6899
MA
5975 tramp-completion-file-name-structure10
5976 tramp-completion-file-name-structure11
5977 tramp-completion-file-name-structure12
5978 tramp-completion-file-name-structure13
16674e4f
KG
5979 tramp-file-name-structure))
5980
5981 (delq nil result)))
5982
5983(defun tramp-completion-dissect-file-name1 (structure name)
5984 "Returns a `tramp-file-name' structure matching STRUCTURE.
00d6fd04 5985The structure consists of remote method, remote user,
7432277c 5986remote host and localname (filename on remote host)."
fb7933a3 5987
00d6fd04
MA
5988 (save-match-data
5989 (when (string-match (nth 0 structure) name)
5990 (let ((method (and (nth 1 structure)
5991 (match-string (nth 1 structure) name)))
5992 (user (and (nth 2 structure)
5993 (match-string (nth 2 structure) name)))
5994 (host (and (nth 3 structure)
5995 (match-string (nth 3 structure) name)))
5996 (localname (and (nth 4 structure)
5997 (match-string (nth 4 structure) name))))
5998 (vector method user host localname)))))
16674e4f
KG
5999
6000;; This function returns all possible method completions, adding the
6001;; trailing method delimeter.
16674e4f
KG
6002(defun tramp-get-completion-methods (partial-method)
6003 "Returns all method completions for PARTIAL-METHOD."
4007ba5b
KG
6004 (mapcar
6005 (lambda (method)
6006 (and method
6007 (string-match (concat "^" (regexp-quote partial-method)) method)
00d6fd04
MA
6008 (tramp-completion-make-tramp-file-name method nil nil nil)))
6009 (mapcar 'car tramp-methods)))
16674e4f
KG
6010
6011;; Compares partial user and host names with possible completions.
6012(defun tramp-get-completion-user-host (method partial-user partial-host user host)
6013 "Returns the most expanded string for user and host name completion.
6014PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
6015 (cond
6016
6017 ((and partial-user partial-host)
6018 (if (and host
6019 (string-match (concat "^" (regexp-quote partial-host)) host)
6020 (string-equal partial-user (or user partial-user)))
6021 (setq user partial-user)
6022 (setq user nil
6023 host nil)))
6024
6025 (partial-user
6026 (setq host nil)
6027 (unless
6028 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
6029 (setq user nil)))
6030
6031 (partial-host
6032 (setq user nil)
6033 (unless
6034 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
6035 (setq host nil)))
6036
6037 (t (setq user nil
6038 host nil)))
6039
292ffc15 6040 (unless (zerop (+ (length user) (length host)))
00d6fd04 6041 (tramp-completion-make-tramp-file-name method user host nil)))
16674e4f
KG
6042
6043(defun tramp-parse-rhosts (filename)
6044 "Return a list of (user host) tuples allowed to access.
292ffc15 6045Either user or host may be nil."
00d6fd04
MA
6046 ;; On Windows, there are problems in completion when
6047 ;; `default-directory' is remote.
9e6ab520 6048 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6049 res)
8daea7fc 6050 (when (file-readable-p filename)
16674e4f
KG
6051 (with-temp-buffer
6052 (insert-file-contents filename)
6053 (goto-char (point-min))
6054 (while (not (eobp))
292ffc15 6055 (push (tramp-parse-rhosts-group) res))))
16674e4f
KG
6056 res))
6057
16674e4f
KG
6058(defun tramp-parse-rhosts-group ()
6059 "Return a (user host) tuple allowed to access.
292ffc15 6060Either user or host may be nil."
16674e4f
KG
6061 (let ((result)
6062 (regexp
6063 (concat
6064 "^\\(" tramp-host-regexp "\\)"
6065 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
9e6ab520 6066 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f
KG
6067 (when (re-search-forward regexp nil t)
6068 (setq result (append (list (match-string 3) (match-string 1)))))
6069 (widen)
6070 (forward-line 1)
6071 result))
6072
6073(defun tramp-parse-shosts (filename)
6074 "Return a list of (user host) tuples allowed to access.
6075User is always nil."
00d6fd04
MA
6076 ;; On Windows, there are problems in completion when
6077 ;; `default-directory' is remote.
9e6ab520 6078 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6079 res)
8daea7fc 6080 (when (file-readable-p filename)
16674e4f
KG
6081 (with-temp-buffer
6082 (insert-file-contents filename)
6083 (goto-char (point-min))
6084 (while (not (eobp))
292ffc15 6085 (push (tramp-parse-shosts-group) res))))
16674e4f
KG
6086 res))
6087
6088(defun tramp-parse-shosts-group ()
6089 "Return a (user host) tuple allowed to access.
6090User is always nil."
16674e4f
KG
6091 (let ((result)
6092 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
9e6ab520 6093 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f
KG
6094 (when (re-search-forward regexp nil t)
6095 (setq result (list nil (match-string 1))))
6096 (widen)
6097 (or
6098 (> (skip-chars-forward ",") 0)
6099 (forward-line 1))
6100 result))
6101
8daea7fc
KG
6102(defun tramp-parse-sconfig (filename)
6103 "Return a list of (user host) tuples allowed to access.
6104User is always nil."
00d6fd04
MA
6105 ;; On Windows, there are problems in completion when
6106 ;; `default-directory' is remote.
9e6ab520 6107 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6108 res)
8daea7fc
KG
6109 (when (file-readable-p filename)
6110 (with-temp-buffer
6111 (insert-file-contents filename)
6112 (goto-char (point-min))
6113 (while (not (eobp))
6114 (push (tramp-parse-sconfig-group) res))))
6115 res))
6116
6117(defun tramp-parse-sconfig-group ()
6118 "Return a (user host) tuple allowed to access.
6119User is always nil."
8daea7fc
KG
6120 (let ((result)
6121 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
9e6ab520 6122 (narrow-to-region (point) (tramp-compat-line-end-position))
8daea7fc
KG
6123 (when (re-search-forward regexp nil t)
6124 (setq result (list nil (match-string 1))))
6125 (widen)
6126 (or
6127 (> (skip-chars-forward ",") 0)
6128 (forward-line 1))
6129 result))
6130
5ec2cc41
KG
6131(defun tramp-parse-shostkeys (dirname)
6132 "Return a list of (user host) tuples allowed to access.
6133User is always nil."
00d6fd04
MA
6134 ;; On Windows, there are problems in completion when
6135 ;; `default-directory' is remote.
9e6ab520 6136 (let* ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
6137 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
6138 (files (when (file-directory-p dirname) (directory-files dirname)))
6139 result)
5ec2cc41
KG
6140 (while files
6141 (when (string-match regexp (car files))
6142 (push (list nil (match-string 1 (car files))) result))
6143 (setq files (cdr files)))
6144 result))
6145
6146(defun tramp-parse-sknownhosts (dirname)
6147 "Return a list of (user host) tuples allowed to access.
6148User is always nil."
00d6fd04
MA
6149 ;; On Windows, there are problems in completion when
6150 ;; `default-directory' is remote.
9e6ab520 6151 (let* ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
6152 (regexp (concat "^\\(" tramp-host-regexp
6153 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
6154 (files (when (file-directory-p dirname) (directory-files dirname)))
6155 result)
5ec2cc41
KG
6156 (while files
6157 (when (string-match regexp (car files))
6158 (push (list nil (match-string 1 (car files))) result))
6159 (setq files (cdr files)))
6160 result))
6161
16674e4f
KG
6162(defun tramp-parse-hosts (filename)
6163 "Return a list of (user host) tuples allowed to access.
6164User is always nil."
00d6fd04
MA
6165 ;; On Windows, there are problems in completion when
6166 ;; `default-directory' is remote.
9e6ab520 6167 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6168 res)
8daea7fc 6169 (when (file-readable-p filename)
16674e4f
KG
6170 (with-temp-buffer
6171 (insert-file-contents filename)
6172 (goto-char (point-min))
6173 (while (not (eobp))
292ffc15 6174 (push (tramp-parse-hosts-group) res))))
16674e4f
KG
6175 res))
6176
6177(defun tramp-parse-hosts-group ()
6178 "Return a (user host) tuple allowed to access.
6179User is always nil."
16674e4f 6180 (let ((result)
b96e6899
MA
6181 (regexp
6182 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
9e6ab520 6183 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f 6184 (when (re-search-forward regexp nil t)
b96e6899 6185 (setq result (list nil (match-string 1))))
16674e4f
KG
6186 (widen)
6187 (or
6188 (> (skip-chars-forward " \t") 0)
6189 (forward-line 1))
6190 result))
6191
8daea7fc
KG
6192;; For su-alike methods it would be desirable to return "root@localhost"
6193;; as default. Unfortunately, we have no information whether any user name
00d6fd04 6194;; has been typed already. So we use `tramp-current-user' as indication,
8daea7fc 6195;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
16674e4f
KG
6196(defun tramp-parse-passwd (filename)
6197 "Return a list of (user host) tuples allowed to access.
6198Host is always \"localhost\"."
00d6fd04
MA
6199 ;; On Windows, there are problems in completion when
6200 ;; `default-directory' is remote.
9e6ab520 6201 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6202 res)
8daea7fc 6203 (if (zerop (length tramp-current-user))
16674e4f 6204 '(("root" nil))
8daea7fc 6205 (when (file-readable-p filename)
16674e4f
KG
6206 (with-temp-buffer
6207 (insert-file-contents filename)
6208 (goto-char (point-min))
6209 (while (not (eobp))
292ffc15 6210 (push (tramp-parse-passwd-group) res))))
16674e4f
KG
6211 res)))
6212
6213(defun tramp-parse-passwd-group ()
6214 "Return a (user host) tuple allowed to access.
292ffc15 6215Host is always \"localhost\"."
16674e4f
KG
6216 (let ((result)
6217 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
9e6ab520 6218 (narrow-to-region (point) (tramp-compat-line-end-position))
16674e4f
KG
6219 (when (re-search-forward regexp nil t)
6220 (setq result (list (match-string 1) "localhost")))
6221 (widen)
6222 (forward-line 1)
6223 result))
6224
292ffc15
KG
6225(defun tramp-parse-netrc (filename)
6226 "Return a list of (user host) tuples allowed to access.
6227User may be nil."
00d6fd04
MA
6228 ;; On Windows, there are problems in completion when
6229 ;; `default-directory' is remote.
9e6ab520 6230 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04 6231 res)
8daea7fc 6232 (when (file-readable-p filename)
292ffc15
KG
6233 (with-temp-buffer
6234 (insert-file-contents filename)
6235 (goto-char (point-min))
6236 (while (not (eobp))
6237 (push (tramp-parse-netrc-group) res))))
6238 res))
6239
6240(defun tramp-parse-netrc-group ()
6241 "Return a (user host) tuple allowed to access.
6242User may be nil."
292ffc15
KG
6243 (let ((result)
6244 (regexp
6245 (concat
6246 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
6247 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
9e6ab520 6248 (narrow-to-region (point) (tramp-compat-line-end-position))
292ffc15
KG
6249 (when (re-search-forward regexp nil t)
6250 (setq result (list (match-string 3) (match-string 1))))
6251 (widen)
6252 (forward-line 1)
6253 result))
6254
00d6fd04
MA
6255(defun tramp-parse-putty (registry)
6256 "Return a list of (user host) tuples allowed to access.
6257User is always nil."
6258 ;; On Windows, there are problems in completion when
6259 ;; `default-directory' is remote.
9e6ab520 6260 (let ((default-directory (tramp-compat-temporary-file-directory))
00d6fd04
MA
6261 res)
6262 (with-temp-buffer
a4aeb9a4 6263 (when (zerop (tramp-local-call-process "reg" nil t nil "query" registry))
00d6fd04
MA
6264 (goto-char (point-min))
6265 (while (not (eobp))
6266 (push (tramp-parse-putty-group registry) res))))
6267 res))
6268
6269(defun tramp-parse-putty-group (registry)
6270 "Return a (user host) tuple allowed to access.
6271User is always nil."
6272 (let ((result)
6273 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
9e6ab520 6274 (narrow-to-region (point) (tramp-compat-line-end-position))
00d6fd04
MA
6275 (when (re-search-forward regexp nil t)
6276 (setq result (list nil (match-string 1))))
6277 (widen)
6278 (forward-line 1)
6279 result))
6280
fb7933a3
KG
6281;;; Internal Functions:
6282
00d6fd04
MA
6283(defun tramp-maybe-send-script (vec script name)
6284 "Define in remote shell function NAME implemented as SCRIPT.
6285Only send the definition if it has not already been done."
6286 (let* ((p (tramp-get-connection-process vec))
6287 (scripts (tramp-get-connection-property p "scripts" nil)))
1834b39f 6288 (unless (member name scripts)
00d6fd04
MA
6289 (tramp-message vec 5 "Sending script `%s'..." name)
6290 ;; The script could contain a call of Perl. This is masked with `%s'.
6291 (tramp-send-command-and-check
6292 vec
6293 (format "%s () {\n%s\n}" name
6294 (format script (tramp-get-remote-perl vec))))
6295 (tramp-set-connection-property p "scripts" (cons name scripts))
6296 (tramp-message vec 5 "Sending script `%s'...done." name))))
c82c5727 6297
fb7933a3 6298(defun tramp-set-auto-save ()
00d6fd04 6299 (when (and ;; ange-ftp has its own auto-save mechanism
7177e2a3
MA
6300 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6301 'tramp-sh-file-name-handler)
fb7933a3
KG
6302 auto-save-default)
6303 (auto-save-mode 1)))
6304(add-hook 'find-file-hooks 'tramp-set-auto-save t)
a69c01a0 6305(add-hook 'tramp-unload-hook
aa485f7c
MA
6306 (lambda ()
6307 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
fb7933a3
KG
6308
6309(defun tramp-run-test (switch filename)
6310 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6311Returns the exit code of the `test' program."
00d6fd04
MA
6312 (with-parsed-tramp-file-name filename nil
6313 (tramp-send-command-and-check
6314 v
6315 (format
6316 "%s %s %s"
6317 (tramp-get-test-command v)
6318 switch
6319 (tramp-shell-quote-argument localname)))))
6320
6321(defun tramp-run-test2 (format-string file1 file2)
6322 "Run `test'-like program on the remote system, given FILE1, FILE2.
6323FORMAT-STRING contains the program name, switches, and place holders.
6324Returns the exit code of the `test' program. Barfs if the methods,
fb7933a3 6325hosts, or files, disagree."
00d6fd04
MA
6326 (unless (tramp-equal-remote file1 file2)
6327 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6328 (tramp-error
6329 v 'file-error
6330 "tramp-run-test2 only implemented for same method, user, host")))
6331 (with-parsed-tramp-file-name file1 v1
6332 (with-parsed-tramp-file-name file1 v2
fb7933a3 6333 (tramp-send-command-and-check
00d6fd04
MA
6334 v1
6335 (format format-string
6336 (tramp-shell-quote-argument v1-localname)
6337 (tramp-shell-quote-argument v2-localname))))))
fb7933a3 6338
00d6fd04
MA
6339(defun tramp-buffer-name (vec)
6340 "A name for the connection buffer VEC."
6341 ;; We must use `tramp-file-name-real-host', because for gateway
6342 ;; methods the default port will be expanded later on, which would
6343 ;; tamper the name.
6344 (let ((method (tramp-file-name-method vec))
6345 (user (tramp-file-name-user vec))
6346 (host (tramp-file-name-real-host vec)))
6347 (if (not (zerop (length user)))
6348 (format "*tramp/%s %s@%s*" method user host)
6349 (format "*tramp/%s %s*" method host))))
6350
b88f2d0a
MA
6351(defun tramp-delete-temp-file-function ()
6352 "Remove temporary files related to current buffer."
6353 (when (stringp tramp-temp-buffer-file-name)
6354 (condition-case nil
66bdc868 6355 (tramp-compat-delete-file tramp-temp-buffer-file-name 'force)
b88f2d0a
MA
6356 (error nil))))
6357
6358(add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6359(add-hook 'tramp-cache-unload-hook
6360 (lambda ()
6361 (remove-hook 'kill-buffer-hook
6362 'tramp-delete-temp-file-function)))
6363
00d6fd04
MA
6364(defun tramp-get-buffer (vec)
6365 "Get the connection buffer to be used for VEC."
6366 (or (get-buffer (tramp-buffer-name vec))
6367 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
6368 (setq buffer-undo-list t)
6369 (setq default-directory
6370 (tramp-make-tramp-file-name
6371 (tramp-file-name-method vec)
6372 (tramp-file-name-user vec)
6373 (tramp-file-name-host vec)
6374 "/"))
6375 (current-buffer))))
6376
6377(defun tramp-get-connection-buffer (vec)
6378 "Get the connection buffer to be used for VEC.
6379In case a second asynchronous communication has been started, it is different
6380from `tramp-get-buffer'."
6381 (or (tramp-get-connection-property vec "process-buffer" nil)
6382 (tramp-get-buffer vec)))
6383
6384(defun tramp-get-connection-process (vec)
6385 "Get the connection process to be used for VEC.
6386In case a second asynchronous communication has been started, it is different
6387from the default one."
6388 (get-process
6389 (or (tramp-get-connection-property vec "process-name" nil)
6390 (tramp-buffer-name vec))))
6391
6392(defun tramp-debug-buffer-name (vec)
6393 "A name for the debug buffer for VEC."
6394 ;; We must use `tramp-file-name-real-host', because for gateway
6395 ;; methods the default port will be expanded later on, which would
6396 ;; tamper the name.
6397 (let ((method (tramp-file-name-method vec))
6398 (user (tramp-file-name-user vec))
6399 (host (tramp-file-name-real-host vec)))
6400 (if (not (zerop (length user)))
6401 (format "*debug tramp/%s %s@%s*" method user host)
6402 (format "*debug tramp/%s %s*" method host))))
6403
b533bc97
MA
6404(defconst tramp-debug-outline-regexp
6405 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6406
00d6fd04
MA
6407(defun tramp-get-debug-buffer (vec)
6408 "Get the debug buffer for VEC."
01917a18 6409 (with-current-buffer
00d6fd04
MA
6410 (get-buffer-create (tramp-debug-buffer-name vec))
6411 (when (bobp)
6412 (setq buffer-undo-list t)
b533bc97 6413 ;; Activate `outline-mode'. This runs `text-mode-hook' and
9ce8462a 6414 ;; `outline-mode-hook'. We must prevent that local processes
b533bc97
MA
6415 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
6416 ;; Furthermore, `outline-regexp' must have the correct value
6417 ;; already, because it is used by `font-lock-compile-keywords'.
6418 (let ((default-directory (tramp-compat-temporary-file-directory))
6419 (outline-regexp tramp-debug-outline-regexp))
00d6fd04 6420 (outline-mode))
b533bc97 6421 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
9ce8462a 6422 (set (make-local-variable 'outline-level) 'tramp-outline-level))
01917a18 6423 (current-buffer)))
fb7933a3 6424
00d6fd04
MA
6425(defun tramp-outline-level ()
6426 "Return the depth to which a statement is nested in the outline.
6427Point must be at the beginning of a header line.
6428
6429The outline level is equal to the verbosity of the Tramp message."
6430 (1+ (string-to-number (match-string 1))))
fb7933a3 6431
00d6fd04
MA
6432(defun tramp-find-executable
6433 (vec progname dirlist &optional ignore-tilde ignore-path)
6434 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6435First arg VEC specifies the connection, PROGNAME is the program
6436to search for, and DIRLIST gives the list of directories to
6437search. If IGNORE-TILDE is non-nil, directory names starting
6438with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6439only in DIRLIST.
fb7933a3 6440
7432277c 6441Returns the absolute file name of PROGNAME, if found, and nil otherwise.
fb7933a3
KG
6442
6443This function expects to be in the right *tramp* buffer."
c0e17ff2 6444 (with-current-buffer (tramp-get-connection-buffer vec)
00d6fd04
MA
6445 (let (result)
6446 ;; Check whether the executable is in $PATH. "which(1)" does not
6447 ;; report always a correct error code; therefore we check the
6448 ;; number of words it returns.
6449 (unless ignore-path
6450 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6451 (goto-char (point-min))
c0e17ff2 6452 (if (looking-at "^\\s-*1$")
00d6fd04
MA
6453 (setq result (concat "\\" progname))))
6454 (unless result
6455 (when ignore-tilde
b533bc97 6456 ;; Remove all ~/foo directories from dirlist. In XEmacs,
00d6fd04
MA
6457 ;; `remove' is in CL, and we want to avoid CL dependencies.
6458 (let (newdl d)
6459 (while dirlist
6460 (setq d (car dirlist))
6461 (setq dirlist (cdr dirlist))
6462 (unless (char-equal ?~ (aref d 0))
6463 (setq newdl (cons d newdl))))
6464 (setq dirlist (nreverse newdl))))
6465 (tramp-send-command
6466 vec
6467 (format (concat "while read d; "
6468 "do if test -x $d/%s -a -f $d/%s; "
6469 "then echo tramp_executable $d/%s; "
6470 "break; fi; done <<'EOF'\n"
6471 "%s\nEOF")
6472 progname progname progname (mapconcat 'identity dirlist "\n")))
6473 (goto-char (point-max))
6474 (when (search-backward "tramp_executable " nil t)
6475 (skip-chars-forward "^ ")
6476 (skip-chars-forward " ")
9e6ab520
MA
6477 (setq result (buffer-substring
6478 (point) (tramp-compat-line-end-position)))))
00d6fd04
MA
6479 result)))
6480
6481(defun tramp-set-remote-path (vec)
6482 "Sets the remote environment PATH to existing directories.
6483I.e., for each directory in `tramp-remote-path', it is tested
6484whether it exists and if so, it is added to the environment
6485variable PATH."
6486 (tramp-message vec 5 (format "Setting $PATH environment variable"))
f84638eb
MA
6487 (tramp-send-command
6488 vec (format "PATH=%s; export PATH"
6489 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
fb7933a3 6490
cfb5c0db
MA
6491;; ------------------------------------------------------------
6492;; -- Communication with external shell --
6493;; ------------------------------------------------------------
fb7933a3 6494
00d6fd04 6495(defun tramp-find-file-exists-command (vec)
fb7933a3
KG
6496 "Find a command on the remote host for checking if a file exists.
6497Here, we are looking for a command which has zero exit status if the
6498file exists and nonzero exit status otherwise."
00d6fd04 6499 (let ((existing "/")
fb7933a3 6500 (nonexisting
00d6fd04
MA
6501 (tramp-shell-quote-argument "/ this file does not exist "))
6502 result)
fb7933a3
KG
6503 ;; The algorithm is as follows: we try a list of several commands.
6504 ;; For each command, we first run `$cmd /' -- this should return
6505 ;; true, as the root directory always exists. And then we run
00d6fd04 6506 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
fb7933a3
KG
6507 ;; does not exist. This should return false. We use the first
6508 ;; command we find that seems to work.
6509 ;; The list of commands to try is as follows:
00d6fd04
MA
6510 ;; `ls -d' This works on most systems, but NetBSD 1.4
6511 ;; has a bug: `ls' always returns zero exit
6512 ;; status, even for files which don't exist.
6513 ;; `test -e' Some Bourne shells have a `test' builtin
6514 ;; which does not know the `-e' option.
6515 ;; `/bin/test -e' For those, the `test' binary on disk normally
6516 ;; provides the option. Alas, the binary
6517 ;; is sometimes `/bin/test' and sometimes it's
6518 ;; `/usr/bin/test'.
6519 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
fb7933a3 6520 (unless (or
00d6fd04
MA
6521 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6522 (zerop (tramp-send-command-and-check
6523 vec (format "%s %s" result existing)))
6524 (not (zerop (tramp-send-command-and-check
6525 vec (format "%s %s" result nonexisting)))))
6526 (and (setq result "/bin/test -e")
6527 (zerop (tramp-send-command-and-check
6528 vec (format "%s %s" result existing)))
6529 (not (zerop (tramp-send-command-and-check
6530 vec (format "%s %s" result nonexisting)))))
6531 (and (setq result "/usr/bin/test -e")
6532 (zerop (tramp-send-command-and-check
6533 vec (format "%s %s" result existing)))
6534 (not (zerop (tramp-send-command-and-check
6535 vec (format "%s %s" result nonexisting)))))
6536 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6537 (zerop (tramp-send-command-and-check
6538 vec (format "%s %s" result existing)))
6539 (not (zerop (tramp-send-command-and-check
6540 vec (format "%s %s" result nonexisting))))))
6541 (tramp-error
6542 vec 'file-error "Couldn't find command to check if file exists"))
6543 result))
bf247b6e 6544
fb7933a3 6545;; CCC test ksh or bash found for tilde expansion?
00d6fd04
MA
6546(defun tramp-find-shell (vec)
6547 "Opens a shell on the remote host which groks tilde expansion."
6548 (unless (tramp-get-connection-property vec "remote-shell" nil)
6549 (let (shell)
6550 (with-current-buffer (tramp-get-buffer vec)
7e780ff1 6551 (tramp-send-command vec "echo ~root" t)
00d6fd04 6552 (cond
c0e17ff2
MA
6553 ((or (string-match "^~root$" (buffer-string))
6554 ;; The default shell (ksh93) of OpenSolaris is buggy.
6555 (string-equal (tramp-get-connection-property vec "uname" "")
6556 "SunOS 5.11"))
00d6fd04 6557 (setq shell
f84638eb 6558 (or (tramp-find-executable
c0e17ff2 6559 vec "bash" (tramp-get-remote-path vec) t t)
f84638eb 6560 (tramp-find-executable
c0e17ff2 6561 vec "ksh" (tramp-get-remote-path vec) t t)))
00d6fd04
MA
6562 (unless shell
6563 (tramp-error
6564 vec 'file-error
6565 "Couldn't find a shell which groks tilde expansion"))
6566 ;; Find arguments for this shell.
6567 (let ((alist tramp-sh-extra-args)
6568 item extra-args)
6569 (while (and alist (null extra-args))
6570 (setq item (pop alist))
6571 (when (string-match (car item) shell)
6572 (setq extra-args (cdr item))))
6573 (when extra-args (setq shell (concat shell " " extra-args))))
6574 (tramp-message
6575 vec 5 "Starting remote shell `%s' for tilde expansion..." shell)
dab816a9 6576 (let ((tramp-end-of-output tramp-initial-end-of-output))
a4aeb9a4 6577 (tramp-send-command
b08104a0 6578 vec
70c11b0b
MA
6579 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6580 (shell-quote-argument tramp-end-of-output) shell)
b08104a0 6581 t))
a0a5183a 6582 ;; Setting prompts.
00d6fd04 6583 (tramp-message vec 5 "Setting remote shell prompt...")
70c11b0b
MA
6584 (tramp-send-command
6585 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
9fa0d3aa
MA
6586 (tramp-send-command vec "PS2=''" t)
6587 (tramp-send-command vec "PS3=''" t)
6588 (tramp-send-command vec "PROMPT_COMMAND=''" t)
00d6fd04 6589 (tramp-message vec 5 "Setting remote shell prompt...done"))
a0a5183a 6590
00d6fd04
MA
6591 (t (tramp-message
6592 vec 5 "Remote `%s' groks tilde expansion, good"
6593 (tramp-get-method-parameter
6594 (tramp-file-name-method vec) 'tramp-remote-sh))
6595 (tramp-set-connection-property
6596 vec "remote-shell"
6597 (tramp-get-method-parameter
6598 (tramp-file-name-method vec) 'tramp-remote-sh))))))))
fb7933a3 6599
bf247b6e
KS
6600;; ------------------------------------------------------------
6601;; -- Functions for establishing connection --
6602;; ------------------------------------------------------------
fb7933a3 6603
ac474af1
KG
6604;; The following functions are actions to be taken when seeing certain
6605;; prompts from the remote host. See the variable
6606;; `tramp-actions-before-shell' for usage of these functions.
6607
00d6fd04 6608(defun tramp-action-login (proc vec)
ac474af1 6609 "Send the login name."
00d6fd04
MA
6610 (when (not (stringp tramp-current-user))
6611 (save-window-excursion
6612 (let ((enable-recursive-minibuffers t))
6613 (pop-to-buffer (tramp-get-connection-buffer vec))
6614 (setq tramp-current-user (read-string (match-string 0))))))
6615 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
6616 (with-current-buffer (tramp-get-connection-buffer vec)
6617 (tramp-message vec 6 "\n%s" (buffer-string)))
6618 (tramp-send-string vec tramp-current-user))
6619
6620(defun tramp-action-password (proc vec)
ac474af1 6621 "Query the user for a password."
70c11b0b
MA
6622 (with-current-buffer (process-buffer proc)
6623 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
6624 (tramp-message vec 3 "Sending %s" (match-string 1)))
00d6fd04
MA
6625 (tramp-enter-password proc))
6626
6627(defun tramp-action-succeed (proc vec)
ac474af1 6628 "Signal success in finding shell prompt."
ac474af1
KG
6629 (throw 'tramp-action 'ok))
6630
00d6fd04 6631(defun tramp-action-permission-denied (proc vec)
ac474af1 6632 "Signal permission denied."
00d6fd04 6633 (kill-process proc)
ac474af1
KG
6634 (throw 'tramp-action 'permission-denied))
6635
00d6fd04 6636(defun tramp-action-yesno (proc vec)
3cdaec13
KG
6637 "Ask the user for confirmation using `yes-or-no-p'.
6638Send \"yes\" to remote process on confirmation, abort otherwise.
6639See also `tramp-action-yn'."
ac474af1 6640 (save-window-excursion
00d6fd04
MA
6641 (let ((enable-recursive-minibuffers t))
6642 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6643 (unless (yes-or-no-p (match-string 0))
6644 (kill-process proc)
6645 (throw 'tramp-action 'permission-denied))
6646 (with-current-buffer (tramp-get-connection-buffer vec)
6647 (tramp-message vec 6 "\n%s" (buffer-string)))
6648 (tramp-send-string vec "yes"))))
6649
6650(defun tramp-action-yn (proc vec)
3cdaec13
KG
6651 "Ask the user for confirmation using `y-or-n-p'.
6652Send \"y\" to remote process on confirmation, abort otherwise.
6653See also `tramp-action-yesno'."
6654 (save-window-excursion
00d6fd04
MA
6655 (let ((enable-recursive-minibuffers t))
6656 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6657 (unless (y-or-n-p (match-string 0))
6658 (kill-process proc)
6659 (throw 'tramp-action 'permission-denied))
6660 (with-current-buffer (tramp-get-connection-buffer vec)
6661 (tramp-message vec 6 "\n%s" (buffer-string)))
6662 (tramp-send-string vec "y"))))
6663
6664(defun tramp-action-terminal (proc vec)
487f4fb7
KG
6665 "Tell the remote host which terminal type to use.
6666The terminal type can be configured with `tramp-terminal-type'."
00d6fd04 6667 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
7e780ff1
MA
6668 (with-current-buffer (tramp-get-connection-buffer vec)
6669 (tramp-message vec 6 "\n%s" (buffer-string)))
00d6fd04 6670 (tramp-send-string vec tramp-terminal-type))
487f4fb7 6671
00d6fd04 6672(defun tramp-action-process-alive (proc vec)
a94d821f 6673 "Check, whether a process has finished."
00d6fd04 6674 (unless (memq (process-status proc) '(run open))
19a87064
MA
6675 (throw 'tramp-action 'process-died)))
6676
00d6fd04 6677(defun tramp-action-out-of-band (proc vec)
a94d821f 6678 "Check, whether an out-of-band copy has finished."
00d6fd04
MA
6679 (cond ((and (memq (process-status proc) '(stop exit))
6680 (zerop (process-exit-status proc)))
6681 (tramp-message vec 3 "Process has finished.")
38c65fca 6682 (throw 'tramp-action 'ok))
00d6fd04
MA
6683 ((or (and (memq (process-status proc) '(stop exit))
6684 (not (zerop (process-exit-status proc))))
6685 (memq (process-status proc) '(signal)))
01917a18
MA
6686 ;; `scp' could have copied correctly, but set modes could have failed.
6687 ;; This can be ignored.
00d6fd04
MA
6688 (with-current-buffer (process-buffer proc)
6689 (goto-char (point-min))
6690 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
6691 (progn
6692 (tramp-message vec 5 "'set mode' error ignored.")
6693 (tramp-message vec 3 "Process has finished.")
6694 (throw 'tramp-action 'ok))
6695 (tramp-message vec 3 "Process has died.")
6696 (throw 'tramp-action 'process-died))))
38c65fca
KG
6697 (t nil)))
6698
ac474af1
KG
6699;; Functions for processing the actions.
6700
00d6fd04 6701(defun tramp-process-one-action (proc vec actions)
ac474af1 6702 "Wait for output from the shell and perform one action."
00d6fd04 6703 (let (found todo item pattern action)
e6466697 6704 (while (not found)
00d6fd04
MA
6705 ;; Reread output once all actions have been performed.
6706 ;; Obviously, the output was not complete.
6707 (tramp-accept-process-output proc 1)
e6466697
MA
6708 (setq todo actions)
6709 (while todo
e6466697 6710 (setq item (pop todo))
95d610cb 6711 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
e6466697 6712 (setq action (nth 1 item))
00d6fd04
MA
6713 (tramp-message
6714 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
6715 (when (tramp-check-for-regexp proc pattern)
6716 (tramp-message vec 5 "Call `%s'" (symbol-name action))
6717 (setq found (funcall action proc vec)))))
e6466697
MA
6718 found))
6719
00d6fd04 6720(defun tramp-process-actions (proc vec actions &optional timeout)
e6466697 6721 "Perform actions until success or TIMEOUT."
263c02ef 6722 ;; Enable auth-source and password-cache.
7540f029 6723 (tramp-set-connection-property vec "first-password-request" t)
ac474af1
KG
6724 (let (exit)
6725 (while (not exit)
00d6fd04 6726 (tramp-message proc 3 "Waiting for prompts from remote shell")
ac474af1
KG
6727 (setq exit
6728 (catch 'tramp-action
e6466697
MA
6729 (if timeout
6730 (with-timeout (timeout)
00d6fd04
MA
6731 (tramp-process-one-action proc vec actions))
6732 (tramp-process-one-action proc vec actions)))))
6733 (with-current-buffer (tramp-get-connection-buffer vec)
6734 (tramp-message vec 6 "\n%s" (buffer-string)))
ac474af1 6735 (unless (eq exit 'ok)
9c13938d 6736 (tramp-clear-passwd vec)
00d6fd04
MA
6737 (tramp-error-with-buffer
6738 nil vec 'file-error
6739 (cond
6740 ((eq exit 'permission-denied) "Permission denied")
6741 ((eq exit 'process-died) "Process died")
6742 (t "Login failed"))))))
fb7933a3
KG
6743
6744;; Utility functions.
6745
00d6fd04 6746(defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
d2a2c17f
MA
6747 "Like `accept-process-output' for Tramp processes.
6748This is needed in order to hide `last-coding-system-used', which is set
6749for process communication also."
00d6fd04
MA
6750 (with-current-buffer (process-buffer proc)
6751 (tramp-message proc 10 "%s %s" proc (process-status proc))
6752 (let (buffer-read-only last-coding-system-used)
6753 ;; Under Windows XP, accept-process-output doesn't return
6754 ;; sometimes. So we add an additional timeout.
6755 (with-timeout ((or timeout 1))
6756 (accept-process-output proc timeout timeout-msecs)))
6757 (tramp-message proc 10 "\n%s" (buffer-string))))
6758
6759(defun tramp-check-for-regexp (proc regexp)
a94d821f 6760 "Check, whether REGEXP is contained in process buffer of PROC.
00d6fd04
MA
6761Erase echoed commands if exists."
6762 (with-current-buffer (process-buffer proc)
6763 (goto-char (point-min))
674da028 6764
00d6fd04
MA
6765 ;; Check whether we need to remove echo output.
6766 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
6767 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
6768 (let ((begin (match-beginning 0)))
6769 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
6770 ;; Discard echo from remote output.
6771 (tramp-set-connection-property proc "check-remote-echo" nil)
6772 (tramp-message proc 5 "echo-mark found")
b533bc97 6773 (forward-line 1)
00d6fd04
MA
6774 (delete-region begin (point))
6775 (goto-char (point-min)))))
674da028 6776
68712eb6
MA
6777 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
6778 ;; Sometimes, the echo string is suppressed on the remote side.
6779 (not (string-equal
0d5852cf
MA
6780 (tramp-compat-funcall
6781 'substring-no-properties tramp-echo-mark-marker
68712eb6 6782 0 (min tramp-echo-mark-marker-length (1- (point-max))))
0d5852cf
MA
6783 (tramp-compat-funcall
6784 'buffer-substring-no-properties
68712eb6 6785 1 (min (1+ tramp-echo-mark-marker-length) (point-max))))))
70c11b0b 6786 ;; No echo to be handled, now we can look for the regexp.
674da028 6787 (goto-char (point-min))
00d6fd04 6788 (re-search-forward regexp nil t))))
d2a2c17f 6789
fb7933a3
KG
6790(defun tramp-wait-for-regexp (proc timeout regexp)
6791 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6792Expects the output of PROC to be sent to the current buffer. Returns
6793the string that matched, or nil. Waits indefinitely if TIMEOUT is
6794nil."
00d6fd04
MA
6795 (with-current-buffer (process-buffer proc)
6796 (let ((found (tramp-check-for-regexp proc regexp))
6797 (start-time (current-time)))
6798 (cond (timeout
6799 ;; Work around a bug in XEmacs 21, where the timeout
6800 ;; expires faster than it should. This degenerates
6801 ;; to polling for buggy XEmacsen, but oh, well.
6802 (while (and (not found)
6803 (< (tramp-time-diff (current-time) start-time)
6804 timeout))
6805 (with-timeout (timeout)
6806 (while (not found)
6807 (tramp-accept-process-output proc 1)
6808 (unless (memq (process-status proc) '(run open))
6809 (tramp-error-with-buffer
6810 nil proc 'file-error "Process has died"))
6811 (setq found (tramp-check-for-regexp proc regexp))))))
6812 (t
6813 (while (not found)
6814 (tramp-accept-process-output proc 1)
6815 (unless (memq (process-status proc) '(run open))
6816 (tramp-error-with-buffer
6817 nil proc 'file-error "Process has died"))
6818 (setq found (tramp-check-for-regexp proc regexp)))))
6819 (tramp-message proc 6 "\n%s" (buffer-string))
fb7933a3 6820 (when (not found)
00d6fd04
MA
6821 (if timeout
6822 (tramp-error
6823 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
6824 regexp timeout)
6825 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6826 found)))
fb7933a3 6827
b25a52cc
KG
6828(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6829 "Wait for shell prompt and barf if none appears.
6830Looks at process PROC to see if a shell prompt appears in TIMEOUT
6831seconds. If not, it produces an error message with the given ERROR-ARGS."
7e780ff1
MA
6832 (unless
6833 (tramp-wait-for-regexp
6834 proc timeout
6835 (format
6836 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
00d6fd04
MA
6837 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6838
7e780ff1
MA
6839;; We don't call `tramp-send-string' in order to hide the password
6840;; from the debug buffer, and because end-of-line handling of the
6841;; string.
6842(defun tramp-enter-password (proc)
00d6fd04
MA
6843 "Prompt for a password and send it to the remote end."
6844 (process-send-string
7e780ff1
MA
6845 proc (concat (tramp-read-passwd proc)
6846 (or (tramp-get-method-parameter
6847 tramp-current-method
6848 'tramp-password-end-of-line)
6849 tramp-default-password-end-of-line))))
00d6fd04
MA
6850
6851(defun tramp-open-connection-setup-interactive-shell (proc vec)
fb7933a3 6852 "Set up an interactive shell.
00d6fd04
MA
6853Mainly sets the prompt and the echo correctly. PROC is the shell
6854process to set up. VEC specifies the connection."
dab816a9 6855 (let ((tramp-end-of-output tramp-initial-end-of-output))
8950769a
MA
6856 ;; It is useful to set the prompt in the following command because
6857 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6858 ;; about and thus /bin/sh will display a strange prompt. For
6859 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6860 ;; display the current working directory but /bin/sh will display
6861 ;; a dollar sign. The following command line sets $PS1 to a sane
6862 ;; value, and works under Bourne-ish shells as well as csh-like
6863 ;; shells. Daniel Pittman reports that the unusual positioning of
6864 ;; the single quotes makes it work under `rc', too. We also unset
6865 ;; the variable $ENV because that is read by some sh
6866 ;; implementations (eg, bash when called as sh) on startup; this
6867 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6868 ;; is another way to set the prompt in /bin/bash, it must be
6869 ;; discarded as well.
a4aeb9a4
MA
6870 (tramp-send-command
6871 vec
6872 (format
70c11b0b
MA
6873 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6874 (shell-quote-argument tramp-end-of-output)
a4aeb9a4
MA
6875 (tramp-get-method-parameter
6876 (tramp-file-name-method vec) 'tramp-remote-sh))
8950769a
MA
6877 t)
6878
6879 ;; Disable echo.
6880 (tramp-message vec 5 "Setting up remote shell environment")
6881 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
6882 ;; Check whether the echo has really been disabled. Some
6883 ;; implementations, like busybox of embedded GNU/Linux, don't
6884 ;; support disabling.
6885 (tramp-send-command vec "echo foo" t)
6886 (with-current-buffer (process-buffer proc)
6887 (goto-char (point-min))
6888 (when (looking-at "echo foo")
6889 (tramp-set-connection-property proc "remote-echo" t)
6890 (tramp-message vec 5 "Remote echo still on. Ok.")
6891 ;; Make sure backspaces and their echo are enabled and no line
6892 ;; width magic interferes with them.
6893 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
e42c6bbc 6894
7e780ff1 6895 (tramp-message vec 5 "Setting shell prompt")
70c11b0b
MA
6896 (tramp-send-command
6897 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
9fa0d3aa
MA
6898 (tramp-send-command vec "PS2=''" t)
6899 (tramp-send-command vec "PS3=''" t)
6900 (tramp-send-command vec "PROMPT_COMMAND=''" t)
e42c6bbc 6901
fb7933a3
KG
6902 ;; Try to set up the coding system correctly.
6903 ;; CCC this can't be the right way to do it. Hm.
00d6fd04 6904 (tramp-message vec 5 "Determining coding system")
7e780ff1 6905 (tramp-send-command vec "echo foo ; echo bar" t)
00d6fd04 6906 (with-current-buffer (process-buffer proc)
fb7933a3
KG
6907 (goto-char (point-min))
6908 (if (featurep 'mule)
00d6fd04
MA
6909 ;; Use MULE to select the right EOL convention for communicating
6910 ;; with the process.
0d5852cf 6911 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
00d6fd04
MA
6912 (cons 'undecided 'undecided)))
6913 cs-decode cs-encode)
6914 (when (symbolp cs) (setq cs (cons cs cs)))
6915 (setq cs-decode (car cs))
6916 (setq cs-encode (cdr cs))
6917 (unless cs-decode (setq cs-decode 'undecided))
6918 (unless cs-encode (setq cs-encode 'undecided))
6919 (setq cs-encode (tramp-coding-system-change-eol-conversion
6920 cs-encode 'unix))
6921 (when (search-forward "\r" nil t)
6922 (setq cs-decode (tramp-coding-system-change-eol-conversion
6923 cs-decode 'dos)))
0d5852cf
MA
6924 (tramp-compat-funcall
6925 'set-buffer-process-coding-system cs-decode cs-encode)
70c11b0b
MA
6926 (tramp-message
6927 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
fb7933a3
KG
6928 ;; Look for ^M and do something useful if found.
6929 (when (search-forward "\r" nil t)
00d6fd04
MA
6930 ;; We have found a ^M but cannot frob the process coding system
6931 ;; because we're running on a non-MULE Emacs. Let's try
6932 ;; stty, instead.
7e780ff1 6933 (tramp-send-command vec "stty -onlcr" t))))
7e5686f0
MA
6934 ;; Dump stty settings in the traces.
6935 (when (>= tramp-verbose 10)
6936 (tramp-send-command vec "stty -a" t))
7e780ff1 6937 (tramp-send-command vec "set +o vi +o emacs" t)
e42c6bbc
MA
6938
6939 ;; Check whether the output of "uname -sr" has been changed. If
6940 ;; yes, this is a strong indication that we must expire all
d8ac123e
MA
6941 ;; connection properties. We start again with
6942 ;; `tramp-maybe-open-connection', it will be catched there.
e42c6bbc
MA
6943 (tramp-message vec 5 "Checking system information")
6944 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
6945 (new-uname
6946 (tramp-set-connection-property
6947 vec "uname"
6948 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
6949 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
d8ac123e 6950 (with-current-buffer (tramp-get-debug-buffer vec)
7ba94701 6951 ;; Keep the debug buffer.
2296b54d
MA
6952 (rename-buffer
6953 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
0d5852cf 6954 (tramp-compat-funcall 'tramp-cleanup-connection vec)
d8ac123e
MA
6955 (if (= (point-min) (point-max))
6956 (kill-buffer nil)
6957 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
6958 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
6959 (tramp-get-buffer vec)
6960 (tramp-message
6961 vec 3
6962 "Connection reset, because remote host changed from `%s' to `%s'"
6963 old-uname new-uname)
6964 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
e42c6bbc
MA
6965
6966 ;; Check whether the remote host suffers from buggy
6967 ;; `send-process-string'. This is known for FreeBSD (see comment in
6968 ;; `send_process', file process.c). I've tested sending 624 bytes
6969 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
6970 ;; this host type is detected locally. It cannot handle remote
6971 ;; hosts, though.
00d6fd04
MA
6972 (with-connection-property proc "chunksize"
6973 (cond
6974 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
6975 tramp-chunksize)
6976 (t
6977 (tramp-message
6978 vec 5 "Checking remote host type for `send-process-string' bug")
6979 (if (string-match
e42c6bbc 6980 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
00d6fd04 6981 500 0))))
e42c6bbc 6982
00d6fd04
MA
6983 ;; Set remote PATH variable.
6984 (tramp-set-remote-path vec)
e42c6bbc 6985
fb7933a3
KG
6986 ;; Search for a good shell before searching for a command which
6987 ;; checks if a file exists. This is done because Tramp wants to use
6988 ;; "test foo; echo $?" to check if various conditions hold, and
6989 ;; there are buggy /bin/sh implementations which don't execute the
6990 ;; "echo $?" part if the "test" part has an error. In particular,
c0e17ff2
MA
6991 ;; the OpenSolaris /bin/sh is a problem. There are also other
6992 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
6993 ;; in in function declarations, or changing HISTFILE in place.
6994 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
6995 ;; detected.
00d6fd04 6996 (tramp-find-shell vec)
e42c6bbc 6997
00d6fd04 6998 ;; Disable unexpected output.
7e780ff1 6999 (tramp-send-command vec "mesg n; biff n" t)
e42c6bbc 7000
00d6fd04
MA
7001 ;; Set the environment.
7002 (tramp-message vec 5 "Setting default environment")
661aaece 7003
00d6fd04
MA
7004 (let ((env (copy-sequence tramp-remote-process-environment))
7005 unset item)
7006 (while env
70c11b0b 7007 (setq item (tramp-compat-split-string (car env) "="))
7e5686f0
MA
7008 (setcdr item (mapconcat 'identity (cdr item) "="))
7009 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
00d6fd04 7010 (tramp-send-command
7e5686f0 7011 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
00d6fd04
MA
7012 (push (car item) unset))
7013 (setq env (cdr env)))
7014 (when unset
fb7933a3 7015 (tramp-send-command
7e780ff1 7016 vec (format "unset %s" (mapconcat 'identity unset " "))))) t)
fb7933a3 7017
ac474af1
KG
7018;; CCC: We should either implement a Perl version of base64 encoding
7019;; and decoding. Then we just use that in the last item. The other
7020;; alternative is to use the Perl version of UU encoding. But then
7021;; we need a Lisp version of uuencode.
16674e4f
KG
7022;;
7023;; Old text from documentation of tramp-methods:
7024;; Using a uuencode/uudecode inline method is discouraged, please use one
7025;; of the base64 methods instead since base64 encoding is much more
7026;; reliable and the commands are more standardized between the different
7027;; Unix versions. But if you can't use base64 for some reason, please
7028;; note that the default uudecode command does not work well for some
7029;; Unices, in particular AIX and Irix. For AIX, you might want to use
7030;; the following command for uudecode:
7031;;
7032;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7033;;
7034;; For Irix, no solution is known yet.
7035
00d6fd04
MA
7036(defconst tramp-local-coding-commands
7037 '((b64 base64-encode-region base64-decode-region)
7038 (uu tramp-uuencode-region uudecode-decode-region)
7039 (pack
7040 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7041 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7042 "List of local coding commands for inline transfer.
16674e4f
KG
7043Each item is a list that looks like this:
7044
b533bc97 7045\(FORMAT ENCODING DECODING\)
ac474af1 7046
00d6fd04
MA
7047FORMAT is symbol describing the encoding/decoding format. It can be
7048`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
ac474af1 7049
00d6fd04
MA
7050ENCODING and DECODING can be strings, giving commands, or symbols,
7051giving functions. If they are strings, then they can contain
16674e4f
KG
7052the \"%s\" format specifier. If that specifier is present, the input
7053filename will be put into the command line at that spot. If the
7054specifier is not present, the input should be read from standard
7055input.
ac474af1 7056
16674e4f
KG
7057If they are functions, they will be called with two arguments, start
7058and end of region, and are expected to replace the region contents
7059with the encoded or decoded results, respectively.")
ac474af1 7060
00d6fd04 7061(defconst tramp-remote-coding-commands
3dc847a3
MA
7062 '((b64 "base64" "base64 -d")
7063 (b64 "mimencode -b" "mimencode -u -b")
00d6fd04
MA
7064 (b64 "mmencode -b" "mmencode -u -b")
7065 (b64 "recode data..base64" "recode base64..data")
7066 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
7067 (b64 tramp-perl-encode tramp-perl-decode)
7068 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7069 (uu "uuencode xxx" "uudecode -o -")
7070 (uu "uuencode xxx" "uudecode -p")
7071 (uu "uuencode xxx" tramp-uudecode)
7072 (pack
7073 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7074 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7075 "List of remote coding commands for inline transfer.
7076Each item is a list that looks like this:
7077
b533bc97 7078\(FORMAT ENCODING DECODING\)
00d6fd04
MA
7079
7080FORMAT is symbol describing the encoding/decoding format. It can be
7081`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7082
7083ENCODING and DECODING can be strings, giving commands, or symbols,
7084giving variables. If they are strings, then they can contain
7085the \"%s\" format specifier. If that specifier is present, the input
7086filename will be put into the command line at that spot. If the
7087specifier is not present, the input should be read from standard
7088input.
7089
7090If they are variables, this variable is a string containing a Perl
7091implementation for this functionality. This Perl program will be transferred
db9e401b 7092to the remote host, and it is available as shell function with the same name.")
00d6fd04
MA
7093
7094(defun tramp-find-inline-encoding (vec)
ac474af1 7095 "Find an inline transfer encoding that works.
00d6fd04
MA
7096Goes through the list `tramp-local-coding-commands' and
7097`tramp-remote-coding-commands'."
7098 (save-excursion
7099 (let ((local-commands tramp-local-coding-commands)
7100 (magic "xyzzy")
7101 loc-enc loc-dec rem-enc rem-dec litem ritem found)
7102 (while (and local-commands (not found))
7103 (setq litem (pop local-commands))
7104 (catch 'wont-work-local
7105 (let ((format (nth 0 litem))
7106 (remote-commands tramp-remote-coding-commands))
7107 (setq loc-enc (nth 1 litem))
7108 (setq loc-dec (nth 2 litem))
7109 ;; If the local encoder or decoder is a string, the
7110 ;; corresponding command has to work locally.
7111 (if (not (stringp loc-enc))
7112 (tramp-message
7113 vec 5 "Checking local encoding function `%s'" loc-enc)
7114 (tramp-message
7115 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
7116 (unless (zerop (tramp-call-local-coding-command
7117 loc-enc nil nil))
7118 (throw 'wont-work-local nil)))
7119 (if (not (stringp loc-dec))
7120 (tramp-message
7121 vec 5 "Checking local decoding function `%s'" loc-dec)
7122 (tramp-message
7123 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
7124 (unless (zerop (tramp-call-local-coding-command
7125 loc-dec nil nil))
7126 (throw 'wont-work-local nil)))
7127 ;; Search for remote coding commands with the same format
7128 (while (and remote-commands (not found))
7129 (setq ritem (pop remote-commands))
7130 (catch 'wont-work-remote
7131 (when (equal format (nth 0 ritem))
7132 (setq rem-enc (nth 1 ritem))
7133 (setq rem-dec (nth 2 ritem))
7134 ;; Check if remote encoding and decoding commands can be
7135 ;; called remotely with null input and output. This makes
7136 ;; sure there are no syntax errors and the command is really
7137 ;; found. Note that we do not redirect stdout to /dev/null,
7138 ;; for two reasons: when checking the decoding command, we
7139 ;; actually check the output it gives. And also, when
7140 ;; redirecting "mimencode" output to /dev/null, then as root
7141 ;; it might change the permissions of /dev/null!
7142 (when (not (stringp rem-enc))
7143 (let ((name (symbol-name rem-enc)))
7144 (while (string-match (regexp-quote "-") name)
7145 (setq name (replace-match "_" nil t name)))
7146 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
7147 (setq rem-enc name)))
7148 (tramp-message
7149 vec 5
7150 "Checking remote encoding command `%s' for sanity" rem-enc)
7151 (unless (zerop (tramp-send-command-and-check
7152 vec (format "%s </dev/null" rem-enc) t))
7153 (throw 'wont-work-remote nil))
7154
7155 (when (not (stringp rem-dec))
7156 (let ((name (symbol-name rem-dec)))
7157 (while (string-match (regexp-quote "-") name)
7158 (setq name (replace-match "_" nil t name)))
7159 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
7160 (setq rem-dec name)))
7161 (tramp-message
7162 vec 5
7163 "Checking remote decoding command `%s' for sanity" rem-dec)
7164 (unless (zerop (tramp-send-command-and-check
7165 vec
7166 (format "echo %s | %s | %s"
b593f105
MA
7167 magic rem-enc rem-dec)
7168 t))
00d6fd04
MA
7169 (throw 'wont-work-remote nil))
7170
7171 (with-current-buffer (tramp-get-buffer vec)
7172 (goto-char (point-min))
7173 (unless (looking-at (regexp-quote magic))
7174 (throw 'wont-work-remote nil)))
7175
7176 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7177 (setq rem-enc (nth 1 ritem))
7178 (setq rem-dec (nth 2 ritem))
7179 (setq found t)))))))
7180
1d7e9a01 7181 ;; Did we find something?
00d6fd04 7182 (unless found
7e5686f0
MA
7183 (tramp-error
7184 vec 'file-error "Couldn't find an inline transfer encoding"))
00d6fd04
MA
7185
7186 ;; Set connection properties.
7187 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
7188 (tramp-set-connection-property vec "local-encoding" loc-enc)
7189 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
7190 (tramp-set-connection-property vec "local-decoding" loc-dec)
7191 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
7192 (tramp-set-connection-property vec "remote-encoding" rem-enc)
7193 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
7194 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
16674e4f
KG
7195
7196(defun tramp-call-local-coding-command (cmd input output)
7197 "Call the local encoding or decoding command.
7198If CMD contains \"%s\", provide input file INPUT there in command.
7199Otherwise, INPUT is passed via standard input.
7200INPUT can also be nil which means `/dev/null'.
7201OUTPUT can be a string (which specifies a filename), or t (which
7202means standard output and thus the current buffer), or nil (which
7203means discard it)."
a4aeb9a4
MA
7204 (tramp-local-call-process
7205 tramp-encoding-shell
7206 (when (and input (not (string-match "%s" cmd))) input)
7207 (if (eq output t) t nil)
7208 nil
7209 tramp-encoding-command-switch
7210 (concat
7211 (if (string-match "%s" cmd) (format cmd input) cmd)
7212 (if (stringp output) (concat "> " output) ""))))
00d6fd04 7213
6a29a838
MA
7214(defconst tramp-inline-compress-commands
7215 '(("gzip" "gzip -d")
7216 ("bzip2" "bzip2 -d")
7217 ("compress" "compress -d"))
7218 "List of compress and decompress commands for inline transfer.
7219Each item is a list that looks like this:
7220
7221\(COMPRESS DECOMPRESS\)
7222
7223COMPRESS or DECOMPRESS are strings with the respective commands.")
7224
7225(defun tramp-find-inline-compress (vec)
7226 "Find an inline transfer compress command that works.
7227Goes through the list `tramp-inline-compress-commands'."
7228 (save-excursion
7229 (let ((commands tramp-inline-compress-commands)
7230 (magic "xyzzy")
7231 item compress decompress
7232 found)
7233 (while (and commands (not found))
7234 (catch 'next
7235 (setq item (pop commands)
7236 compress (nth 0 item)
7237 decompress (nth 1 item))
7238 (tramp-message
7239 vec 5
7240 "Checking local compress command `%s', `%s' for sanity"
7241 compress decompress)
7242 (unless (zerop (tramp-call-local-coding-command
7243 (format "echo %s | %s | %s"
7244 magic compress decompress) nil nil))
7245 (throw 'next nil))
7246 (tramp-message
7247 vec 5
7248 "Checking remote compress command `%s', `%s' for sanity"
7249 compress decompress)
7250 (unless (zerop (tramp-send-command-and-check
7251 vec (format "echo %s | %s | %s"
7252 magic compress decompress) t))
7253 (throw 'next nil))
7254 (setq found t)))
7255
7256 ;; Did we find something?
7257 (if found
7258 (progn
7259 ;; Set connection properties.
7260 (tramp-message
7261 vec 5 "Using inline transfer compress command `%s'" compress)
7262 (tramp-set-connection-property vec "inline-compress" compress)
7263 (tramp-message
7264 vec 5 "Using inline transfer decompress command `%s'" decompress)
7265 (tramp-set-connection-property vec "inline-decompress" decompress))
7266
7267 (tramp-set-connection-property vec "inline-compress" nil)
7268 (tramp-set-connection-property vec "inline-decompress" nil)
7269 (tramp-message
7270 vec 2 "Couldn't find an inline transfer compress command")))))
7271
00d6fd04
MA
7272(defun tramp-compute-multi-hops (vec)
7273 "Expands VEC according to `tramp-default-proxies-alist'.
7274Gateway hops are already opened."
7275 (let ((target-alist `(,vec))
7276 (choices tramp-default-proxies-alist)
7277 item proxy)
7278
7279 ;; Look for proxy hosts to be passed.
7280 (while choices
7281 (setq item (pop choices)
70c11b0b 7282 proxy (eval (nth 2 item)))
00d6fd04
MA
7283 (when (and
7284 ;; host
70c11b0b 7285 (string-match (or (eval (nth 0 item)) "")
00d6fd04
MA
7286 (or (tramp-file-name-host (car target-alist)) ""))
7287 ;; user
70c11b0b 7288 (string-match (or (eval (nth 1 item)) "")
00d6fd04
MA
7289 (or (tramp-file-name-user (car target-alist)) "")))
7290 (if (null proxy)
7291 ;; No more hops needed.
7292 (setq choices nil)
7293 ;; Replace placeholders.
7294 (setq proxy
7295 (format-spec
7296 proxy
b533bc97
MA
7297 (format-spec-make
7298 ?u (or (tramp-file-name-user (car target-alist)) "")
7299 ?h (or (tramp-file-name-host (car target-alist)) ""))))
00d6fd04
MA
7300 (with-parsed-tramp-file-name proxy l
7301 ;; Add the hop.
7302 (add-to-list 'target-alist l)
7303 ;; Start next search.
7304 (setq choices tramp-default-proxies-alist)))))
7305
7306 ;; Handle gateways.
8a4438b6
MA
7307 (when (and (boundp 'tramp-gw-tunnel-method)
7308 (string-match (format
7309 "^\\(%s\\|%s\\)$"
7310 (symbol-value 'tramp-gw-tunnel-method)
7311 (symbol-value 'tramp-gw-socks-method))
7312 (tramp-file-name-method (car target-alist))))
00d6fd04
MA
7313 (let ((gw (pop target-alist))
7314 (hop (pop target-alist)))
7315 ;; Is the method prepared for gateways?
7316 (unless (tramp-get-method-parameter
7317 (tramp-file-name-method hop) 'tramp-default-port)
7318 (tramp-error
7319 vec 'file-error
7320 "Method `%s' is not supported for gateway access."
7321 (tramp-file-name-method hop)))
7322 ;; Add default port if needed.
7323 (unless
7324 (string-match
7325 tramp-host-with-port-regexp (tramp-file-name-host hop))
7326 (aset hop 2
7327 (concat
7328 (tramp-file-name-host hop) tramp-prefix-port-format
7329 (number-to-string
7330 (tramp-get-method-parameter
7331 (tramp-file-name-method hop) 'tramp-default-port)))))
7332 ;; Open the gateway connection.
7333 (add-to-list
7334 'target-alist
7335 (vector
7336 (tramp-file-name-method hop) (tramp-file-name-user hop)
0d5852cf 7337 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil))
00d6fd04
MA
7338 ;; For the password prompt, we need the correct values.
7339 ;; Therefore, we must remember the gateway vector. But we
7340 ;; cannot do it as connection property, because it shouldn't
7341 ;; be persistent. And we have no started process yet either.
7342 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
7343
7344 ;; Foreign and out-of-band methods are not supported for multi-hops.
7345 (when (cdr target-alist)
7346 (setq choices target-alist)
7347 (while choices
7348 (setq item (pop choices))
7349 (when
7350 (or
7351 (not
7352 (tramp-get-method-parameter
7353 (tramp-file-name-method item) 'tramp-login-program))
7354 (tramp-get-method-parameter
7355 (tramp-file-name-method item) 'tramp-copy-program))
7356 (tramp-error
7357 vec 'file-error
7358 "Method `%s' is not supported for multi-hops."
7359 (tramp-file-name-method item)))))
7360
2991e49f
MA
7361 ;; In case the host name is not used for the remote shell
7362 ;; command, the user could be misguided by applying a random
7363 ;; hostname.
7364 (let* ((v (car target-alist))
7365 (method (tramp-file-name-method v))
7366 (host (tramp-file-name-host v)))
7367 (unless
7368 (or
7369 ;; There are multi-hops.
7370 (cdr target-alist)
7371 ;; The host name is used for the remote shell command.
7372 (member
7373 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7374 ;; The host is local. We cannot use `tramp-local-host-p'
7375 ;; here, because it opens a connection as well.
b96e6899 7376 (string-match tramp-local-host-regexp host))
2991e49f 7377 (tramp-error
42bc9b6d
MA
7378 v 'file-error
7379 "Host `%s' looks like a remote host, `%s' can only use the local host"
7380 host method)))
2991e49f 7381
00d6fd04
MA
7382 ;; Result.
7383 target-alist))
7384
7385(defun tramp-maybe-open-connection (vec)
7386 "Maybe open a connection VEC.
fb7933a3
KG
7387Does not do anything if a connection is already open, but re-opens the
7388connection if a previous connection has died for some reason."
d8ac123e
MA
7389 (catch 'uname-changed
7390 (let ((p (tramp-get-connection-process vec))
7391 (process-environment (copy-sequence process-environment)))
7392
7393 ;; If too much time has passed since last command was sent, look
7394 ;; whether process is still alive. If it isn't, kill it. When
7395 ;; using ssh, it can sometimes happen that the remote end has
7396 ;; hung up but the local ssh client doesn't recognize this until
7397 ;; it tries to send some data to the remote end. So that's why
7398 ;; we try to send a command from time to time, then look again
7399 ;; whether the process is really alive.
7400 (condition-case nil
7401 (when (and (> (tramp-time-diff
7402 (current-time)
7403 (tramp-get-connection-property
7404 p "last-cmd-time" '(0 0 0)))
7405 60)
7406 p (processp p) (memq (process-status p) '(run open)))
7407 (tramp-send-command vec "echo are you awake" t t)
7408 (unless (and (memq (process-status p) '(run open))
7409 (tramp-wait-for-output p 10))
7410 ;; The error will be catched locally.
7411 (tramp-error vec 'file-error "Awake did fail")))
7412 (file-error
7413 (tramp-flush-connection-property vec)
7414 (tramp-flush-connection-property p)
7415 (delete-process p)
7416 (setq p nil)))
7417
7418 ;; New connection must be opened.
7419 (unless (and p (processp p) (memq (process-status p) '(run open)))
7420
7421 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7422 ;; messages from the beginning.
7423 (tramp-get-buffer vec)
7424 (if (zerop (length (tramp-file-name-user vec)))
7425 (tramp-message
7426 vec 3 "Opening connection for %s using %s..."
7427 (tramp-file-name-host vec)
7428 (tramp-file-name-method vec))
00d6fd04 7429 (tramp-message
d8ac123e
MA
7430 vec 3 "Opening connection for %s@%s using %s..."
7431 (tramp-file-name-user vec)
00d6fd04 7432 (tramp-file-name-host vec)
d8ac123e
MA
7433 (tramp-file-name-method vec)))
7434
7435 ;; Start new process.
7436 (when (and p (processp p))
7437 (delete-process p))
7438 (setenv "TERM" tramp-terminal-type)
7439 (setenv "LC_ALL" "C")
7440 (setenv "PROMPT_COMMAND")
dab816a9 7441 (setenv "PS1" tramp-initial-end-of-output)
d8ac123e
MA
7442 (let* ((target-alist (tramp-compute-multi-hops vec))
7443 (process-connection-type tramp-process-connection-type)
7444 (process-adaptive-read-buffering nil)
7445 (coding-system-for-read nil)
7446 ;; This must be done in order to avoid our file name handler.
7447 (p (let ((default-directory
7448 (tramp-compat-temporary-file-directory)))
7449 (start-process
7450 (or (tramp-get-connection-property vec "process-name" nil)
7451 (tramp-buffer-name vec))
7452 (tramp-get-connection-buffer vec)
70c11b0b 7453 tramp-encoding-shell))))
00d6fd04 7454
d8ac123e
MA
7455 (tramp-message
7456 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
7457
7458 ;; Check whether process is alive.
d8ac123e 7459 (tramp-set-process-query-on-exit-flag p nil)
a94d821f
MA
7460 (with-progress-reporter vec 3 "Waiting 60s for local shell to come up"
7461 (tramp-barf-if-no-shell-prompt
7462 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell))
d8ac123e
MA
7463
7464 ;; Now do all the connections as specified.
7465 (while target-alist
7466 (let* ((hop (car target-alist))
7467 (l-method (tramp-file-name-method hop))
7468 (l-user (tramp-file-name-user hop))
7469 (l-host (tramp-file-name-host hop))
7470 (l-port nil)
7471 (login-program
7472 (tramp-get-method-parameter l-method 'tramp-login-program))
7473 (login-args
7474 (tramp-get-method-parameter l-method 'tramp-login-args))
7475 (gw-args
7476 (tramp-get-method-parameter l-method 'tramp-gw-args))
7477 (gw (tramp-get-file-property hop "" "gateway" nil))
7478 (g-method (and gw (tramp-file-name-method gw)))
7479 (g-user (and gw (tramp-file-name-user gw)))
7480 (g-host (and gw (tramp-file-name-host gw)))
7481 (command login-program)
7482 ;; We don't create the temporary file. In fact, it
7483 ;; is just a prefix for the ControlPath option of
7484 ;; ssh; the real temporary file has another name, and
7485 ;; it is created and protected by ssh. It is also
7486 ;; removed by ssh, when the connection is closed.
7487 (tmpfile
7488 (tramp-set-connection-property
7489 p "temp-file"
7490 (make-temp-name
7491 (expand-file-name
7492 tramp-temp-name-prefix
7493 (tramp-compat-temporary-file-directory)))))
7494 spec)
7495
7496 ;; Add gateway arguments if necessary.
7497 (when (and gw gw-args)
7498 (setq login-args (append login-args gw-args)))
7499
7500 ;; Check for port number. Until now, there's no need
7501 ;; for handling like method, user, host.
7502 (when (string-match tramp-host-with-port-regexp l-host)
7503 (setq l-port (match-string 2 l-host)
7504 l-host (match-string 1 l-host)))
7505
7506 ;; Set variables for computing the prompt for reading
2296b54d 7507 ;; password. They can also be derived from a gateway.
d8ac123e
MA
7508 (setq tramp-current-method (or g-method l-method)
7509 tramp-current-user (or g-user l-user)
7510 tramp-current-host (or g-host l-host))
7511
7512 ;; Replace login-args place holders.
7513 (setq
7514 l-host (or l-host "")
7515 l-user (or l-user "")
7516 l-port (or l-port "")
b533bc97 7517 spec (format-spec-make ?h l-host ?u l-user ?p l-port ?t tmpfile)
d8ac123e
MA
7518 command
7519 (concat
e2a421af
MA
7520 ;; We do not want to see the trailing local prompt in
7521 ;; `start-file-process'.
7522 (unless (memq system-type '(windows-nt)) "exec ")
d8ac123e
MA
7523 command " "
7524 (mapconcat
aa485f7c
MA
7525 (lambda (x)
7526 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7527 (unless (member "" x) (mapconcat 'identity x " ")))
d8ac123e 7528 login-args " ")
d8ac123e
MA
7529 ;; Local shell could be a Windows COMSPEC. It doesn't
7530 ;; know the ";" syntax, but we must exit always for
70c11b0b 7531 ;; `start-file-process'. "exec" does not work either.
e2a421af 7532 (if (memq system-type '(windows-nt)) " && exit || exit")))
d8ac123e
MA
7533
7534 ;; Send the command.
7535 (tramp-message vec 3 "Sending command `%s'" command)
7536 (tramp-send-command vec command t t)
7537 (tramp-process-actions p vec tramp-actions-before-shell 60)
7538 (tramp-message vec 3 "Found remote shell prompt on `%s'" l-host))
7539 ;; Next hop.
7540 (setq target-alist (cdr target-alist)))
7541
7542 ;; Make initial shell settings.
7543 (tramp-open-connection-setup-interactive-shell p vec))))))
00d6fd04
MA
7544
7545(defun tramp-send-command (vec command &optional neveropen nooutput)
7546 "Send the COMMAND to connection VEC.
7547Erases temporary buffer before sending the command. If optional
7548arg NEVEROPEN is non-nil, never try to open the connection. This
7549is meant to be used from `tramp-maybe-open-connection' only. The
7550function waits for output unless NOOUTPUT is set."
7551 (unless neveropen (tramp-maybe-open-connection vec))
7552 (let ((p (tramp-get-connection-process vec)))
8950769a 7553 (when (tramp-get-connection-property p "remote-echo" nil)
00d6fd04
MA
7554 ;; We mark the command string that it can be erased in the output buffer.
7555 (tramp-set-connection-property p "check-remote-echo" t)
7556 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7557 (tramp-message vec 6 "%s" command)
7558 (tramp-send-string vec command)
7559 (unless nooutput (tramp-wait-for-output p))))
7560
00d6fd04 7561(defun tramp-wait-for-output (proc &optional timeout)
7e5686f0
MA
7562 "Wait for output from remote command."
7563 (unless (buffer-live-p (process-buffer proc))
7564 (delete-process proc)
7565 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
00d6fd04 7566 (with-current-buffer (process-buffer proc)
dab816a9 7567 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
bede3e9f 7568 ;; be leading escape sequences, which must be ignored.
dab816a9 7569 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
bede3e9f
MA
7570 ;; Sometimes, the commands do not return a newline but a
7571 ;; null byte before the shell prompt, for example "git
7572 ;; ls-files -c -z ...".
7573 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7574 (found (tramp-wait-for-regexp proc timeout regexp1)))
00d6fd04
MA
7575 (if found
7576 (let (buffer-read-only)
7e5686f0
MA
7577 ;; A simple-minded busybox has sent " ^H" sequences.
7578 ;; Delete them.
7579 (goto-char (point-min))
7580 (when (re-search-forward
7581 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
7582 (forward-line 1)
7583 (delete-region (point-min) (point)))
7584 ;; Delete the prompt.
00d6fd04 7585 (goto-char (point-max))
0664ff72 7586 (re-search-backward regexp nil t)
00d6fd04
MA
7587 (delete-region (point) (point-max)))
7588 (if timeout
7589 (tramp-error
7590 proc 'file-error
7591 "[[Remote prompt `%s' not found in %d secs]]"
7592 tramp-end-of-output timeout)
7593 (tramp-error
7594 proc 'file-error
7595 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7596 ;; Return value is whether end-of-output sentinel was found.
7597 found)))
fb7933a3 7598
b593f105
MA
7599(defun tramp-send-command-and-check
7600 (vec command &optional subshell dont-suppress-err)
fb7933a3 7601 "Run COMMAND and check its exit status.
fb7933a3
KG
7602Sends `echo $?' along with the COMMAND for checking the exit status. If
7603COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7604
b593f105
MA
7605If the optional argument SUBSHELL is non-nil, the command is
7606executed in a subshell, ie surrounded by parentheses. If
7607DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
00d6fd04
MA
7608 (tramp-send-command
7609 vec
7610 (concat (if subshell "( " "")
7611 command
b593f105 7612 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
00d6fd04 7613 "echo tramp_exit_status $?"
b593f105 7614 (if subshell " )" "")))
00d6fd04
MA
7615 (with-current-buffer (tramp-get-connection-buffer vec)
7616 (goto-char (point-max))
7617 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7618 (tramp-error
7619 vec 'file-error "Couldn't find exit status of `%s'" command))
7620 (skip-chars-forward "^ ")
7621 (prog1
7622 (read (current-buffer))
7623 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7624
7625(defun tramp-barf-unless-okay (vec command fmt &rest args)
fb7933a3
KG
7626 "Run COMMAND, check exit status, throw error if exit status not okay.
7627Similar to `tramp-send-command-and-check' but accepts two more arguments
7628FMT and ARGS which are passed to `error'."
00d6fd04
MA
7629 (unless (zerop (tramp-send-command-and-check vec command))
7630 (apply 'tramp-error vec 'file-error fmt args)))
7631
7632(defun tramp-send-command-and-read (vec command)
7633 "Run COMMAND and return the output, which must be a Lisp expression.
7634In case there is no valid Lisp expression, it raises an error"
7635 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7636 (with-current-buffer (tramp-get-connection-buffer vec)
7637 ;; Read the expression.
7638 (goto-char (point-min))
7639 (condition-case nil
7640 (prog1 (read (current-buffer))
7641 ;; Error handling.
9e6ab520 7642 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
9ce8462a 7643 (error nil)))
00d6fd04
MA
7644 (error (tramp-error
7645 vec 'file-error
7646 "`%s' does not return a valid Lisp expression: `%s'"
7647 command (buffer-string))))))
fb7933a3 7648
7432277c
KG
7649;; It seems that Tru64 Unix does not like it if long strings are sent
7650;; to it in one go. (This happens when sending the Perl
7651;; `file-attributes' implementation, for instance.) Therefore, we
27e813fe 7652;; have this function which sends the string in chunks.
00d6fd04
MA
7653(defun tramp-send-string (vec string)
7654 "Send the STRING via connection VEC.
7432277c
KG
7655
7656The STRING is expected to use Unix line-endings, but the lines sent to
7657the remote host use line-endings as defined in the variable
00d6fd04
MA
7658`tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7659 (let* ((p (tramp-get-connection-process vec))
7660 (chunksize (tramp-get-connection-property p "chunksize" nil)))
7661 (unless p
7662 (tramp-error
7663 vec 'file-error "Can't send string to remote host -- not logged in"))
7664 (tramp-set-connection-property p "last-cmd-time" (current-time))
7665 (tramp-message vec 10 "%s" string)
7666 (with-current-buffer (tramp-get-connection-buffer vec)
7667 ;; Clean up the buffer. We cannot call `erase-buffer' because
7668 ;; narrowing might be in effect.
7669 (let (buffer-read-only) (delete-region (point-min) (point-max)))
27e813fe 7670 ;; Replace "\n" by `tramp-rsh-end-of-line'.
00d6fd04
MA
7671 (setq string
7672 (mapconcat 'identity
70c11b0b 7673 (tramp-compat-split-string string "\n")
00d6fd04
MA
7674 tramp-rsh-end-of-line))
7675 (unless (or (string= string "")
7676 (string-equal (substring string -1) tramp-rsh-end-of-line))
7677 (setq string (concat string tramp-rsh-end-of-line)))
27e813fe 7678 ;; Send the string.
00d6fd04
MA
7679 (if (and chunksize (not (zerop chunksize)))
7680 (let ((pos 0)
7681 (end (length string)))
7682 (while (< pos end)
7683 (tramp-message
7684 vec 10 "Sending chunk from %s to %s"
7685 pos (min (+ pos chunksize) end))
7686 (process-send-string
7687 p (substring string pos (min (+ pos chunksize) end)))
7688 (setq pos (+ pos chunksize))))
7689 (process-send-string p string)))))
fb7933a3
KG
7690
7691(defun tramp-mode-string-to-int (mode-string)
7692 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
f3c071dd
MA
7693 (let* (case-fold-search
7694 (mode-chars (string-to-vector mode-string))
fb7933a3
KG
7695 (owner-read (aref mode-chars 1))
7696 (owner-write (aref mode-chars 2))
7697 (owner-execute-or-setid (aref mode-chars 3))
7698 (group-read (aref mode-chars 4))
7699 (group-write (aref mode-chars 5))
7700 (group-execute-or-setid (aref mode-chars 6))
7701 (other-read (aref mode-chars 7))
7702 (other-write (aref mode-chars 8))
7703 (other-execute-or-sticky (aref mode-chars 9)))
7704 (save-match-data
7705 (logior
f3c071dd
MA
7706 (cond
7707 ((char-equal owner-read ?r) (tramp-octal-to-decimal "00400"))
7708 ((char-equal owner-read ?-) 0)
7709 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7710 (cond
7711 ((char-equal owner-write ?w) (tramp-octal-to-decimal "00200"))
7712 ((char-equal owner-write ?-) 0)
7713 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7714 (cond
7715 ((char-equal owner-execute-or-setid ?x)
7716 (tramp-octal-to-decimal "00100"))
7717 ((char-equal owner-execute-or-setid ?S)
7718 (tramp-octal-to-decimal "04000"))
7719 ((char-equal owner-execute-or-setid ?s)
7720 (tramp-octal-to-decimal "04100"))
7721 ((char-equal owner-execute-or-setid ?-) 0)
7722 (t (error "Fourth char `%c' must be one of `xsS-'"
7723 owner-execute-or-setid)))
7724 (cond
7725 ((char-equal group-read ?r) (tramp-octal-to-decimal "00040"))
7726 ((char-equal group-read ?-) 0)
7727 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7728 (cond
7729 ((char-equal group-write ?w) (tramp-octal-to-decimal "00020"))
7730 ((char-equal group-write ?-) 0)
7731 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7732 (cond
7733 ((char-equal group-execute-or-setid ?x)
7734 (tramp-octal-to-decimal "00010"))
7735 ((char-equal group-execute-or-setid ?S)
7736 (tramp-octal-to-decimal "02000"))
7737 ((char-equal group-execute-or-setid ?s)
7738 (tramp-octal-to-decimal "02010"))
7739 ((char-equal group-execute-or-setid ?-) 0)
7740 (t (error "Seventh char `%c' must be one of `xsS-'"
7741 group-execute-or-setid)))
7742 (cond
7743 ((char-equal other-read ?r)
7744 (tramp-octal-to-decimal "00004"))
7745 ((char-equal other-read ?-) 0)
7746 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7747 (cond
7748 ((char-equal other-write ?w) (tramp-octal-to-decimal "00002"))
7749 ((char-equal other-write ?-) 0)
fb7933a3 7750 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
f3c071dd
MA
7751 (cond
7752 ((char-equal other-execute-or-sticky ?x)
7753 (tramp-octal-to-decimal "00001"))
7754 ((char-equal other-execute-or-sticky ?T)
7755 (tramp-octal-to-decimal "01000"))
7756 ((char-equal other-execute-or-sticky ?t)
7757 (tramp-octal-to-decimal "01001"))
7758 ((char-equal other-execute-or-sticky ?-) 0)
7759 (t (error "Tenth char `%c' must be one of `xtT-'"
7760 other-execute-or-sticky)))))))
fb7933a3 7761
00d6fd04
MA
7762(defun tramp-convert-file-attributes (vec attr)
7763 "Convert file-attributes ATTR generated by perl script, stat or ls.
c82c5727
LH
7764Convert file mode bits to string and set virtual device number.
7765Return ATTR."
680db9ac
MA
7766 (when attr
7767 ;; Convert last access time.
7768 (unless (listp (nth 4 attr))
7769 (setcar (nthcdr 4 attr)
7770 (list (floor (nth 4 attr) 65536)
7771 (floor (mod (nth 4 attr) 65536)))))
7772 ;; Convert last modification time.
7773 (unless (listp (nth 5 attr))
7774 (setcar (nthcdr 5 attr)
7775 (list (floor (nth 5 attr) 65536)
7776 (floor (mod (nth 5 attr) 65536)))))
7777 ;; Convert last status change time.
7778 (unless (listp (nth 6 attr))
7779 (setcar (nthcdr 6 attr)
7780 (list (floor (nth 6 attr) 65536)
7781 (floor (mod (nth 6 attr) 65536)))))
7782 ;; Convert file size.
7783 (when (< (nth 7 attr) 0)
7784 (setcar (nthcdr 7 attr) -1))
7785 (when (and (floatp (nth 7 attr))
7786 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7787 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7788 ;; Convert file mode bits to string.
7789 (unless (stringp (nth 8 attr))
7790 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7791 (when (stringp (car attr))
7792 (aset (nth 8 attr) 0 ?l)))
7793 ;; Convert directory indication bit.
7794 (when (string-match "^d" (nth 8 attr))
7795 (setcar attr t))
7796 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7797 (when (consp (car attr))
7798 (if (and (stringp (caar attr))
7799 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7800 (setcar attr (match-string 1 (caar attr)))
7801 (setcar attr nil)))
7802 ;; Set file's gid change bit.
7803 (setcar (nthcdr 9 attr)
7804 (if (numberp (nth 3 attr))
7805 (not (= (nth 3 attr)
7806 (tramp-get-remote-gid vec 'integer)))
7807 (not (string-equal
7808 (nth 3 attr)
7809 (tramp-get-remote-gid vec 'string)))))
7810 ;; Convert inode.
7811 (unless (listp (nth 10 attr))
7812 (setcar (nthcdr 10 attr)
7813 (condition-case nil
7814 (cons (floor (nth 10 attr) 65536)
7815 (floor (mod (nth 10 attr) 65536)))
7816 ;; Inodes can be incredible huge. We must hide this.
7817 (error (tramp-get-inode vec)))))
7818 ;; Set virtual device number.
7819 (setcar (nthcdr 11 attr)
7820 (tramp-get-device vec))
7821 attr))
c82c5727 7822
293c24f9
MA
7823(defun tramp-check-cached-permissions (vec access)
7824 "Check `file-attributes' caches for VEC.
7825Return t if according to the cache access type ACCESS is known to
7826be granted."
7827 (let ((result nil)
7828 (offset (cond
7829 ((eq ?r access) 1)
7830 ((eq ?w access) 2)
7831 ((eq ?x access) 3))))
7832 (dolist (suffix '("string" "integer") result)
7833 (setq
7834 result
7835 (or
7836 result
7837 (let ((file-attr
7838 (tramp-get-file-property
7839 vec (tramp-file-name-localname vec)
7840 (concat "file-attributes-" suffix) nil))
7841 (remote-uid
7842 (tramp-get-connection-property
7843 vec (concat "uid-" suffix) nil))
7844 (remote-gid
7845 (tramp-get-connection-property
7846 vec (concat "gid-" suffix) nil)))
7847 (and
7848 file-attr
7849 (or
7850 ;; Not a symlink
7851 (eq t (car file-attr))
7852 (null (car file-attr)))
7853 (or
7854 ;; World accessible.
7855 (eq access (aref (nth 8 file-attr) (+ offset 6)))
7856 ;; User accessible and owned by user.
7857 (and
7858 (eq access (aref (nth 8 file-attr) offset))
7859 (equal remote-uid (nth 2 file-attr)))
7860 ;; Group accessible and owned by user's
7861 ;; principal group.
7862 (and
7863 (eq access (aref (nth 8 file-attr) (+ offset 3)))
7864 (equal remote-gid (nth 3 file-attr)))))))))))
7865
ce3f516f 7866(defun tramp-get-inode (vec)
00d6fd04
MA
7867 "Returns the virtual inode number.
7868If it doesn't exist, generate a new one."
ce3f516f
MA
7869 (let ((string (tramp-make-tramp-file-name
7870 (tramp-file-name-method vec)
7871 (tramp-file-name-user vec)
7872 (tramp-file-name-host vec)
7873 "")))
00d6fd04
MA
7874 (unless (assoc string tramp-inodes)
7875 (add-to-list 'tramp-inodes
7876 (list string (length tramp-inodes))))
7877 (nth 1 (assoc string tramp-inodes))))
7878
7879(defun tramp-get-device (vec)
c82c5727
LH
7880 "Returns the virtual device number.
7881If it doesn't exist, generate a new one."
00d6fd04
MA
7882 (let ((string (tramp-make-tramp-file-name
7883 (tramp-file-name-method vec)
7884 (tramp-file-name-user vec)
7885 (tramp-file-name-host vec)
7886 "")))
c82c5727
LH
7887 (unless (assoc string tramp-devices)
7888 (add-to-list 'tramp-devices
7889 (list string (length tramp-devices))))
b946a456 7890 (cons -1 (nth 1 (assoc string tramp-devices)))))
fb7933a3
KG
7891
7892(defun tramp-file-mode-from-int (mode)
7893 "Turn an integer representing a file mode into an ls(1)-like string."
7894 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
7895 (user (logand (lsh mode -6) 7))
7896 (group (logand (lsh mode -3) 7))
7897 (other (logand (lsh mode -0) 7))
7898 (suid (> (logand (lsh mode -9) 4) 0))
7899 (sgid (> (logand (lsh mode -9) 2) 0))
7900 (sticky (> (logand (lsh mode -9) 1) 0)))
7901 (setq user (tramp-file-mode-permissions user suid "s"))
7902 (setq group (tramp-file-mode-permissions group sgid "s"))
7903 (setq other (tramp-file-mode-permissions other sticky "t"))
7904 (concat type user group other)))
7905
fb7933a3
KG
7906(defun tramp-file-mode-permissions (perm suid suid-text)
7907 "Convert a permission bitset into a string.
7908This is used internally by `tramp-file-mode-from-int'."
7909 (let ((r (> (logand perm 4) 0))
7910 (w (> (logand perm 2) 0))
7911 (x (> (logand perm 1) 0)))
7912 (concat (or (and r "r") "-")
7913 (or (and w "w") "-")
7914 (or (and suid x suid-text) ; suid, execute
7915 (and suid (upcase suid-text)) ; suid, !execute
7916 (and x "x") "-")))) ; !suid
7917
fb7933a3
KG
7918(defun tramp-decimal-to-octal (i)
7919 "Return a string consisting of the octal digits of I.
7920Not actually used. Use `(format \"%o\" i)' instead?"
7921 (cond ((< i 0) (error "Cannot convert negative number to octal"))
7922 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
7923 ((zerop i) "0")
7924 (t (concat (tramp-decimal-to-octal (/ i 8))
7925 (number-to-string (% i 8))))))
7926
fb7933a3
KG
7927;; Kudos to Gerd Moellmann for this suggestion.
7928(defun tramp-octal-to-decimal (ostr)
7929 "Given a string of octal digits, return a decimal number."
7930 (let ((x (or ostr "")))
7931 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7932 (unless (string-match "\\`[0-7]*\\'" x)
7933 (error "Non-octal junk in string `%s'" x))
7934 (string-to-number ostr 8)))
7935
7936(defun tramp-shell-case-fold (string)
7937 "Converts STRING to shell glob pattern which ignores case."
7938 (mapconcat
7939 (lambda (c)
7940 (if (equal (downcase c) (upcase c))
7941 (vector c)
7942 (format "[%c%c]" (downcase c) (upcase c))))
7943 string
7944 ""))
7945
7946
bf247b6e 7947;; ------------------------------------------------------------
a4aeb9a4 7948;; -- Tramp file names --
bf247b6e 7949;; ------------------------------------------------------------
fb7933a3
KG
7950;; Conversion functions between external representation and
7951;; internal data structure. Convenience functions for internal
7952;; data structure.
7953
00d6fd04 7954(defun tramp-file-name-p (vec)
a94d821f 7955 "Check, whether VEC is a Tramp object."
00d6fd04
MA
7956 (and (vectorp vec) (= 4 (length vec))))
7957
7958(defun tramp-file-name-method (vec)
7959 "Return method component of VEC."
7960 (and (tramp-file-name-p vec) (aref vec 0)))
7961
7962(defun tramp-file-name-user (vec)
7963 "Return user component of VEC."
7964 (and (tramp-file-name-p vec) (aref vec 1)))
7965
7966(defun tramp-file-name-host (vec)
7967 "Return host component of VEC."
7968 (and (tramp-file-name-p vec) (aref vec 2)))
7969
7970(defun tramp-file-name-localname (vec)
7971 "Return localname component of VEC."
7972 (and (tramp-file-name-p vec) (aref vec 3)))
7973
dea31ca6 7974;; The user part of a Tramp file name vector can be of kind
b96e6899 7975;; "user%domain". Sometimes, we must extract these parts.
dea31ca6
MA
7976(defun tramp-file-name-real-user (vec)
7977 "Return the user name of VEC without domain."
a17632c1
MA
7978 (save-match-data
7979 (let ((user (tramp-file-name-user vec)))
7980 (if (and (stringp user)
7981 (string-match tramp-user-with-domain-regexp user))
7982 (match-string 1 user)
7983 user))))
dea31ca6
MA
7984
7985(defun tramp-file-name-domain (vec)
7986 "Return the domain name of VEC."
a17632c1
MA
7987 (save-match-data
7988 (let ((user (tramp-file-name-user vec)))
7989 (and (stringp user)
7990 (string-match tramp-user-with-domain-regexp user)
7991 (match-string 2 user)))))
dea31ca6 7992
00d6fd04
MA
7993;; The host part of a Tramp file name vector can be of kind
7994;; "host#port". Sometimes, we must extract these parts.
8a4438b6 7995(defun tramp-file-name-real-host (vec)
00d6fd04 7996 "Return the host name of VEC without port."
a17632c1
MA
7997 (save-match-data
7998 (let ((host (tramp-file-name-host vec)))
7999 (if (and (stringp host)
8000 (string-match tramp-host-with-port-regexp host))
8001 (match-string 1 host)
8002 host))))
00d6fd04 8003
8a4438b6 8004(defun tramp-file-name-port (vec)
00d6fd04 8005 "Return the port number of VEC."
a17632c1
MA
8006 (save-match-data
8007 (let ((host (tramp-file-name-host vec)))
8008 (and (stringp host)
8009 (string-match tramp-host-with-port-regexp host)
8010 (string-to-number (match-string 2 host))))))
fb7933a3
KG
8011
8012(defun tramp-tramp-file-p (name)
a09dc9bf 8013 "Return t if NAME is a string with Tramp file name syntax."
fb7933a3 8014 (save-match-data
a09dc9bf 8015 (and (stringp name) (string-match tramp-file-name-regexp name))))
bf247b6e 8016
8a4438b6 8017(defun tramp-find-method (method user host)
00d6fd04
MA
8018 "Return the right method string to use.
8019This is METHOD, if non-nil. Otherwise, do a lookup in
8020`tramp-default-method-alist'."
8021 (or method
8022 (let ((choices tramp-default-method-alist)
8023 lmethod item)
8024 (while choices
8025 (setq item (pop choices))
8026 (when (and (string-match (or (nth 0 item) "") (or host ""))
8027 (string-match (or (nth 1 item) "") (or user "")))
8028 (setq lmethod (nth 2 item))
8029 (setq choices nil)))
8030 lmethod)
8031 tramp-default-method))
8032
8a4438b6 8033(defun tramp-find-user (method user host)
00d6fd04
MA
8034 "Return the right user string to use.
8035This is USER, if non-nil. Otherwise, do a lookup in
8036`tramp-default-user-alist'."
8037 (or user
8038 (let ((choices tramp-default-user-alist)
8039 luser item)
8040 (while choices
8041 (setq item (pop choices))
8042 (when (and (string-match (or (nth 0 item) "") (or method ""))
8043 (string-match (or (nth 1 item) "") (or host "")))
8044 (setq luser (nth 2 item))
8045 (setq choices nil)))
8046 luser)
8047 tramp-default-user))
8048
8a4438b6 8049(defun tramp-find-host (method user host)
00d6fd04
MA
8050 "Return the right host string to use.
8051This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
8052 (or (and (> (length host) 0) host)
8053 tramp-default-host))
8054
9ce8462a 8055(defun tramp-dissect-file-name (name &optional nodefault)
00d6fd04 8056 "Return a `tramp-file-name' structure.
9ce8462a
MA
8057The structure consists of remote method, remote user, remote host
8058and localname (file name on remote host). If NODEFAULT is
8059non-nil, the file name parts are not expanded to their default
8060values."
4007ba5b 8061 (save-match-data
00d6fd04 8062 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
a4aeb9a4 8063 (unless match (error "Not a Tramp file name: %s" name))
00d6fd04
MA
8064 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
8065 (user (match-string (nth 2 tramp-file-name-structure) name))
8066 (host (match-string (nth 3 tramp-file-name-structure) name))
8067 (localname (match-string (nth 4 tramp-file-name-structure) name)))
5d449a36
MA
8068 (when (member method '("multi" "multiu"))
8069 (error
8070 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
8071 method))
b96e6899
MA
8072 (when host
8073 (when (string-match tramp-prefix-ipv6-regexp host)
8074 (setq host (replace-match "" nil t host)))
8075 (when (string-match tramp-postfix-ipv6-regexp host)
8076 (setq host (replace-match "" nil t host))))
9ce8462a
MA
8077 (if nodefault
8078 (vector method user host localname)
8079 (vector
8080 (tramp-find-method method user host)
8081 (tramp-find-user method user host)
8082 (tramp-find-host method user host)
8083 localname))))))
00d6fd04
MA
8084
8085(defun tramp-equal-remote (file1 file2)
a94d821f 8086 "Check, whether the remote parts of FILE1 and FILE2 are identical.
00d6fd04
MA
8087The check depends on method, user and host name of the files. If
8088one of the components is missing, the default values are used.
8089The local file name parts of FILE1 and FILE2 are not taken into
8090account.
fb7933a3 8091
00d6fd04
MA
8092Example:
8093
8094 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
8095
8096would yield `t'. On the other hand, the following check results in nil:
8097
8098 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
9e6ab520
MA
8099 (and (stringp (file-remote-p file1))
8100 (stringp (file-remote-p file2))
94be87e8 8101 (string-equal (file-remote-p file1) (file-remote-p file2))))
00d6fd04
MA
8102
8103(defun tramp-make-tramp-file-name (method user host localname)
8104 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
8105 (concat tramp-prefix-format
8106 (when (not (zerop (length method)))
8107 (concat method tramp-postfix-method-format))
8108 (when (not (zerop (length user)))
8109 (concat user tramp-postfix-user-format))
b96e6899
MA
8110 (when host
8111 (if (string-match tramp-ipv6-regexp host)
8112 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8113 host))
8114 tramp-postfix-host-format
00d6fd04
MA
8115 (when localname localname)))
8116
8117(defun tramp-completion-make-tramp-file-name (method user host localname)
8118 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
8119It must not be a complete Tramp file name, but as long as there are
8120necessary only. This function will be used in file name completion."
8121 (concat tramp-prefix-format
8122 (when (not (zerop (length method)))
8123 (concat method tramp-postfix-method-format))
8124 (when (not (zerop (length user)))
8125 (concat user tramp-postfix-user-format))
8126 (when (not (zerop (length host)))
b96e6899
MA
8127 (concat
8128 (if (string-match tramp-ipv6-regexp host)
8129 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8130 host)
8131 tramp-postfix-host-format))
00d6fd04
MA
8132 (when localname localname)))
8133
8134(defun tramp-make-copy-program-file-name (vec)
8135 "Create a file name suitable to be passed to `rcp' and workalikes."
8136 (let ((user (tramp-file-name-user vec))
0f205eee 8137 (host (tramp-file-name-real-host vec))
00d6fd04
MA
8138 (localname (tramp-shell-quote-argument
8139 (tramp-file-name-localname vec))))
8140 (if (not (zerop (length user)))
8141 (format "%s@%s:%s" user host localname)
8142 (format "%s:%s" host localname))))
8143
7f49fe46 8144(defun tramp-method-out-of-band-p (vec size)
38c65fca 8145 "Return t if this is an out-of-band method, nil otherwise."
7f49fe46
MA
8146 (and
8147 ;; It shall be an out-of-band method.
8148 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
8149 ;; Either the file size is large enough, or (in rare cases) there
8150 ;; does not exist a remote encoding.
6a29a838
MA
8151 (or (null tramp-copy-size-limit)
8152 (> size tramp-copy-size-limit)
8153 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
fb7933a3 8154
0f205eee
MA
8155(defun tramp-local-host-p (vec)
8156 "Return t if this points to the local host, nil otherwise."
c992abdb
MA
8157 ;; We cannot use `tramp-file-name-real-host'. A port is an
8158 ;; indication for an ssh tunnel or alike.
8159 (let ((host (tramp-file-name-host vec)))
0f205eee
MA
8160 (and
8161 (stringp host)
b96e6899 8162 (string-match tramp-local-host-regexp host)
46bcd78c
MA
8163 ;; The method shall be applied to one of the shell file name
8164 ;; handler. `tramp-local-host-p' is also called for "smb" and
8165 ;; alike, where it must fail.
8166 (tramp-get-method-parameter
8167 (tramp-file-name-method vec) 'tramp-login-program)
a0a5183a
MA
8168 ;; The local temp directory must be writable for the other user.
8169 (file-writable-p
8170 (tramp-make-tramp-file-name
8171 (tramp-file-name-method vec)
8172 (tramp-file-name-user vec)
8173 host
93c3eb7c
MA
8174 (tramp-compat-temporary-file-directory)))
8175 ;; On some systems, chown runs only for root.
8176 (or (zerop (user-uid))
8177 (zerop (tramp-get-remote-uid vec 'integer))))))
0f205eee 8178
fb7933a3
KG
8179;; Variables local to connection.
8180
f84638eb 8181(defun tramp-get-remote-path (vec)
70c11b0b
MA
8182 (with-connection-property
8183 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8184 ;; cache the result for the session only. Otherwise, the result
8185 ;; is cached persistently.
8186 (if (memq 'tramp-own-remote-path tramp-remote-path)
8187 (tramp-get-connection-process vec)
8188 vec)
8189 "remote-path"
b533bc97 8190 (let* ((remote-path (copy-tree tramp-remote-path))
70c11b0b
MA
8191 (elt1 (memq 'tramp-default-remote-path remote-path))
8192 (elt2 (memq 'tramp-own-remote-path remote-path))
f84638eb 8193 (default-remote-path
70c11b0b 8194 (when elt1
f84638eb 8195 (condition-case nil
70c11b0b
MA
8196 (tramp-send-command-and-read
8197 vec "echo \\\"`getconf PATH`\\\"")
f84638eb
MA
8198 ;; Default if "getconf" is not available.
8199 (error
8200 (tramp-message
8201 vec 3
8202 "`getconf PATH' not successful, using default value \"%s\"."
8203 "/bin:/usr/bin")
70c11b0b
MA
8204 "/bin:/usr/bin"))))
8205 (own-remote-path
8206 (when elt2
8207 (condition-case nil
8208 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
8209 ;; Default if "getconf" is not available.
8210 (error
8211 (tramp-message
8212 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8213 nil)))))
8214
8215 ;; Replace place holder `tramp-default-remote-path'.
8216 (when elt1
8217 (setcdr elt1
f84638eb 8218 (append
70c11b0b
MA
8219 (tramp-compat-split-string default-remote-path ":")
8220 (cdr elt1)))
f84638eb
MA
8221 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
8222
70c11b0b
MA
8223 ;; Replace place holder `tramp-own-remote-path'.
8224 (when elt2
8225 (setcdr elt2
8226 (append
8227 (tramp-compat-split-string own-remote-path ":")
8228 (cdr elt2)))
8229 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
8230
8231 ;; Remove double entries.
8232 (setq elt1 remote-path)
8233 (while (consp elt1)
8234 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
8235 (setcar elt2 nil))
8236 (setq elt1 (cdr elt1)))
8237
f84638eb
MA
8238 ;; Remove non-existing directories.
8239 (delq
8240 nil
8241 (mapcar
8242 (lambda (x)
8243 (and
70c11b0b
MA
8244 (stringp x)
8245 (file-directory-p
8246 (tramp-make-tramp-file-name
8247 (tramp-file-name-method vec)
8248 (tramp-file-name-user vec)
8249 (tramp-file-name-host vec)
8250 x))
f84638eb
MA
8251 x))
8252 remote-path)))))
8253
a4aeb9a4
MA
8254(defun tramp-get-remote-tmpdir (vec)
8255 (with-connection-property vec "tmp-directory"
8256 (let ((dir (tramp-shell-quote-argument "/tmp")))
8257 (if (and (zerop
8258 (tramp-send-command-and-check
8259 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
8260 (zerop
8261 (tramp-send-command-and-check
8262 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
8263 dir
8264 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
8265
00d6fd04
MA
8266(defun tramp-get-ls-command (vec)
8267 (with-connection-property vec "ls"
946a5aeb
MA
8268 (tramp-message vec 5 "Finding a suitable `ls' command")
8269 (or
8270 (catch 'ls-found
8271 (dolist (cmd '("ls" "gnuls" "gls"))
8272 (let ((dl (tramp-get-remote-path vec))
8273 result)
8274 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
7e5686f0
MA
8275 ;; Check parameters. On busybox, "ls" output coloring is
8276 ;; enabled by default sometimes. So we try to disable it
8277 ;; when possible. $LS_COLORING is not supported there.
946a5aeb
MA
8278 (when (zerop (tramp-send-command-and-check
8279 vec (format "%s -lnd /" result)))
7e5686f0
MA
8280 (when (zerop (tramp-send-command-and-check
8281 vec (format "%s --color=never /" result)))
8282 (setq result (concat result " --color=never")))
946a5aeb
MA
8283 (throw 'ls-found result))
8284 (setq dl (cdr dl))))))
8285 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
00d6fd04 8286
8e754ea2
MA
8287(defun tramp-get-ls-command-with-dired (vec)
8288 (save-match-data
8289 (with-connection-property vec "ls-dired"
8290 (tramp-message vec 5 "Checking, whether `ls --dired' works")
8291 (zerop (tramp-send-command-and-check
7f49fe46 8292 vec (format "%s --dired /" (tramp-get-ls-command vec)))))))
8e754ea2 8293
00d6fd04
MA
8294(defun tramp-get-test-command (vec)
8295 (with-connection-property vec "test"
946a5aeb
MA
8296 (tramp-message vec 5 "Finding a suitable `test' command")
8297 (if (zerop (tramp-send-command-and-check vec "test 0"))
8298 "test"
8299 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
00d6fd04
MA
8300
8301(defun tramp-get-test-nt-command (vec)
8302 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8303 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8304 ;; for otherwise the shell crashes.
8305 (with-connection-property vec "test-nt"
8306 (or
8307 (progn
8308 (tramp-send-command
8309 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
8310 (with-current-buffer (tramp-get-buffer vec)
8311 (goto-char (point-min))
a0a5183a 8312 (when (looking-at (regexp-quote tramp-end-of-output))
00d6fd04
MA
8313 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
8314 (progn
8315 (tramp-send-command
8316 vec
8317 (format
8318 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8319 (tramp-get-test-command vec)))
8320 "tramp_test_nt %s %s"))))
8321
8322(defun tramp-get-file-exists-command (vec)
8323 (with-connection-property vec "file-exists"
946a5aeb
MA
8324 (tramp-message vec 5 "Finding command to check if file exists")
8325 (tramp-find-file-exists-command vec)))
00d6fd04
MA
8326
8327(defun tramp-get-remote-ln (vec)
8328 (with-connection-property vec "ln"
946a5aeb
MA
8329 (tramp-message vec 5 "Finding a suitable `ln' command")
8330 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
00d6fd04
MA
8331
8332(defun tramp-get-remote-perl (vec)
8333 (with-connection-property vec "perl"
946a5aeb 8334 (tramp-message vec 5 "Finding a suitable `perl' command")
293c24f9
MA
8335 (let ((result
8336 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
8337 (tramp-find-executable
8338 vec "perl" (tramp-get-remote-path vec)))))
8339 ;; We must check also for some Perl modules.
8340 (when result
8341 (with-connection-property vec "perl-file-spec"
8342 (zerop
8343 (tramp-send-command-and-check
8344 vec (format "%s -e 'use File::Spec;'" result))))
8345 (with-connection-property vec "perl-cwd-realpath"
8346 (zerop
8347 (tramp-send-command-and-check
8348 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
8349 result)))
00d6fd04
MA
8350
8351(defun tramp-get-remote-stat (vec)
8352 (with-connection-property vec "stat"
946a5aeb
MA
8353 (tramp-message vec 5 "Finding a suitable `stat' command")
8354 (let ((result (tramp-find-executable
8355 vec "stat" (tramp-get-remote-path vec)))
8356 tmp)
8357 ;; Check whether stat(1) returns usable syntax. %s does not
8358 ;; work on older AIX systems.
8359 (when result
8360 (setq tmp
8361 ;; We don't want to display an error message.
8362 (with-temp-message (or (current-message) "")
8363 (condition-case nil
8364 (tramp-send-command-and-read
8365 vec (format "%s -c '(\"%%N\" %%s)' /" result))
8366 (error nil))))
8367 (unless (and (listp tmp) (stringp (car tmp))
8368 (string-match "^./.$" (car tmp))
8369 (integerp (cadr tmp)))
8370 (setq result nil)))
8371 result)))
fb7933a3 8372
293c24f9
MA
8373(defun tramp-get-remote-readlink (vec)
8374 (with-connection-property vec "readlink"
8375 (tramp-message vec 5 "Finding a suitable `readlink' command")
8376 (let ((result (tramp-find-executable
8377 vec "readlink" (tramp-get-remote-path vec))))
8378 (when (and result
8379 ;; We don't want to display an error message.
8380 (with-temp-message (or (current-message) "")
8381 (condition-case nil
8382 (zerop
8383 (tramp-send-command-and-check
8384 vec (format "%s --canonicalize-missing /" result)))
8385 (error nil))))
8386 result))))
8387
00d6fd04
MA
8388(defun tramp-get-remote-id (vec)
8389 (with-connection-property vec "id"
946a5aeb
MA
8390 (tramp-message vec 5 "Finding POSIX `id' command")
8391 (or
8392 (catch 'id-found
8393 (let ((dl (tramp-get-remote-path vec))
8394 result)
8395 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8396 ;; Check POSIX parameter.
8397 (when (zerop (tramp-send-command-and-check
8398 vec (format "%s -u" result)))
8399 (throw 'id-found result))
8400 (setq dl (cdr dl)))))
8401 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
00d6fd04
MA
8402
8403(defun tramp-get-remote-uid (vec id-format)
8404 (with-connection-property vec (format "uid-%s" id-format)
8405 (let ((res (tramp-send-command-and-read
8406 vec
8407 (format "%s -u%s %s"
8408 (tramp-get-remote-id vec)
8409 (if (equal id-format 'integer) "" "n")
8410 (if (equal id-format 'integer)
8411 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8412 ;; The command might not always return a number.
8413 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8414
8415(defun tramp-get-remote-gid (vec id-format)
8416 (with-connection-property vec (format "gid-%s" id-format)
8417 (let ((res (tramp-send-command-and-read
8418 vec
8419 (format "%s -g%s %s"
8420 (tramp-get-remote-id vec)
8421 (if (equal id-format 'integer) "" "n")
8422 (if (equal id-format 'integer)
8423 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8424 ;; The command might not always return a number.
8425 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
fb7933a3 8426
8d60099b
MA
8427(defun tramp-get-local-uid (id-format)
8428 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8429
8430(defun tramp-get-local-gid (id-format)
9e6ab520 8431 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8d60099b 8432
00d6fd04 8433;; Some predefined connection properties.
6a29a838
MA
8434(defun tramp-get-inline-compress (vec prop size)
8435 "Return the compress command related to PROP.
8436PROP is either `inline-compress' or `inline-decompress'. SIZE is
8437the length of the file to be compressed.
8438
8439If no corresponding command is found, nil is returned."
8440 (when (and (integerp tramp-inline-compress-start-size)
8441 (> size tramp-inline-compress-start-size))
8442 (with-connection-property vec prop
8443 (tramp-find-inline-compress vec)
8444 (tramp-get-connection-property vec prop nil))))
8445
8446(defun tramp-get-inline-coding (vec prop size)
8447 "Return the coding command related to PROP.
8448PROP is either `remote-encoding', `remode-decoding',
8449`local-encoding' or `local-decoding'.
8450
8451SIZE is the length of the file to be coded. Depending on SIZE,
8452compression might be applied.
8453
8454If no corresponding command is found, nil is returned.
8455Otherwise, either a string is returned which contains a `%s' mark
8456to be used for the respective input or output file; or a Lisp
8457function cell is returned to be applied on a buffer."
8458 (let ((coding
8459 (with-connection-property vec prop
8460 (tramp-find-inline-encoding vec)
8461 (tramp-get-connection-property vec prop nil)))
8462 (prop1 (if (string-match "encoding" prop)
8463 "inline-compress" "inline-decompress"))
8464 compress)
00d6fd04 8465 ;; The connection property might have been cached. So we must send
6a29a838
MA
8466 ;; the script to the remote side - maybe.
8467 (when (and coding (symbolp coding) (string-match "remote" prop))
8468 (let ((name (symbol-name coding)))
00d6fd04
MA
8469 (while (string-match (regexp-quote "-") name)
8470 (setq name (replace-match "_" nil t name)))
6a29a838
MA
8471 (tramp-maybe-send-script vec (symbol-value coding) name)
8472 (setq coding name)))
8473 (when coding
8474 ;; Check for the `compress' command.
8475 (setq compress (tramp-get-inline-compress vec prop1 size))
8476 ;; Return the value.
8477 (cond
8478 ((and compress (symbolp coding))
8479 (if (string-match "decompress" prop1)
8480 `(lambda (beg end)
8481 (,coding beg end)
8482 (let ((coding-system-for-write 'binary)
8483 (coding-system-for-read 'binary))
8484 (apply
8485 'call-process-region (point-min) (point-max)
8486 (car (split-string ,compress)) t t nil
8487 (cdr (split-string ,compress)))))
8488 `(lambda (beg end)
8489 (let ((coding-system-for-write 'binary)
8490 (coding-system-for-read 'binary))
8491 (apply
8492 'call-process-region beg end
8493 (car (split-string ,compress)) t t nil
8494 (cdr (split-string ,compress))))
8495 (,coding (point-min) (point-max)))))
8496 ((symbolp coding)
8497 coding)
8498 ((and compress (string-match "decoding" prop))
8499 (format "(%s | %s >%%s)" coding compress))
8500 (compress
8501 (format "(%s <%%s | %s)" compress coding))
8502 ((string-match "decoding" prop)
8503 (format "%s >%%s" coding))
8504 (t
8505 (format "%s <%%s" coding))))))
fb7933a3 8506
00d6fd04 8507(defun tramp-get-method-parameter (method param)
c951aecb 8508 "Return the method parameter PARAM.
6a29a838 8509If the `tramp-methods' entry does not exist, return nil."
00d6fd04
MA
8510 (let ((entry (assoc param (assoc method tramp-methods))))
8511 (when entry (cadr entry))))
90f8dc03 8512
fb7933a3
KG
8513;; Auto saving to a special directory.
8514
00cec167 8515(defun tramp-exists-file-name-handler (operation &rest args)
a94d821f 8516 "Check, whether OPERATION runs a file name handler."
00d6fd04
MA
8517 ;; The file name handler is determined on base of either an
8518 ;; argument, `buffer-file-name', or `default-directory'.
8519 (condition-case nil
8520 (let* ((buffer-file-name "/")
8521 (default-directory "/")
8522 (fnha file-name-handler-alist)
8523 (check-file-name-operation operation)
8524 (file-name-handler-alist
8525 (list
8526 (cons "/"
aa485f7c
MA
8527 (lambda (operation &rest args)
8528 "Returns OPERATION if it is the one to be checked."
8529 (if (equal check-file-name-operation operation)
8530 operation
8531 (let ((file-name-handler-alist fnha))
8532 (apply operation args))))))))
00d6fd04
MA
8533 (equal (apply operation args) operation))
8534 (error nil)))
c1105d05
MA
8535
8536(unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8537 (defadvice make-auto-save-file-name
8538 (around tramp-advice-make-auto-save-file-name () activate)
00d6fd04 8539 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
b533bc97 8540 (if (tramp-tramp-file-p (buffer-file-name))
7f49fe46
MA
8541 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8542 ;; directly, because this would bypass the locking mechanism.
8543 (setq ad-return-value
8544 (tramp-file-name-handler 'make-auto-save-file-name))
a69c01a0 8545 ad-do-it))
191bb792
MA
8546 (add-hook
8547 'tramp-unload-hook
8548 (lambda ()
8549 (ad-remove-advice
8550 'make-auto-save-file-name
d7ec1df7
MA
8551 'around 'tramp-advice-make-auto-save-file-name)
8552 (ad-activate 'make-auto-save-file-name))))
fb7933a3 8553
b533bc97
MA
8554;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
8555;; umask. This is a security threat.
414da5ab
MA
8556
8557(defun tramp-set-auto-save-file-modes ()
8558 "Set permissions of autosaved remote files to the original permissions."
8559 (let ((bfn (buffer-file-name)))
b533bc97 8560 (when (and (tramp-tramp-file-p bfn)
b50dd0d2 8561 (buffer-modified-p)
414da5ab 8562 (stringp buffer-auto-save-file-name)
340b8d4f
MA
8563 (not (equal bfn buffer-auto-save-file-name)))
8564 (unless (file-exists-p buffer-auto-save-file-name)
8565 (write-region "" nil buffer-auto-save-file-name))
8566 ;; Permissions should be set always, because there might be an old
8567 ;; auto-saved file belonging to another original file. This could
8568 ;; be a security threat.
7177e2a3 8569 (set-file-modes buffer-auto-save-file-name
11948172 8570 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
414da5ab 8571
b533bc97
MA
8572(unless (and (featurep 'xemacs)
8573 (= emacs-major-version 21)
8574 (> emacs-minor-version 4))
a69c01a0
MA
8575 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
8576 (add-hook 'tramp-unload-hook
aa485f7c
MA
8577 (lambda ()
8578 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
414da5ab 8579
fb7933a3
KG
8580(defun tramp-subst-strs-in-string (alist string)
8581 "Replace all occurrences of the string FROM with TO in STRING.
8582ALIST is of the form ((FROM . TO) ...)."
8583 (save-match-data
8584 (while alist
8585 (let* ((pr (car alist))
8586 (from (car pr))
8587 (to (cdr pr)))
8588 (while (string-match (regexp-quote from) string)
8589 (setq string (replace-match to t t string)))
8590 (setq alist (cdr alist))))
8591 string))
8592
fb7933a3
KG
8593;; ------------------------------------------------------------
8594;; -- Compatibility functions section --
8595;; ------------------------------------------------------------
8596
00d6fd04 8597(defun tramp-read-passwd (proc &optional prompt)
fb7933a3 8598 "Read a password from user (compat function).
5615d63f 8599Consults the auth-source package.
5ec2cc41 8600Invokes `password-read' if available, `read-passwd' else."
00d6fd04
MA
8601 (let* ((key (tramp-make-tramp-file-name
8602 tramp-current-method tramp-current-user
8603 tramp-current-host ""))
8604 (pw-prompt
8605 (or prompt
8606 (with-current-buffer (process-buffer proc)
8607 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
8608 (format "%s for %s " (capitalize (match-string 1)) key)))))
7540f029
MA
8609 (with-parsed-tramp-file-name key nil
8610 (prog1
8611 (or
8612 ;; See if auth-sources contains something useful, if it's bound.
8613 (and (boundp 'auth-sources)
8614 (tramp-get-connection-property v "first-password-request" nil)
8615 ;; Try with Tramp's current method.
0d5852cf
MA
8616 (tramp-compat-funcall
8617 'auth-source-user-or-password
8618 "password" tramp-current-host tramp-current-method))
7540f029
MA
8619 ;; Try the password cache.
8620 (when (functionp 'password-read)
8621 (unless (tramp-get-connection-property
8622 v "first-password-request" nil)
0d5852cf 8623 (tramp-compat-funcall 'password-cache-remove key))
7540f029 8624 (let ((password
0d5852cf
MA
8625 (tramp-compat-funcall 'password-read pw-prompt key)))
8626 (tramp-compat-funcall 'password-cache-add key password)
7540f029
MA
8627 password))
8628 ;; Else, get the password interactively.
8629 (read-passwd pw-prompt))
8630 (tramp-set-connection-property v "first-password-request" nil)))))
00d6fd04 8631
9c13938d
MA
8632(defun tramp-clear-passwd (vec)
8633 "Clear password cache for connection related to VEC."
0d5852cf
MA
8634 (tramp-compat-funcall
8635 'password-cache-remove
8636 (tramp-make-tramp-file-name
8637 (tramp-file-name-method vec)
8638 (tramp-file-name-user vec)
8639 (tramp-file-name-host vec)
8640 "")))
00d6fd04
MA
8641
8642;; Snarfed code from time-date.el and parse-time.el
8643
8644(defconst tramp-half-a-year '(241 17024)
8645"Evaluated by \"(days-to-time 183)\".")
8646
8647(defconst tramp-parse-time-months
8648 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
8649 ("apr" . 4) ("may" . 5) ("jun" . 6)
8650 ("jul" . 7) ("aug" . 8) ("sep" . 9)
8651 ("oct" . 10) ("nov" . 11) ("dec" . 12))
8652 "Alist mapping month names to integers.")
8653
8654(defun tramp-time-less-p (t1 t2)
8655 "Say whether time value T1 is less than time value T2."
8656 (unless t1 (setq t1 '(0 0)))
8657 (unless t2 (setq t2 '(0 0)))
8658 (or (< (car t1) (car t2))
8659 (and (= (car t1) (car t2))
8660 (< (nth 1 t1) (nth 1 t2)))))
8661
8662(defun tramp-time-subtract (t1 t2)
8663 "Subtract two time values.
8664Return the difference in the format of a time value."
8665 (unless t1 (setq t1 '(0 0)))
8666 (unless t2 (setq t2 '(0 0)))
8667 (let ((borrow (< (cadr t1) (cadr t2))))
8668 (list (- (car t1) (car t2) (if borrow 1 0))
8669 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
fb7933a3
KG
8670
8671(defun tramp-time-diff (t1 t2)
8672 "Return the difference between the two times, in seconds.
1a762140 8673T1 and T2 are time values (as returned by `current-time' for example)."
fb7933a3 8674 ;; Pacify byte-compiler with `symbol-function'.
ea9d1443
KG
8675 (cond ((and (fboundp 'subtract-time)
8676 (fboundp 'float-time))
0d5852cf
MA
8677 (tramp-compat-funcall
8678 'float-time (tramp-compat-funcall 'subtract-time t1 t2)))
ea9d1443
KG
8679 ((and (fboundp 'subtract-time)
8680 (fboundp 'time-to-seconds))
0d5852cf
MA
8681 (tramp-compat-funcall
8682 'time-to-seconds (tramp-compat-funcall 'subtract-time t1 t2)))
fb7933a3 8683 ((fboundp 'itimer-time-difference)
0d5852cf
MA
8684 (tramp-compat-funcall
8685 'itimer-time-difference
8686 (if (< (length t1) 3) (append t1 '(0)) t1)
8687 (if (< (length t2) 3) (append t2 '(0)) t2)))
fb7933a3 8688 (t
00d6fd04 8689 (let ((time (tramp-time-subtract t1 t2)))
ea9d1443
KG
8690 (+ (* (car time) 65536.0)
8691 (cadr time)
8692 (/ (or (nth 2 time) 0) 1000000.0))))))
fb7933a3
KG
8693
8694(defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
8695 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8696EOL-TYPE can be one of `dos', `unix', or `mac'."
8697 (cond ((fboundp 'coding-system-change-eol-conversion)
0d5852cf
MA
8698 (tramp-compat-funcall
8699 'coding-system-change-eol-conversion coding-system eol-type))
fb7933a3 8700 ((fboundp 'subsidiary-coding-system)
0d5852cf
MA
8701 (tramp-compat-funcall
8702 'subsidiary-coding-system coding-system
8703 (cond ((eq eol-type 'dos) 'crlf)
8704 ((eq eol-type 'unix) 'lf)
8705 ((eq eol-type 'mac) 'cr)
8706 (t
8707 (error "Unknown EOL-TYPE `%s', must be %s"
8708 eol-type
8709 "`dos', `unix', or `mac'")))))
fb7933a3
KG
8710 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8711
19a87064
MA
8712(defun tramp-set-process-query-on-exit-flag (process flag)
8713 "Specify if query is needed for process when Emacs is exited.
8714If the second argument flag is non-nil, Emacs will query the user before
8715exiting if process is running."
8716 (if (fboundp 'set-process-query-on-exit-flag)
0d5852cf
MA
8717 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
8718 (tramp-compat-funcall 'process-kill-without-query) process flag))
19a87064 8719
19a87064 8720
bf247b6e
KS
8721;; ------------------------------------------------------------
8722;; -- Kludges section --
8723;; ------------------------------------------------------------
fb7933a3
KG
8724
8725;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8726;; does not deal well with newline characters. Newline is replaced by
8727;; backslash newline. But if, say, the string `a backslash newline b'
8728;; is passed to a shell, the shell will expand this into "ab",
8729;; completely omitting the newline. This is not what was intended.
8730;; It does not appear to be possible to make the function
8731;; `shell-quote-argument' work with newlines without making it
8732;; dependent on the shell used. But within this package, we know that
8733;; we will always use a Bourne-like shell, so we use an approach which
8734;; groks newlines.
8735;;
8736;; The approach is simple: we call `shell-quote-argument', then
8737;; massage the newline part of the result.
8738;;
8739;; This function should produce a string which is grokked by a Unix
8740;; shell, even if the Emacs is running on Windows. Since this is the
8741;; kludges section, we bind `system-type' in such a way that
8742;; `shell-quote-arguments' behaves as if on Unix.
8743;;
8744;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8745;; function to work with Bourne-like shells.
8746;;
8747;; CCC: This function should be rewritten so that
8748;; `shell-quote-argument' is not used. This way, we are safe from
8749;; changes in `shell-quote-argument'.
8750(defun tramp-shell-quote-argument (s)
8751 "Similar to `shell-quote-argument', but groks newlines.
8752Only works for Bourne-like shells."
8753 (let ((system-type 'not-windows))
8754 (save-match-data
8755 (let ((result (shell-quote-argument s))
8756 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
8757 (when (and (>= (length result) 2)
8758 (string= (substring result 0 2) "\\~"))
8759 (setq result (substring result 1)))
8760 (while (string-match nl result)
8761 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
8762 t t result)))
8763 result))))
8764
a69c01a0
MA
8765;; Checklist for `tramp-unload-hook'
8766;; - Unload all `tramp-*' packages
8767;; - Reset `file-name-handler-alist'
8768;; - Cleanup hooks where Tramp functions are in
8769;; - Cleanup advised functions
8770;; - Cleanup autoloads
8771;;;###autoload
8772(defun tramp-unload-tramp ()
08b1eb21 8773 "Discard Tramp from loading remote files."
a69c01a0
MA
8774 (interactive)
8775 ;; When Tramp is not loaded yet, its autoloads are still active.
8c04e197 8776 (tramp-unload-file-name-handlers)
a69c01a0 8777 ;; ange-ftp settings must be enabled.
0d5852cf 8778 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
00d6fd04
MA
8779 ;; Maybe its not loaded yet.
8780 (condition-case nil
8781 (unload-feature 'tramp 'force)
a69c01a0
MA
8782 (error nil)))
8783
dea31ca6
MA
8784(when (and load-in-progress
8785 (string-match "Loading tramp..." (or (current-message) "")))
ccb4a481
MA
8786 (message "Loading tramp...done"))
8787
fb7933a3
KG
8788(provide 'tramp)
8789
fb7933a3
KG
8790;;; TODO:
8791
4007ba5b 8792;; * Handle nonlocal exits such as C-g.
00d6fd04
MA
8793;; * But it would probably be better to use with-local-quit at the
8794;; place where it's actually needed: around any potentially
8795;; indefinitely blocking piece of code. In this case it would be
8796;; within Tramp around one of its calls to accept-process-output (or
8797;; around one of the loops that calls accept-process-output)
d037d501 8798;; (Stefan Monnier).
fb7933a3 8799;; * Rewrite `tramp-shell-quote-argument' to abstain from using
b1d06e75 8800;; `shell-quote-argument'.
fb7933a3
KG
8801;; * In Emacs 21, `insert-directory' shows total number of bytes used
8802;; by the files in that directory. Add this here.
8803;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8804;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
fb7933a3 8805;; * Case-insensitive filename completion. (Norbert Goevert.)
fb7933a3
KG
8806;; * Don't use globbing for directories with many files, as this is
8807;; likely to produce long command lines, and some shells choke on
8808;; long command lines.
fb7933a3 8809;; * How to deal with MULE in `insert-file-contents' and `write-region'?
fb7933a3
KG
8810;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8811;; * abbreviate-file-name
8e754ea2 8812;; * Better error checking. At least whenever we see something
fb7933a3
KG
8813;; strange when doing zerop, we should kill the process and start
8814;; again. (Greg Stark)
fb7933a3 8815;; * Remove unneeded parameters from methods.
fb7933a3
KG
8816;; * Make it work for different encodings, and for different file name
8817;; encodings, too. (Daniel Pittman)
fb7933a3
KG
8818;; * Don't search for perl5 and perl. Instead, only search for perl and
8819;; then look if it's the right version (with `perl -v').
8820;; * When editing a remote CVS controlled file as a different user, VC
8821;; gets confused about the file locking status. Try to find out why
8822;; the workaround doesn't work.
3cdaec13 8823;; * Username and hostname completion.
6c4e47fa 8824;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8daea7fc 8825;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
16674e4f 8826;; Code is nearly identical.
cfb5c0db
MA
8827;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8828;; until the last but one hop via `start-file-process'. Apply it
8829;; also for ftp and smb.
00d6fd04
MA
8830;; * WIBNI if we had a command "trampclient"? If I was editing in
8831;; some shell with root priviledges, it would be nice if I could
8832;; just call
8833;; trampclient filename.c
8834;; as an editor, and the _current_ shell would connect to an Emacs
8835;; server and would be used in an existing non-priviledged Emacs
8836;; session for doing the editing in question.
8837;; That way, I need not tell Emacs my password again and be afraid
8838;; that it makes it into core dumps or other ugly stuff (I had Emacs
8839;; once display a just typed password in the context of a keyboard
8840;; sequence prompt for a question immediately following in a shell
8841;; script run within Emacs -- nasty).
8842;; And if I have some ssh session running to a different computer,
8843;; having the possibility of passing a local file there to a local
8844;; Emacs session (in case I can arrange for a connection back) would
8845;; be nice.
a4aeb9a4 8846;; Likely the corresponding Tramp server should not allow the
00d6fd04
MA
8847;; equivalent of the emacsclient -eval option in order to make this
8848;; reasonably unproblematic. And maybe trampclient should have some
8849;; way of passing credentials, like by using an SSL socket or
6e4f5731 8850;; something. (David Kastrup)
00d6fd04 8851;; * Reconnect directly to a compliant shell without first going
6e4f5731 8852;; through the user's default shell. (Pete Forman)
00d6fd04 8853;; * Make `tramp-default-user' obsolete.
11c71217 8854;; * How can I interrupt the remote process with a signal
6e4f5731 8855;; (interrupt-process seems not to work)? (Markus Triska)
2296b54d
MA
8856;; * Avoid the local shell entirely for starting remote processes. If
8857;; so, I think even a signal, when delivered directly to the local
8858;; SSH instance, would correctly be propagated to the remote process
8859;; automatically; possibly SSH would have to be started with
6e4f5731 8860;; "-t". (Markus Triska)
dea31ca6 8861;; * It makes me wonder if tramp couldn't fall back to ssh when scp
6e4f5731
MA
8862;; isn't on the remote host. (Mark A. Hershberger)
8863;; * Use lsh instead of ssh. (Alfred M. Szmidt)
3e2fa353
MA
8864;; * Implement a general server-local-variable mechanism, as there are
8865;; probably other variables that need different values for different
8866;; servers too. The user could then configure a variable (such as
8867;; tramp-server-local-variable-alist) to define any such variables
8868;; that they need to, which would then be let bound as appropriate
6e4f5731 8869;; in tramp functions. (Jason Rumney)
946a5aeb
MA
8870;; * Optimize out-of-band copying, when both methods are scp-like (not
8871;; rsync).
8872;; * Keep a second connection open for out-of-band methods like scp or
8873;; rsync.
7540f029 8874;; * Support ptys in `tramp-handle-start-file-process'. (Bug#4604)
7e5686f0
MA
8875;; * IMHO, it's a drawback that currently Tramp doesn't support
8876;; Unicode in Dired file names by default. Is it possible to
8877;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8878;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
6e4f5731 8879;; expects only English messages? (Juri Linkov)
7e5686f0
MA
8880;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8881;; * Do not handle files with drive letter as remote. (Bug#5447)
b533bc97 8882;; * Load Tramp subpackages only when needed. (Bug#1529, Bug#5448, Bug#5705)
7e5686f0
MA
8883;; * Try telnet+curl as new method. It might be useful for busybox,
8884;; without built-in uuencode/uudecode.
7540f029
MA
8885;; * Let `shell-dynamic-complete-*' and `comint-dynamic-complete' work
8886;; on remote hosts.
8887;; * Use secrets.el for password handling.
5f2b693f 8888;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
fb7933a3
KG
8889
8890;; Functions for file-name-handler-alist:
8891;; diff-latest-backup-file -- in diff.el
fb7933a3 8892
cdd44874 8893;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
fb7933a3 8894;;; tramp.el ends here
57671b72
MA
8895
8896;; Local Variables:
8897;; mode: Emacs-Lisp
8898;; coding: utf-8
8899;; End: