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