Use lexical-bind in Tramp
[emacs.git] / lisp / net / tramp.el
bloba9254693adb725590c77a4b8fdc71e08cba7dfae
1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Maintainer: Michael Albinus <michael.albinus@gmx.de>
8 ;; Keywords: comm, processes
9 ;; Package: tramp
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package provides remote file editing, similar to ange-ftp.
29 ;; The difference is that ange-ftp uses FTP to transfer files between
30 ;; the local and the remote host, whereas tramp.el uses a combination
31 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
33 ;; For more detailed instructions, please see the info file.
35 ;; Notes:
36 ;; -----
38 ;; This package only works for Emacs 23.1 and higher.
40 ;; Also see the todo list at the bottom of this file.
42 ;; The current version of Tramp can be retrieved from the following URL:
43 ;; http://ftp.gnu.org/gnu/tramp/
45 ;; There's a mailing list for this, as well. Its name is:
46 ;; tramp-devel@gnu.org
47 ;; You can use the Web to subscribe, under the following URL:
48 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
50 ;; For the adventurous, the current development sources are available
51 ;; via Git. You can find instructions about this at the following URL:
52 ;; http://savannah.gnu.org/projects/tramp/
54 ;; Don't forget to put on your asbestos longjohns, first!
56 ;;; Code:
58 (require 'tramp-compat)
60 ;; Pacify byte-compiler.
61 (eval-when-compile
62 (require 'cl))
63 (defvar auto-save-file-name-transforms)
64 (defvar eshell-path-env)
65 (defvar ls-lisp-use-insert-directory-program)
66 (defvar outline-regexp)
68 ;;; User Customizable Internal Variables:
70 (defgroup tramp nil
71 "Edit remote files with a combination of ssh, scp, etc."
72 :group 'files
73 :group 'comm
74 :link '(custom-manual "(tramp)Top")
75 :version "22.1")
77 ;; Maybe we need once a real Tramp mode, with key bindings etc.
78 ;;;###autoload
79 (defcustom tramp-mode t
80 "Whether Tramp is enabled.
81 If it is set to nil, all remote file names are used literally."
82 :group 'tramp
83 :type 'boolean
84 :require 'tramp)
86 (defcustom tramp-verbose 3
87 "Verbosity level for Tramp messages.
88 Any level x includes messages for all levels 1 .. x-1. The levels are
90 0 silent (no tramp messages at all)
91 1 errors
92 2 warnings
93 3 connection to remote hosts (default level)
94 4 activities
95 5 internal
96 6 sent and received strings
97 7 file caching
98 8 connection properties
99 9 test commands
100 10 traces (huge)."
101 :group 'tramp
102 :type 'integer
103 :require 'tramp)
105 (defcustom tramp-backup-directory-alist nil
106 "Alist of filename patterns and backup directory names.
107 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
108 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
109 is a local file name, the backup directory is prepended with Tramp file
110 name prefix \(method, user, host) of file.
112 \(setq tramp-backup-directory-alist backup-directory-alist)
114 gives the same backup policy for Tramp files on their hosts like the
115 policy for local files."
116 :group 'tramp
117 :type '(repeat (cons (regexp :tag "Regexp matching filename")
118 (directory :tag "Backup directory name")))
119 :require 'tramp)
121 (defcustom tramp-auto-save-directory nil
122 "Put auto-save files in this directory, if set.
123 The idea is to use a local directory so that auto-saving is faster.
124 This setting has precedence over `auto-save-file-name-transforms'."
125 :group 'tramp
126 :type '(choice (const :tag "Use default" nil)
127 (directory :tag "Auto save directory name"))
128 :require 'tramp)
130 (defcustom tramp-encoding-shell
131 (or (tramp-compat-funcall 'w32-shell-name) "/bin/sh")
132 "Use this program for encoding and decoding commands on the local host.
133 This shell is used to execute the encoding and decoding command on the
134 local host, so if you want to use `~' in those commands, you should
135 choose a shell here which groks tilde expansion. `/bin/sh' normally
136 does not understand tilde expansion.
138 For encoding and decoding, commands like the following are executed:
140 /bin/sh -c COMMAND < INPUT > OUTPUT
142 This variable can be used to change the \"/bin/sh\" part. See the
143 variable `tramp-encoding-command-switch' for the \"-c\" part.
145 If the shell must be forced to be interactive, see
146 `tramp-encoding-command-interactive'.
148 Note that this variable is not used for remote commands. There are
149 mechanisms in tramp.el which automatically determine the right shell to
150 use for the remote host."
151 :group 'tramp
152 :type '(file :must-match t)
153 :require 'tramp)
155 (defcustom tramp-encoding-command-switch
156 (if (tramp-compat-funcall 'w32-shell-dos-semantics) "/c" "-c")
157 "Use this switch together with `tramp-encoding-shell' for local commands.
158 See the variable `tramp-encoding-shell' for more information."
159 :group 'tramp
160 :type 'string
161 :require 'tramp)
163 (defcustom tramp-encoding-command-interactive
164 (unless (tramp-compat-funcall 'w32-shell-dos-semantics) "-i")
165 "Use this switch together with `tramp-encoding-shell' for interactive shells.
166 See the variable `tramp-encoding-shell' for more information."
167 :version "24.1"
168 :group 'tramp
169 :type '(choice (const nil) string)
170 :require 'tramp)
172 ;;;###tramp-autoload
173 (defvar tramp-methods nil
174 "Alist of methods for remote files.
175 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
176 Each NAME stands for a remote access method. Each PARAM is a
177 pair of the form (KEY VALUE). The following KEYs are defined:
178 * `tramp-remote-shell'
179 This specifies the shell to use on the remote host. This
180 MUST be a Bourne-like shell. It is normally not necessary to
181 set this to any value other than \"/bin/sh\": Tramp wants to
182 use a shell which groks tilde expansion, but it can search
183 for it. Also note that \"/bin/sh\" exists on all Unixen,
184 this might not be true for the value that you decide to use.
185 You Have Been Warned.
186 * `tramp-remote-shell-login'
187 This specifies the arguments to let `tramp-remote-shell' run
188 as a login shell. It defaults to (\"-l\"), but some shells,
189 like ksh, require another argument. See
190 `tramp-connection-properties' for a way to overwrite the
191 default value.
192 * `tramp-remote-shell-args'
193 For implementation of `shell-command', this specifies the
194 arguments to let `tramp-remote-shell' run a single command.
195 * `tramp-login-program'
196 This specifies the name of the program to use for logging in to the
197 remote host. This may be the name of rsh or a workalike program,
198 or the name of telnet or a workalike, or the name of su or a workalike.
199 * `tramp-login-args'
200 This specifies the list of arguments to pass to the above
201 mentioned program. Please note that this is a list of list of arguments,
202 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
203 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
204 There are some patterns: \"%h\" in this list is replaced by the host
205 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
206 port number, and \"%%\" can be used to obtain a literal percent character.
207 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
208 expansion (i.e. no host or no user specified), this list is not used as
209 argument. By this, arguments like (\"-l\" \"%u\") are optional.
210 \"%t\" is replaced by the temporary file name produced with
211 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
212 parameter of a program, if exists. \"%c\" adds additional
213 `tramp-ssh-controlmaster-options' options for the first hop.
214 * `tramp-login-env'
215 A list of environment variables and their values, which will
216 be set when calling `tramp-login-program'.
217 * `tramp-async-args'
218 When an asynchronous process is started, we know already that
219 the connection works. Therefore, we can pass additional
220 parameters to suppress diagnostic messages, in order not to
221 tamper the process output.
222 * `tramp-copy-program'
223 This specifies the name of the program to use for remotely copying
224 the file; this might be the absolute filename of scp or the name of
225 a workalike program. It is always applied on the local host.
226 * `tramp-copy-args'
227 This specifies the list of parameters to pass to the above mentioned
228 program, the hints for `tramp-login-args' also apply here.
229 * `tramp-copy-env'
230 A list of environment variables and their values, which will
231 be set when calling `tramp-copy-program'.
232 * `tramp-remote-copy-program'
233 The listener program to be applied on remote side, if needed.
234 * `tramp-remote-copy-args'
235 The list of parameters to pass to the listener program, the hints
236 for `tramp-login-args' also apply here. Additionally, \"%r\" could
237 be used here and in `tramp-copy-args'. It denotes a randomly
238 chosen port for the remote listener.
239 * `tramp-copy-keep-date'
240 This specifies whether the copying program when the preserves the
241 timestamp of the original file.
242 * `tramp-copy-keep-tmpfile'
243 This specifies whether a temporary local file shall be kept
244 for optimization reasons (useful for \"rsync\" methods).
245 * `tramp-copy-recursive'
246 Whether the operation copies directories recursively.
247 * `tramp-default-port'
248 The default port of a method.
249 * `tramp-tmpdir'
250 A directory on the remote host for temporary files. If not
251 specified, \"/tmp\" is taken as default.
252 * `tramp-connection-timeout'
253 This is the maximum time to be spent for establishing a connection.
254 In general, the global default value shall be used, but for
255 some methods, like \"su\" or \"sudo\", a shorter timeout
256 might be desirable.
257 * `tramp-case-insensitive'
258 Whether the remote file system handles file names case insensitive.
259 Only a non-nil value counts, the default value nil means to
260 perform further checks on the remote host. See
261 `tramp-connection-properties' for a way to overwrite this.
263 What does all this mean? Well, you should specify `tramp-login-program'
264 for all methods; this program is used to log in to the remote site. Then,
265 there are two ways to actually transfer the files between the local and the
266 remote side. One way is using an additional scp-like program. If you want
267 to do this, set `tramp-copy-program' in the method.
269 Another possibility for file transfer is inline transfer, i.e. the
270 file is passed through the same buffer used by `tramp-login-program'. In
271 this case, the file contents need to be protected since the
272 `tramp-login-program' might use escape codes or the connection might not
273 be eight-bit clean. Therefore, file contents are encoded for transit.
274 See the variables `tramp-local-coding-commands' and
275 `tramp-remote-coding-commands' for details.
277 So, to summarize: if the method is an out-of-band method, then you
278 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
279 inline method, then these two parameters should be nil.
281 Notes:
283 When using `su' or `sudo' the phrase \"open connection to a remote
284 host\" sounds strange, but it is used nevertheless, for consistency.
285 No connection is opened to a remote host, but `su' or `sudo' is
286 started on the local host. You should specify a remote host
287 `localhost' or the name of the local host. Another host name is
288 useful only in combination with `tramp-default-proxies-alist'.")
290 (defcustom tramp-default-method
291 ;; An external copy method seems to be preferred, because it performs
292 ;; much better for large files, and it hasn't too serious delays
293 ;; for small files. But it must be ensured that there aren't
294 ;; permanent password queries. Either a password agent like
295 ;; "ssh-agent" or "Pageant" shall run, or the optional
296 ;; password-cache.el or auth-sources.el packages shall be active for
297 ;; password caching. If we detect that the user is running OpenSSH
298 ;; 4.0 or newer, we could reuse the connection, which calls also for
299 ;; an external method.
300 (cond
301 ;; PuTTY is installed. We don't take it, if it is installed on a
302 ;; non-windows system, or pscp from the pssh (parallel ssh) package
303 ;; is found.
304 ((and (eq system-type 'windows-nt) (executable-find "pscp")) "pscp")
305 ;; There is an ssh installation.
306 ((executable-find "scp") "scp")
307 ;; Fallback.
308 (t "ftp"))
309 "Default method to use for transferring files.
310 See `tramp-methods' for possibilities.
311 Also see `tramp-default-method-alist'."
312 :group 'tramp
313 :type 'string
314 :require 'tramp)
316 ;;;###tramp-autoload
317 (defcustom tramp-default-method-alist nil
318 "Default method to use for specific host/user pairs.
319 This is an alist of items (HOST USER METHOD). The first matching item
320 specifies the method to use for a file name which does not specify a
321 method. HOST and USER are regular expressions or nil, which is
322 interpreted as a regular expression which always matches. If no entry
323 matches, the variable `tramp-default-method' takes effect.
325 If the file name does not specify the user, lookup is done using the
326 empty string for the user name.
328 See `tramp-methods' for a list of possibilities for METHOD."
329 :group 'tramp
330 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
331 (choice :tag "User regexp" regexp sexp)
332 (choice :tag "Method name" string (const nil))))
333 :require 'tramp)
335 (defconst tramp-default-method-marker "-"
336 "Marker for default method in remote file names.")
338 (defcustom tramp-default-user nil
339 "Default user to use for transferring files.
340 It is nil by default; otherwise settings in configuration files like
341 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
343 This variable is regarded as obsolete, and will be removed soon."
344 :group 'tramp
345 :type '(choice (const nil) string)
346 :require 'tramp)
348 ;;;###tramp-autoload
349 (defcustom tramp-default-user-alist nil
350 "Default user to use for specific method/host pairs.
351 This is an alist of items (METHOD HOST USER). The first matching item
352 specifies the user to use for a file name which does not specify a
353 user. METHOD and USER are regular expressions or nil, which is
354 interpreted as a regular expression which always matches. If no entry
355 matches, the variable `tramp-default-user' takes effect.
357 If the file name does not specify the method, lookup is done using the
358 empty string for the method name."
359 :group 'tramp
360 :type '(repeat (list (choice :tag "Method regexp" regexp sexp)
361 (choice :tag " Host regexp" regexp sexp)
362 (choice :tag " User name" string (const nil))))
363 :require 'tramp)
365 (defcustom tramp-default-host (system-name)
366 "Default host to use for transferring files.
367 Useful for su and sudo methods mostly."
368 :group 'tramp
369 :type 'string
370 :require 'tramp)
372 ;;;###tramp-autoload
373 (defcustom tramp-default-host-alist nil
374 "Default host to use for specific method/user pairs.
375 This is an alist of items (METHOD USER HOST). The first matching item
376 specifies the host to use for a file name which does not specify a
377 host. METHOD and HOST are regular expressions or nil, which is
378 interpreted as a regular expression which always matches. If no entry
379 matches, the variable `tramp-default-host' takes effect.
381 If the file name does not specify the method, lookup is done using the
382 empty string for the method name."
383 :group 'tramp
384 :version "24.4"
385 :type '(repeat (list (choice :tag "Method regexp" regexp sexp)
386 (choice :tag " User regexp" regexp sexp)
387 (choice :tag " Host name" string (const nil))))
388 :require 'tramp)
390 (defcustom tramp-default-proxies-alist nil
391 "Route to be followed for specific host/user pairs.
392 This is an alist of items (HOST USER PROXY). The first matching
393 item specifies the proxy to be passed for a file name located on
394 a remote target matching USER@HOST. HOST and USER are regular
395 expressions. PROXY must be a Tramp filename without a localname
396 part. Method and user name on PROXY are optional, which is
397 interpreted with the default values. PROXY can contain the
398 patterns %h and %u, which are replaced by the strings matching
399 HOST or USER, respectively.
401 HOST, USER or PROXY could also be Lisp forms, which will be
402 evaluated. The result must be a string or nil, which is
403 interpreted as a regular expression which always matches."
404 :group 'tramp
405 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
406 (choice :tag "User regexp" regexp sexp)
407 (choice :tag " Proxy name" string (const nil))))
408 :require 'tramp)
410 (defcustom tramp-save-ad-hoc-proxies nil
411 "Whether to save ad-hoc proxies persistently."
412 :group 'tramp
413 :version "24.3"
414 :type 'boolean
415 :require 'tramp)
417 (defcustom tramp-restricted-shell-hosts-alist
418 (when (memq system-type '(windows-nt))
419 (list (concat "\\`" (regexp-quote (system-name)) "\\'")))
420 "List of hosts, which run a restricted shell.
421 This is a list of regular expressions, which denote hosts running
422 a registered shell like \"rbash\". Those hosts can be used as
423 proxies only, see `tramp-default-proxies-alist'. If the local
424 host runs a registered shell, it shall be added to this list, too."
425 :version "24.3"
426 :group 'tramp
427 :type '(repeat (regexp :tag "Host regexp"))
428 :require 'tramp)
430 ;;;###tramp-autoload
431 (defconst tramp-local-host-regexp
432 (concat
433 "\\`"
434 (regexp-opt
435 (list "localhost" "localhost6" (system-name) "127.0.0.1" "::1") t)
436 "\\'")
437 "Host names which are regarded as local host.")
439 (defvar tramp-completion-function-alist nil
440 "Alist of methods for remote files.
441 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...).
442 Each NAME stands for a remote access method. Each PAIR is of the form
443 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
444 names from FILE for completion. The following predefined FUNCTIONs exists:
446 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
447 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
448 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
449 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
450 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
451 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
452 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
453 * `tramp-parse-etc-group' for \"/etc/group\" like files.
454 * `tramp-parse-netrc' for \"~/.netrc\" like files.
455 * `tramp-parse-putty' for PuTTY registered sessions.
457 FUNCTION can also be a user defined function. For more details see
458 the info pages.")
460 (defconst tramp-echo-mark-marker "_echo"
461 "String marker to surround echoed commands.")
463 (defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
464 "String length of `tramp-echo-mark-marker'.")
466 (defconst tramp-echo-mark
467 (concat tramp-echo-mark-marker
468 (make-string tramp-echo-mark-marker-length ?\b))
469 "String mark to be transmitted around shell commands.
470 Used to separate their echo from the output they produce. This
471 will only be used if we cannot disable remote echo via stty.
472 This string must have no effect on the remote shell except for
473 producing some echo which can later be detected by
474 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
475 followed by an equal number of backspaces to erase them will
476 usually suffice.")
478 (defconst tramp-echoed-echo-mark-regexp
479 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
480 tramp-echo-mark-marker tramp-echo-mark-marker-length)
481 "Regexp which matches `tramp-echo-mark' as it gets echoed by
482 the remote shell.")
484 (defcustom tramp-local-end-of-line
485 (if (memq system-type '(windows-nt)) "\r\n" "\n")
486 "String used for end of line in local processes."
487 :version "24.1"
488 :group 'tramp
489 :type 'string
490 :require 'tramp)
492 (defcustom tramp-rsh-end-of-line "\n"
493 "String used for end of line in rsh connections.
494 I don't think this ever needs to be changed, so please tell me about it
495 if you need to change this."
496 :group 'tramp
497 :type 'string
498 :require 'tramp)
500 (defcustom tramp-login-prompt-regexp
501 ".*\\(user\\|login\\)\\( .*\\)?: *"
502 "Regexp matching login-like prompts.
503 The regexp should match at end of buffer.
505 Sometimes the prompt is reported to look like \"login as:\"."
506 :group 'tramp
507 :type 'regexp
508 :require 'tramp)
510 (defcustom tramp-shell-prompt-pattern
511 ;; Allow a prompt to start right after a ^M since it indeed would be
512 ;; displayed at the beginning of the line (and Zsh uses it). This
513 ;; regexp works only for GNU Emacs.
514 ;; Allow also [] style prompts. They can appear only during
515 ;; connection initialization; Tramp redefines the prompt afterwards.
516 (concat "\\(?:^\\|\r\\)"
517 "[^]#$%>\n]*#?[]#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
518 "Regexp to match prompts from remote shell.
519 Normally, Tramp expects you to configure `shell-prompt-pattern'
520 correctly, but sometimes it happens that you are connecting to a
521 remote host which sends a different kind of shell prompt. Therefore,
522 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
523 and also things matched by this variable. The default value of this
524 variable is similar to the default value of `shell-prompt-pattern',
525 which should work well in many cases.
527 This regexp must match both `tramp-initial-end-of-output' and
528 `tramp-end-of-output'."
529 :group 'tramp
530 :type 'regexp
531 :require 'tramp)
533 (defcustom tramp-password-prompt-regexp
534 (format "^.*\\(%s\\).*:\^@? *"
535 ;; `password-word-equivalents' has been introduced with Emacs 24.4.
536 (if (boundp 'password-word-equivalents)
537 (regexp-opt (symbol-value 'password-word-equivalents))
538 "password\\|passphrase"))
539 "Regexp matching password-like prompts.
540 The regexp should match at end of buffer.
542 The `sudo' program appears to insert a `^@' character into the prompt."
543 :version "24.4"
544 :group 'tramp
545 :type 'regexp
546 :require 'tramp)
548 (defcustom tramp-wrong-passwd-regexp
549 (concat "^.*"
550 ;; These strings should be on the last line
551 (regexp-opt '("Permission denied"
552 "Login incorrect"
553 "Login Incorrect"
554 "Connection refused"
555 "Connection closed"
556 "Timeout, server not responding."
557 "Sorry, try again."
558 "Name or service not known"
559 "Host key verification failed."
560 "No supported authentication methods left to try!") t)
561 ".*"
562 "\\|"
563 "^.*\\("
564 ;; Here comes a list of regexes, separated by \\|
565 "Received signal [0-9]+"
566 "\\).*")
567 "Regexp matching a `login failed' message.
568 The regexp should match at end of buffer."
569 :group 'tramp
570 :type 'regexp
571 :require 'tramp)
573 (defcustom tramp-yesno-prompt-regexp
574 (concat
575 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
576 "\\s-*")
577 "Regular expression matching all yes/no queries which need to be confirmed.
578 The confirmation should be done with yes or no.
579 The regexp should match at end of buffer.
580 See also `tramp-yn-prompt-regexp'."
581 :group 'tramp
582 :type 'regexp
583 :require 'tramp)
585 (defcustom tramp-yn-prompt-regexp
586 (concat
587 (regexp-opt '("Store key in cache? (y/n)"
588 "Update cached key? (y/n, Return cancels connection)")
590 "\\s-*")
591 "Regular expression matching all y/n queries which need to be confirmed.
592 The confirmation should be done with y or n.
593 The regexp should match at end of buffer.
594 See also `tramp-yesno-prompt-regexp'."
595 :group 'tramp
596 :type 'regexp
597 :require 'tramp)
599 (defcustom tramp-terminal-prompt-regexp
600 (concat "\\("
601 "TERM = (.*)"
602 "\\|"
603 "Terminal type\\? \\[.*\\]"
604 "\\)\\s-*")
605 "Regular expression matching all terminal setting prompts.
606 The regexp should match at end of buffer.
607 The answer will be provided by `tramp-action-terminal', which see."
608 :group 'tramp
609 :type 'regexp
610 :require 'tramp)
612 (defcustom tramp-operation-not-permitted-regexp
613 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
614 (regexp-opt '("Operation not permitted") t))
615 "Regular expression matching keep-date problems in (s)cp operations.
616 Copying has been performed successfully already, so this message can
617 be ignored safely."
618 :group 'tramp
619 :type 'regexp
620 :require 'tramp)
622 (defcustom tramp-copy-failed-regexp
623 (concat "\\(.+: "
624 (regexp-opt '("Permission denied"
625 "not a regular file"
626 "is a directory"
627 "No such file or directory")
629 "\\)\\s-*")
630 "Regular expression matching copy problems in (s)cp operations."
631 :group 'tramp
632 :type 'regexp
633 :require 'tramp)
635 (defcustom tramp-process-alive-regexp
637 "Regular expression indicating a process has finished.
638 In fact this expression is empty by intention, it will be used only to
639 check regularly the status of the associated process.
640 The answer will be provided by `tramp-action-process-alive',
641 `tramp-action-out-of-band', which see."
642 :group 'tramp
643 :type 'regexp
644 :require 'tramp)
646 (defconst tramp-temp-name-prefix "tramp."
647 "Prefix to use for temporary files.
648 If this is a relative file name (such as \"tramp.\"), it is considered
649 relative to the directory name returned by the function
650 `tramp-compat-temporary-file-directory' (which see). It may also be an
651 absolute file name; don't forget to include a prefix for the filename
652 part, though.")
654 (defconst tramp-temp-buffer-name " *tramp temp*"
655 "Buffer name for a temporary buffer.
656 It shall be used in combination with `generate-new-buffer-name'.")
658 (defvar tramp-temp-buffer-file-name nil
659 "File name of a persistent local temporary file.
660 Useful for \"rsync\" like methods.")
661 (make-variable-buffer-local 'tramp-temp-buffer-file-name)
662 (put 'tramp-temp-buffer-file-name 'permanent-local t)
664 ;;;###autoload
665 (defcustom tramp-syntax 'ftp
666 "Tramp filename syntax to be used.
668 It can have the following values:
670 `ftp' -- Ange-FTP like syntax
671 `sep' -- Syntax as defined for XEmacs originally."
672 :group 'tramp
673 :version "24.4"
674 :type '(choice (const :tag "Ange-FTP" ftp)
675 (const :tag "XEmacs" sep))
676 :require 'tramp)
678 (defconst tramp-prefix-format
679 (cond ((eq tramp-syntax 'ftp) "/")
680 ((eq tramp-syntax 'sep) "/[")
681 (t (error "Wrong `tramp-syntax' defined")))
682 "String matching the very beginning of Tramp file names.
683 Used in `tramp-make-tramp-file-name'.")
685 (defconst tramp-prefix-regexp
686 (concat "^" (regexp-quote tramp-prefix-format))
687 "Regexp matching the very beginning of Tramp file names.
688 Should always start with \"^\". Derived from `tramp-prefix-format'.")
690 (defconst tramp-method-regexp
691 "[a-zA-Z0-9-]+"
692 "Regexp matching methods identifiers.")
694 (defconst tramp-postfix-method-format
695 (cond ((eq tramp-syntax 'ftp) ":")
696 ((eq tramp-syntax 'sep) "/")
697 (t (error "Wrong `tramp-syntax' defined")))
698 "String matching delimiter between method and user or host names.
699 Used in `tramp-make-tramp-file-name'.")
701 (defconst tramp-postfix-method-regexp
702 (regexp-quote tramp-postfix-method-format)
703 "Regexp matching delimiter between method and user or host names.
704 Derived from `tramp-postfix-method-format'.")
706 (defconst tramp-user-regexp "[^/|: \t]+"
707 "Regexp matching user names.")
709 ;;;###tramp-autoload
710 (defconst tramp-prefix-domain-format "%"
711 "String matching delimiter between user and domain names.")
713 ;;;###tramp-autoload
714 (defconst tramp-prefix-domain-regexp
715 (regexp-quote tramp-prefix-domain-format)
716 "Regexp matching delimiter between user and domain names.
717 Derived from `tramp-prefix-domain-format'.")
719 (defconst tramp-domain-regexp "[a-zA-Z0-9_.-]+"
720 "Regexp matching domain names.")
722 (defconst tramp-user-with-domain-regexp
723 (concat "\\(" tramp-user-regexp "\\)"
724 tramp-prefix-domain-regexp
725 "\\(" tramp-domain-regexp "\\)")
726 "Regexp matching user names with domain names.")
728 (defconst tramp-postfix-user-format "@"
729 "String matching delimiter between user and host names.
730 Used in `tramp-make-tramp-file-name'.")
732 (defconst tramp-postfix-user-regexp
733 (regexp-quote tramp-postfix-user-format)
734 "Regexp matching delimiter between user and host names.
735 Derived from `tramp-postfix-user-format'.")
737 (defconst tramp-host-regexp "[a-zA-Z0-9_.-]+"
738 "Regexp matching host names.")
740 (defconst tramp-prefix-ipv6-format
741 (cond ((eq tramp-syntax 'ftp) "[")
742 ((eq tramp-syntax 'sep) "")
743 (t (error "Wrong `tramp-syntax' defined")))
744 "String matching left hand side of IPv6 addresses.
745 Used in `tramp-make-tramp-file-name'.")
747 (defconst tramp-prefix-ipv6-regexp
748 (regexp-quote tramp-prefix-ipv6-format)
749 "Regexp matching left hand side of IPv6 addresses.
750 Derived from `tramp-prefix-ipv6-format'.")
752 ;; The following regexp is a bit sloppy. But it shall serve our
753 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
754 ;; "::ffff:192.168.0.1".
755 (defconst tramp-ipv6-regexp
756 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
757 "Regexp matching IPv6 addresses.")
759 (defconst tramp-postfix-ipv6-format
760 (cond ((eq tramp-syntax 'ftp) "]")
761 ((eq tramp-syntax 'sep) "")
762 (t (error "Wrong `tramp-syntax' defined")))
763 "String matching right hand side of IPv6 addresses.
764 Used in `tramp-make-tramp-file-name'.")
766 (defconst tramp-postfix-ipv6-regexp
767 (regexp-quote tramp-postfix-ipv6-format)
768 "Regexp matching right hand side of IPv6 addresses.
769 Derived from `tramp-postfix-ipv6-format'.")
771 (defconst tramp-prefix-port-format
772 (cond ((eq tramp-syntax 'ftp) "#")
773 ((eq tramp-syntax 'sep) "#")
774 (t (error "Wrong `tramp-syntax' defined")))
775 "String matching delimiter between host names and port numbers.")
777 (defconst tramp-prefix-port-regexp
778 (regexp-quote tramp-prefix-port-format)
779 "Regexp matching delimiter between host names and port numbers.
780 Derived from `tramp-prefix-port-format'.")
782 (defconst tramp-port-regexp "[0-9]+"
783 "Regexp matching port numbers.")
785 (defconst tramp-host-with-port-regexp
786 (concat "\\(" tramp-host-regexp "\\)"
787 tramp-prefix-port-regexp
788 "\\(" tramp-port-regexp "\\)")
789 "Regexp matching host names with port numbers.")
791 (defconst tramp-postfix-hop-format "|"
792 "String matching delimiter after ad-hoc hop definitions.")
794 (defconst tramp-postfix-hop-regexp
795 (regexp-quote tramp-postfix-hop-format)
796 "Regexp matching delimiter after ad-hoc hop definitions.
797 Derived from `tramp-postfix-hop-format'.")
799 (defconst tramp-postfix-host-format
800 (cond ((eq tramp-syntax 'ftp) ":")
801 ((eq tramp-syntax 'sep) "]")
802 (t (error "Wrong `tramp-syntax' defined")))
803 "String matching delimiter between host names and localnames.
804 Used in `tramp-make-tramp-file-name'.")
806 (defconst tramp-postfix-host-regexp
807 (regexp-quote tramp-postfix-host-format)
808 "Regexp matching delimiter between host names and localnames.
809 Derived from `tramp-postfix-host-format'.")
811 (defconst tramp-localname-regexp ".*$"
812 "Regexp matching localnames.")
814 (defconst tramp-unknown-id-string "UNKNOWN"
815 "String used to denote an unknown user or group")
817 (defconst tramp-unknown-id-integer -1
818 "Integer used to denote an unknown user or group")
820 ;;; File name format:
822 (defconst tramp-remote-file-name-spec-regexp
823 (concat
824 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
825 "\\(?:" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
826 "\\(" "\\(?:" tramp-host-regexp "\\|"
827 tramp-prefix-ipv6-regexp "\\(?:" tramp-ipv6-regexp "\\)?"
828 tramp-postfix-ipv6-regexp "\\)"
829 "\\(?:" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?")
830 "Regular expression matching a Tramp file name between prefix and postfix.")
832 (defconst tramp-file-name-structure
833 (list
834 (concat
835 tramp-prefix-regexp
836 "\\(" "\\(?:" tramp-remote-file-name-spec-regexp
837 tramp-postfix-hop-regexp "\\)+" "\\)?"
838 tramp-remote-file-name-spec-regexp tramp-postfix-host-regexp
839 "\\(" tramp-localname-regexp "\\)")
840 5 6 7 8 1)
841 "List of six elements (REGEXP METHOD USER HOST FILE HOP), detailing \
842 the Tramp file name structure.
844 The first element REGEXP is a regular expression matching a Tramp file
845 name. The regex should contain parentheses around the method name,
846 the user name, the host name, and the file name parts.
848 The second element METHOD is a number, saying which pair of
849 parentheses matches the method name. The third element USER is
850 similar, but for the user name. The fourth element HOST is similar,
851 but for the host name. The fifth element FILE is for the file name.
852 The last element HOP is the ad-hoc hop definition, which could be a
853 cascade of several hops.
855 These numbers are passed directly to `match-string', which see. That
856 means the opening parentheses are counted to identify the pair.
858 See also `tramp-file-name-regexp'.")
860 ;;;###autoload
861 (defconst tramp-file-name-regexp-unified "\\`/.+:.*:"
862 "Value for `tramp-file-name-regexp' for unified remoting.
863 See `tramp-file-name-structure' for more explanations.
865 On W32 systems, the volume letter must be ignored.")
867 ;;;###autoload
868 (defconst tramp-file-name-regexp-separate "\\`/\\[.*\\]"
869 "Value for `tramp-file-name-regexp' for separate remoting.
870 See `tramp-file-name-structure' for more explanations.")
872 ;;;###autoload
873 (defvar tramp-file-name-regexp
874 (cond ((eq tramp-syntax 'ftp) tramp-file-name-regexp-unified)
875 ((eq tramp-syntax 'sep) tramp-file-name-regexp-separate)
876 (t (error "Wrong `tramp-syntax' defined")))
877 "Regular expression matching file names handled by Tramp.
878 This regexp should match Tramp file names but no other file
879 names. When calling `tramp-register-file-name-handlers', the
880 initial value is overwritten by the car of `tramp-file-name-structure'.")
882 ;;;###autoload
883 (defconst tramp-completion-file-name-regexp-unified
884 (concat
885 "\\`/\\("
886 ;; Optional multi hop.
887 "\\([^/|:]+:[^/|:]*|\\)*"
888 ;; Last hop.
889 (if (memq system-type '(cygwin windows-nt))
890 ;; The method is either "-", or at least two characters.
891 "\\(-\\|[^/|:]\\{2,\\}\\)"
892 ;; At least one character for method.
893 "[^/|:]+")
894 ;; Method separator, user name and host name.
895 "\\(:[^/|:]*\\)?"
896 "\\)?\\'")
897 "Value for `tramp-completion-file-name-regexp' for unified remoting.
898 See `tramp-file-name-structure' for more explanations.
900 On W32 systems, the volume letter must be ignored.")
902 ;;;###autoload
903 (defconst tramp-completion-file-name-regexp-separate
904 "\\`/\\[\\([^]]*\\)?\\'"
905 "Value for `tramp-completion-file-name-regexp' for separate remoting.
906 See `tramp-file-name-structure' for more explanations.")
908 ;;;###autoload
909 (defconst tramp-completion-file-name-regexp
910 (cond ((eq tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
911 ((eq tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
912 (t (error "Wrong `tramp-syntax' defined")))
913 "Regular expression matching file names handled by Tramp completion.
914 This regexp should match partial Tramp file names only.
916 Please note that the entry in `file-name-handler-alist' is made when
917 this file \(tramp.el) is loaded. This means that this variable must be set
918 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
919 updated after changing this variable.
921 Also see `tramp-file-name-structure'.")
923 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
924 ;; known to have a bug in `process-send-string'; some ssh connections appear
925 ;; to drop bytes when data is sent too quickly. There is also a connection
926 ;; buffer local variable, which is computed depending on remote host properties
927 ;; when `tramp-chunksize' is zero or nil.
928 (defcustom tramp-chunksize (when (memq system-type '(hpux)) 500)
929 ;; Parentheses in docstring starting at beginning of line are escaped.
930 ;; Fontification is messed up when
931 ;; `open-paren-in-column-0-is-defun-start' set to t.
932 "If non-nil, chunksize for sending input to local process.
933 It is necessary only on systems which have a buggy `process-send-string'
934 implementation. The necessity, whether this variable must be set, can be
935 checked via the following code:
937 (with-temp-buffer
938 (let* ((user \"xxx\") (host \"yyy\")
939 (init 0) (step 50)
940 (sent init) (received init))
941 (while (= sent received)
942 (setq sent (+ sent step))
943 (erase-buffer)
944 (let ((proc (start-process (buffer-name) (current-buffer)
945 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
946 (when (process-live-p proc)
947 (process-send-string proc (make-string sent ?\\ ))
948 (process-send-eof proc)
949 (process-send-eof proc))
950 (while (not (progn (goto-char (point-min))
951 (re-search-forward \"\\\\w+\" (point-max) t)))
952 (accept-process-output proc 1))
953 (when (process-live-p proc)
954 (setq received (string-to-number (match-string 0)))
955 (delete-process proc)
956 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
957 (sit-for 0))))
958 (if (> sent (+ init step))
959 (message \"You should set `tramp-chunksize' to a maximum of %s\"
960 (- sent step))
961 (message \"Test does not work\")
962 (display-buffer (current-buffer))
963 (sit-for 30))))
965 In the Emacs normally running Tramp, evaluate the above code
966 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
967 respectively). You can do this, for example, by pasting it into
968 the `*scratch*' buffer and then hitting C-j with the cursor after the
969 last closing parenthesis. Note that it works only if you have configured
970 \"ssh\" to run without password query, see ssh-agent(1).
972 You will see the number of bytes sent successfully to the remote host.
973 If that number exceeds 1000, you can stop the execution by hitting
974 C-g, because your Emacs is likely clean.
976 When it is necessary to set `tramp-chunksize', you might consider to
977 use an out-of-the-band method \(like \"scp\") instead of an internal one
978 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
979 performance.
981 If your Emacs is buggy, the code stops and gives you an indication
982 about the value `tramp-chunksize' should be set. Maybe you could just
983 experiment a bit, e.g. changing the values of `init' and `step'
984 in the third line of the code.
986 Please raise a bug report via \"M-x tramp-bug\" if your system needs
987 this variable to be set as well."
988 :group 'tramp
989 :type '(choice (const nil) integer)
990 :require 'tramp)
992 ;; Logging in to a remote host normally requires obtaining a pty. But
993 ;; Emacs on macOS has process-connection-type set to nil by default,
994 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
995 ;; for an override of the system default.
996 (defcustom tramp-process-connection-type t
997 "Overrides `process-connection-type' for connections from Tramp.
998 Tramp binds `process-connection-type' to the value given here before
999 opening a connection to a remote host."
1000 :group 'tramp
1001 :type '(choice (const nil) (const t) (const pty))
1002 :require 'tramp)
1004 (defcustom tramp-connection-timeout 60
1005 "Defines the max time to wait for establishing a connection (in seconds).
1006 This can be overwritten for different connection types in `tramp-methods'.
1008 The timeout does not include the time reading a password."
1009 :group 'tramp
1010 :version "24.4"
1011 :type 'integer
1012 :require 'tramp)
1014 (defcustom tramp-connection-min-time-diff 5
1015 "Defines seconds between two consecutive connection attempts.
1016 This is necessary as self defense mechanism, in order to avoid
1017 yo-yo connection attempts when the remote host is unavailable.
1019 A value of 0 or nil suppresses this check. This might be
1020 necessary, when several out-of-order copy operations are
1021 performed, or when several asynchronous processes will be started
1022 in a short time frame. In those cases it is recommended to
1023 let-bind this variable."
1024 :group 'tramp
1025 :version "24.4"
1026 :type '(choice (const nil) integer)
1027 :require 'tramp)
1029 (defcustom tramp-completion-reread-directory-timeout 10
1030 "Defines seconds since last remote command before rereading a directory.
1031 A remote directory might have changed its contents. In order to
1032 make it visible during file name completion in the minibuffer,
1033 Tramp flushes its cache and rereads the directory contents when
1034 more than `tramp-completion-reread-directory-timeout' seconds
1035 have been gone since last remote command execution. A value of t
1036 would require an immediate reread during filename completion, nil
1037 means to use always cached values for the directory contents."
1038 :group 'tramp
1039 :type '(choice (const nil) (const t) integer)
1040 :require 'tramp)
1042 ;;; Internal Variables:
1044 (defvar tramp-current-method nil
1045 "Connection method for this *tramp* buffer.")
1047 (defvar tramp-current-user nil
1048 "Remote login name for this *tramp* buffer.")
1050 (defvar tramp-current-host nil
1051 "Remote host for this *tramp* buffer.")
1053 (defvar tramp-current-connection nil
1054 "Last connection timestamp.")
1056 ;;;###autoload
1057 (defconst tramp-completion-file-name-handler-alist
1058 '(;(expand-file-name . tramp-completion-handle-expand-file-name)
1059 (file-name-all-completions
1060 . tramp-completion-handle-file-name-all-completions)
1061 (file-name-completion . tramp-completion-handle-file-name-completion))
1062 "Alist of completion handler functions.
1063 Used for file names matching `tramp-completion-file-name-regexp'.
1064 Operations not mentioned here will be handled by Tramp's file
1065 name handler functions, or the normal Emacs functions.")
1067 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1068 ;;;###tramp-autoload
1069 (defvar tramp-foreign-file-name-handler-alist nil
1070 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1071 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1072 calling HANDLER.")
1074 ;;; Internal functions which must come first:
1076 ;; Conversion functions between external representation and
1077 ;; internal data structure. Convenience functions for internal
1078 ;; data structure.
1080 (defun tramp-get-method-parameter (vec param)
1081 "Return the method parameter PARAM.
1082 If VEC is a vector, check first in connection properties.
1083 Afterwards, check in `tramp-methods'. If the `tramp-methods'
1084 entry does not exist, return nil."
1085 (let ((hash-entry
1086 (replace-regexp-in-string "^tramp-" "" (symbol-name param))))
1087 (if (tramp-connection-property-p vec hash-entry)
1088 ;; We use the cached property.
1089 (tramp-get-connection-property vec hash-entry nil)
1090 ;; Use the static value from `tramp-methods'.
1091 (let ((methods-entry
1092 (assoc param (assoc (tramp-file-name-method vec) tramp-methods))))
1093 (when methods-entry (cadr methods-entry))))))
1095 (defun tramp-file-name-p (vec)
1096 "Check, whether VEC is a Tramp object."
1097 (and (vectorp vec) (= 5 (length vec))))
1099 (defun tramp-file-name-method (vec)
1100 "Return method component of VEC."
1101 (and (tramp-file-name-p vec) (aref vec 0)))
1103 (defun tramp-file-name-user (vec)
1104 "Return user component of VEC."
1105 (and (tramp-file-name-p vec) (aref vec 1)))
1107 (defun tramp-file-name-host (vec)
1108 "Return host component of VEC."
1109 (and (tramp-file-name-p vec) (aref vec 2)))
1111 (defun tramp-file-name-localname (vec)
1112 "Return localname component of VEC."
1113 (and (tramp-file-name-p vec) (aref vec 3)))
1115 (defun tramp-file-name-hop (vec)
1116 "Return hop component of VEC."
1117 (and (tramp-file-name-p vec) (aref vec 4)))
1119 ;; The user part of a Tramp file name vector can be of kind
1120 ;; "user%domain". Sometimes, we must extract these parts.
1121 (defun tramp-file-name-real-user (vec)
1122 "Return the user name of VEC without domain."
1123 (save-match-data
1124 (let ((user (tramp-file-name-user vec)))
1125 (if (and (stringp user)
1126 (string-match tramp-user-with-domain-regexp user))
1127 (match-string 1 user)
1128 user))))
1130 (defun tramp-file-name-domain (vec)
1131 "Return the domain name of VEC."
1132 (save-match-data
1133 (let ((user (tramp-file-name-user vec)))
1134 (and (stringp user)
1135 (string-match tramp-user-with-domain-regexp user)
1136 (match-string 2 user)))))
1138 ;; The host part of a Tramp file name vector can be of kind
1139 ;; "host#port". Sometimes, we must extract these parts.
1140 (defun tramp-file-name-real-host (vec)
1141 "Return the host name of VEC without port."
1142 (save-match-data
1143 (let ((host (tramp-file-name-host vec)))
1144 (if (and (stringp host)
1145 (string-match tramp-host-with-port-regexp host))
1146 (match-string 1 host)
1147 host))))
1149 (defun tramp-file-name-port (vec)
1150 "Return the port number of VEC."
1151 (save-match-data
1152 (let ((host (tramp-file-name-host vec)))
1153 (or (and (stringp host)
1154 (string-match tramp-host-with-port-regexp host)
1155 (string-to-number (match-string 2 host)))
1156 (tramp-get-method-parameter vec 'tramp-default-port)))))
1158 ;; The localname can be quoted with "/:". Extract this.
1159 (defun tramp-file-name-unquote-localname (vec)
1160 "Return unquoted localname component of VEC."
1161 (tramp-compat-file-name-unquote (tramp-file-name-localname vec)))
1163 ;;;###tramp-autoload
1164 (defun tramp-tramp-file-p (name)
1165 "Return t if NAME is a string with Tramp file name syntax."
1166 (save-match-data
1167 (and (stringp name)
1168 ;; No "/:" and "/c:". This is not covered by `tramp-file-name-regexp'.
1169 (not (string-match
1170 (if (memq system-type '(cygwin windows-nt))
1171 "^/[[:alpha:]]?:" "^/:")
1172 name))
1173 (string-match tramp-file-name-regexp name))))
1175 (defun tramp-find-method (method user host)
1176 "Return the right method string to use.
1177 This is METHOD, if non-nil. Otherwise, do a lookup in
1178 `tramp-default-method-alist'."
1179 (when (and method (string-equal method tramp-default-method-marker))
1180 (setq method nil))
1181 (let ((result
1182 (or method
1183 (let ((choices tramp-default-method-alist)
1184 lmethod item)
1185 (while choices
1186 (setq item (pop choices))
1187 (when (and (string-match (or (nth 0 item) "") (or host ""))
1188 (string-match (or (nth 1 item) "") (or user "")))
1189 (setq lmethod (nth 2 item))
1190 (setq choices nil)))
1191 lmethod)
1192 tramp-default-method)))
1193 ;; We must mark, whether a default value has been used.
1194 (if (or method (null result))
1195 result
1196 (propertize result 'tramp-default t))))
1198 (defun tramp-find-user (method user host)
1199 "Return the right user string to use.
1200 This is USER, if non-nil. Otherwise, do a lookup in
1201 `tramp-default-user-alist'."
1202 (let ((result
1203 (or user
1204 (let ((choices tramp-default-user-alist)
1205 luser item)
1206 (while choices
1207 (setq item (pop choices))
1208 (when (and (string-match (or (nth 0 item) "") (or method ""))
1209 (string-match (or (nth 1 item) "") (or host "")))
1210 (setq luser (nth 2 item))
1211 (setq choices nil)))
1212 luser)
1213 tramp-default-user)))
1214 ;; We must mark, whether a default value has been used.
1215 (if (or user (null result))
1216 result
1217 (propertize result 'tramp-default t))))
1219 (defun tramp-find-host (method user host)
1220 "Return the right host string to use.
1221 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
1222 (or (and (> (length host) 0) host)
1223 (let ((choices tramp-default-host-alist)
1224 lhost item)
1225 (while choices
1226 (setq item (pop choices))
1227 (when (and (string-match (or (nth 0 item) "") (or method ""))
1228 (string-match (or (nth 1 item) "") (or user "")))
1229 (setq lhost (nth 2 item))
1230 (setq choices nil)))
1231 lhost)
1232 tramp-default-host))
1234 (defun tramp-dissect-file-name (name &optional nodefault)
1235 "Return a `tramp-file-name' structure.
1236 The structure consists of remote method, remote user, remote host,
1237 localname (file name on remote host) and hop. If NODEFAULT is
1238 non-nil, the file name parts are not expanded to their default
1239 values."
1240 (save-match-data
1241 (unless (tramp-tramp-file-p name)
1242 (tramp-compat-user-error nil "Not a Tramp file name: \"%s\"" name))
1243 (if (not (string-match (nth 0 tramp-file-name-structure) name))
1244 (error "`tramp-file-name-structure' didn't match!")
1245 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
1246 (user (match-string (nth 2 tramp-file-name-structure) name))
1247 (host (match-string (nth 3 tramp-file-name-structure) name))
1248 (localname (match-string (nth 4 tramp-file-name-structure) name))
1249 (hop (match-string (nth 5 tramp-file-name-structure) name)))
1250 (when host
1251 (when (string-match tramp-prefix-ipv6-regexp host)
1252 (setq host (replace-match "" nil t host)))
1253 (when (string-match tramp-postfix-ipv6-regexp host)
1254 (setq host (replace-match "" nil t host))))
1255 (if nodefault
1256 (vector method user host localname hop)
1257 (vector
1258 (tramp-find-method method user host)
1259 (tramp-find-user method user host)
1260 (tramp-find-host method user host)
1261 localname hop))))))
1263 (defun tramp-buffer-name (vec)
1264 "A name for the connection buffer VEC."
1265 (let ((method (tramp-file-name-method vec))
1266 (user (tramp-file-name-user vec))
1267 (host (tramp-file-name-real-host vec)))
1268 (if (not (zerop (length user)))
1269 (format "*tramp/%s %s@%s*" method user host)
1270 (format "*tramp/%s %s*" method host))))
1272 (defun tramp-make-tramp-file-name (method user host localname &optional hop)
1273 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1274 When not nil, an optional HOP is prepended."
1275 (concat tramp-prefix-format hop
1276 (when (not (zerop (length method)))
1277 (concat method tramp-postfix-method-format))
1278 (when (not (zerop (length user)))
1279 (concat user tramp-postfix-user-format))
1280 (when host
1281 (if (string-match tramp-ipv6-regexp host)
1282 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
1283 host))
1284 tramp-postfix-host-format
1285 (when localname localname)))
1287 (defun tramp-completion-make-tramp-file-name (method user host localname)
1288 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1289 It must not be a complete Tramp file name, but as long as there are
1290 necessary only. This function will be used in file name completion."
1291 (concat tramp-prefix-format
1292 (when (not (zerop (length method)))
1293 (concat method tramp-postfix-method-format))
1294 (when (not (zerop (length user)))
1295 (concat user tramp-postfix-user-format))
1296 (when (not (zerop (length host)))
1297 (concat
1298 (if (string-match tramp-ipv6-regexp host)
1299 (concat
1300 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
1301 host)
1302 tramp-postfix-host-format))
1303 (when localname localname)))
1305 (defun tramp-get-buffer (vec)
1306 "Get the connection buffer to be used for VEC."
1307 (or (get-buffer (tramp-buffer-name vec))
1308 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
1309 ;; We use the existence of connection property "process-buffer"
1310 ;; as indication, whether a connection is active.
1311 (tramp-set-connection-property
1312 vec "process-buffer"
1313 (tramp-get-connection-property vec "process-buffer" nil))
1314 (setq buffer-undo-list t)
1315 (setq default-directory
1316 (tramp-make-tramp-file-name
1317 (tramp-file-name-method vec)
1318 (tramp-file-name-user vec)
1319 (tramp-file-name-host vec)
1320 "/"))
1321 (current-buffer))))
1323 (defun tramp-get-connection-buffer (vec)
1324 "Get the connection buffer to be used for VEC.
1325 In case a second asynchronous communication has been started, it is different
1326 from `tramp-get-buffer'."
1327 (or (tramp-get-connection-property vec "process-buffer" nil)
1328 (tramp-get-buffer vec)))
1330 (defun tramp-get-connection-name (vec)
1331 "Get the connection name to be used for VEC.
1332 In case a second asynchronous communication has been started, it is different
1333 from the default one."
1334 (or (tramp-get-connection-property vec "process-name" nil)
1335 (tramp-buffer-name vec)))
1337 (defun tramp-get-connection-process (vec)
1338 "Get the connection process to be used for VEC.
1339 In case a second asynchronous communication has been started, it is different
1340 from the default one."
1341 (get-process (tramp-get-connection-name vec)))
1343 (defun tramp-set-connection-local-variables (vec)
1344 "Set connection-local variables in the connection buffer used for VEC.
1345 If connection-local variables are not supported by this Emacs
1346 version, the function does nothing."
1347 (with-current-buffer (tramp-get-connection-buffer vec)
1348 ;; `hack-connection-local-variables-apply' exists since Emacs 26.1.
1349 (tramp-compat-funcall
1350 'hack-connection-local-variables-apply
1351 (append
1352 '(tramp)
1353 (when (tramp-file-name-method vec)
1354 `(:protocol ,(tramp-file-name-method vec)))
1355 (when (tramp-file-name-user vec)
1356 `(:user ,(tramp-file-name-user vec)))
1357 (when (tramp-file-name-host vec)
1358 `(:machine ,(tramp-file-name-host vec)))))))
1360 (defun tramp-debug-buffer-name (vec)
1361 "A name for the debug buffer for VEC."
1362 (let ((method (tramp-file-name-method vec))
1363 (user (tramp-file-name-user vec))
1364 (host (tramp-file-name-real-host vec)))
1365 (if (not (zerop (length user)))
1366 (format "*debug tramp/%s %s@%s*" method user host)
1367 (format "*debug tramp/%s %s*" method host))))
1369 (defconst tramp-debug-outline-regexp
1370 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1371 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1373 (defun tramp-debug-outline-level ()
1374 "Return the depth to which a statement is nested in the outline.
1375 Point must be at the beginning of a header line.
1377 The outline level is equal to the verbosity of the Tramp message."
1378 (1+ (string-to-number (match-string 1))))
1380 (defun tramp-get-debug-buffer (vec)
1381 "Get the debug buffer for VEC."
1382 (with-current-buffer
1383 (get-buffer-create (tramp-debug-buffer-name vec))
1384 (when (bobp)
1385 (setq buffer-undo-list t)
1386 ;; So it does not get loaded while `outline-regexp' is let-bound.
1387 (require 'outline)
1388 ;; Activate `outline-mode'. This runs `text-mode-hook' and
1389 ;; `outline-mode-hook'. We must prevent that local processes
1390 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
1391 ;; Furthermore, `outline-regexp' must have the correct value
1392 ;; already, because it is used by `font-lock-compile-keywords'.
1393 (let ((default-directory (tramp-compat-temporary-file-directory))
1394 (outline-regexp tramp-debug-outline-regexp))
1395 (outline-mode))
1396 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
1397 (set (make-local-variable 'outline-level) 'tramp-debug-outline-level))
1398 (current-buffer)))
1400 (defsubst tramp-debug-message (vec fmt-string &rest arguments)
1401 "Append message to debug buffer.
1402 Message is formatted with FMT-STRING as control string and the remaining
1403 ARGUMENTS to actually emit the message (if applicable)."
1404 (with-current-buffer (tramp-get-debug-buffer vec)
1405 (goto-char (point-max))
1406 ;; Headline.
1407 (when (bobp)
1408 (insert
1409 (format
1410 ";; Emacs: %s Tramp: %s -*- mode: outline; -*-"
1411 emacs-version tramp-version))
1412 (when (>= tramp-verbose 10)
1413 (insert
1414 (format
1415 "\n;; Location: %s Git: %s"
1416 (locate-library "tramp") (tramp-repository-get-version)))))
1417 (unless (bolp)
1418 (insert "\n"))
1419 ;; Timestamp.
1420 (let ((now (current-time)))
1421 (insert (format-time-string "%T." now))
1422 (insert (format "%06d " (nth 2 now))))
1423 ;; Calling Tramp function. We suppress compat and trace functions
1424 ;; from being displayed.
1425 (let ((btn 1) btf fn)
1426 (while (not fn)
1427 (setq btf (nth 1 (backtrace-frame btn)))
1428 (if (not btf)
1429 (setq fn "")
1430 (when (symbolp btf)
1431 (setq fn (symbol-name btf))
1432 (unless
1433 (and
1434 (string-match "^tramp" fn)
1435 (not
1436 (string-match
1437 (concat
1439 (regexp-opt
1440 '("tramp-backtrace"
1441 "tramp-compat-condition-case-unless-debug"
1442 "tramp-compat-funcall"
1443 "tramp-compat-user-error"
1444 "tramp-condition-case-unless-debug"
1445 "tramp-debug-message"
1446 "tramp-error"
1447 "tramp-error-with-buffer"
1448 "tramp-message")
1450 "$")
1451 fn)))
1452 (setq fn nil)))
1453 (setq btn (1+ btn))))
1454 ;; The following code inserts filename and line number. Should
1455 ;; be inactive by default, because it is time consuming.
1456 ; (let ((ffn (find-function-noselect (intern fn))))
1457 ; (insert
1458 ; (format
1459 ; "%s:%d: "
1460 ; (file-name-nondirectory (buffer-file-name (car ffn)))
1461 ; (with-current-buffer (car ffn)
1462 ; (1+ (count-lines (point-min) (cdr ffn)))))))
1463 (insert (format "%s " fn)))
1464 ;; The message.
1465 (insert (apply #'format-message fmt-string arguments))))
1467 (defvar tramp-message-show-message t
1468 "Show Tramp message in the minibuffer.
1469 This variable is used to disable messages from `tramp-error'.
1470 The messages are visible anyway, because an error is raised.")
1472 (defsubst tramp-message (vec-or-proc level fmt-string &rest arguments)
1473 "Emit a message depending on verbosity level.
1474 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
1475 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1476 less than LEVEL. The message is emitted only if `tramp-verbose' is
1477 greater than or equal to LEVEL.
1479 The message is also logged into the debug buffer when `tramp-verbose'
1480 is greater than or equal 4.
1482 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1483 control string and the remaining ARGUMENTS to actually emit the message (if
1484 applicable)."
1485 (ignore-errors
1486 (when (<= level tramp-verbose)
1487 ;; Match data must be preserved!
1488 (save-match-data
1489 ;; Display only when there is a minimum level.
1490 (when (and tramp-message-show-message (<= level 3))
1491 (apply 'message
1492 (concat
1493 (cond
1494 ((= level 0) "")
1495 ((= level 1) "")
1496 ((= level 2) "Warning: ")
1497 (t "Tramp: "))
1498 fmt-string)
1499 arguments))
1500 ;; Log only when there is a minimum level.
1501 (when (>= tramp-verbose 4)
1502 ;; Translate proc to vec.
1503 (when (processp vec-or-proc)
1504 (let ((tramp-verbose 0))
1505 (setq vec-or-proc
1506 (tramp-get-connection-property vec-or-proc "vector" nil))))
1507 ;; Append connection buffer for error messages.
1508 (when (= level 1)
1509 (let ((tramp-verbose 0))
1510 (with-current-buffer (tramp-get-connection-buffer vec-or-proc)
1511 (setq fmt-string (concat fmt-string "\n%s")
1512 arguments (append arguments (list (buffer-string)))))))
1513 ;; Do it.
1514 (when (vectorp vec-or-proc)
1515 (apply 'tramp-debug-message
1516 vec-or-proc
1517 (concat (format "(%d) # " level) fmt-string)
1518 arguments)))))))
1520 (defsubst tramp-backtrace (&optional vec-or-proc)
1521 "Dump a backtrace into the debug buffer.
1522 If VEC-OR-PROC is nil, the buffer *debug tramp* is used. This
1523 function is meant for debugging purposes."
1524 (if vec-or-proc
1525 (tramp-message vec-or-proc 10 "\n%s" (with-output-to-string (backtrace)))
1526 (if (>= tramp-verbose 10)
1527 (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
1529 (defsubst tramp-error (vec-or-proc signal fmt-string &rest arguments)
1530 "Emit an error.
1531 VEC-OR-PROC identifies the connection to use, SIGNAL is the
1532 signal identifier to be raised, remaining arguments passed to
1533 `tramp-message'. Finally, signal SIGNAL is raised."
1534 (let (tramp-message-show-message)
1535 (tramp-backtrace vec-or-proc)
1536 (when vec-or-proc
1537 (tramp-message
1538 vec-or-proc 1 "%s"
1539 (error-message-string
1540 (list signal
1541 (get signal 'error-message)
1542 (apply #'format-message fmt-string arguments)))))
1543 (signal signal (list (apply #'format-message fmt-string arguments)))))
1545 (defsubst tramp-error-with-buffer
1546 (buf vec-or-proc signal fmt-string &rest arguments)
1547 "Emit an error, and show BUF.
1548 If BUF is nil, show the connection buf. Wait for 30\", or until
1549 an input event arrives. The other arguments are passed to `tramp-error'."
1550 (save-window-excursion
1551 (let* ((buf (or (and (bufferp buf) buf)
1552 (and (processp vec-or-proc) (process-buffer vec-or-proc))
1553 (and (vectorp vec-or-proc)
1554 (tramp-get-connection-buffer vec-or-proc))))
1555 (vec (or (and (vectorp vec-or-proc) vec-or-proc)
1556 (and buf (with-current-buffer buf
1557 (tramp-dissect-file-name default-directory))))))
1558 (unwind-protect
1559 (apply 'tramp-error vec-or-proc signal fmt-string arguments)
1560 ;; Save exit.
1561 (when (and buf
1562 tramp-message-show-message
1563 (not (zerop tramp-verbose))
1564 ;; Do not show when flagged from outside.
1565 (not (tramp-completion-mode-p))
1566 ;; Show only when Emacs has started already.
1567 (current-message))
1568 (let ((enable-recursive-minibuffers t))
1569 ;; `tramp-error' does not show messages. So we must do it
1570 ;; ourselves.
1571 (apply 'message fmt-string arguments)
1572 ;; Show buffer.
1573 (pop-to-buffer buf)
1574 (discard-input)
1575 (sit-for 30)))
1576 ;; Reset timestamp. It would be wrong after waiting for a while.
1577 (when (equal (butlast (append vec nil) 2)
1578 (car tramp-current-connection))
1579 (setcdr tramp-current-connection (current-time)))))))
1581 (defmacro with-parsed-tramp-file-name (filename var &rest body)
1582 "Parse a Tramp filename and make components available in the body.
1584 First arg FILENAME is evaluated and dissected into its components.
1585 Second arg VAR is a symbol. It is used as a variable name to hold
1586 the filename structure. It is also used as a prefix for the variables
1587 holding the components. For example, if VAR is the symbol `foo', then
1588 `foo' will be bound to the whole structure, `foo-method' will be bound to
1589 the method component, and so on for `foo-user', `foo-host', `foo-localname',
1590 `foo-hop'.
1592 Remaining args are Lisp expressions to be evaluated (inside an implicit
1593 `progn').
1595 If VAR is nil, then we bind `v' to the structure and `method', `user',
1596 `host', `localname', `hop' to the components."
1597 (let ((bindings
1598 (mapcar (lambda (elem)
1599 `(,(if var (intern (format "%s-%s" var elem)) elem)
1600 (,(intern (format "tramp-file-name-%s" elem))
1601 ,(or var 'v))))
1602 '(method user host localname hop))))
1603 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
1604 ,@bindings)
1605 ;; We don't know which of those vars will be used, so we bind them all,
1606 ;; and then add here a dummy use of all those variables, so we don't get
1607 ;; flooded by warnings about those vars `body' didn't use.
1608 (ignore ,@(mapcar #'car bindings))
1609 ,@body)))
1611 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
1612 (put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
1613 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
1615 (defun tramp-progress-reporter-update (reporter &optional value)
1616 "Report progress of an operation for Tramp."
1617 (let* ((parameters (cdr reporter))
1618 (message (aref parameters 3)))
1619 (when (string-match message (or (current-message) ""))
1620 (progress-reporter-update reporter value))))
1622 (defmacro with-tramp-progress-reporter (vec level message &rest body)
1623 "Executes BODY, spinning a progress reporter with MESSAGE.
1624 If LEVEL does not fit for visible messages, there are only traces
1625 without a visible progress reporter."
1626 (declare (indent 3) (debug t))
1627 `(progn
1628 (tramp-message ,vec ,level "%s..." ,message)
1629 (let ((cookie "failed")
1631 ;; We start a pulsing progress reporter after 3 seconds. Feature
1632 ;; introduced in Emacs 24.1.
1633 (when (and tramp-message-show-message
1634 ;; Display only when there is a minimum level.
1635 (<= ,level (min tramp-verbose 3)))
1636 (ignore-errors
1637 (let ((pr (make-progress-reporter ,message nil nil)))
1638 (when pr
1639 (run-at-time
1640 3 0.1 #'tramp-progress-reporter-update pr)))))))
1641 (unwind-protect
1642 ;; Execute the body.
1643 (prog1 (progn ,@body) (setq cookie "done"))
1644 ;; Stop progress reporter.
1645 (if tm (cancel-timer tm))
1646 (tramp-message ,vec ,level "%s...%s" ,message cookie)))))
1648 (font-lock-add-keywords
1649 'emacs-lisp-mode '("\\<with-tramp-progress-reporter\\>"))
1651 (defmacro with-tramp-file-property (vec file property &rest body)
1652 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
1653 FILE must be a local file name on a connection identified via VEC."
1654 `(if (file-name-absolute-p ,file)
1655 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
1656 (when (eq value 'undef)
1657 ;; We cannot pass @body as parameter to
1658 ;; `tramp-set-file-property' because it mangles our
1659 ;; debug messages.
1660 (setq value (progn ,@body))
1661 (tramp-set-file-property ,vec ,file ,property value))
1662 value)
1663 ,@body))
1665 (put 'with-tramp-file-property 'lisp-indent-function 3)
1666 (put 'with-tramp-file-property 'edebug-form-spec t)
1667 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-tramp-file-property\\>"))
1669 (defmacro with-tramp-connection-property (key property &rest body)
1670 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
1671 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
1672 (when (eq value 'undef)
1673 ;; We cannot pass ,@body as parameter to
1674 ;; `tramp-set-connection-property' because it mangles our debug
1675 ;; messages.
1676 (setq value (progn ,@body))
1677 (tramp-set-connection-property ,key ,property value))
1678 value))
1680 (put 'with-tramp-connection-property 'lisp-indent-function 2)
1681 (put 'with-tramp-connection-property 'edebug-form-spec t)
1682 (font-lock-add-keywords
1683 'emacs-lisp-mode '("\\<with-tramp-connection-property\\>"))
1685 (defun tramp-drop-volume-letter (name)
1686 "Cut off unnecessary drive letter from file NAME.
1687 The functions `tramp-*-handle-expand-file-name' call `expand-file-name'
1688 locally on a remote file name. When the local system is a W32 system
1689 but the remote system is Unix, this introduces a superfluous drive
1690 letter into the file name. This function removes it."
1691 (save-match-data
1692 (funcall
1693 (if (tramp-compat-file-name-quoted-p name)
1694 'tramp-compat-file-name-quote 'identity)
1695 (let ((name (tramp-compat-file-name-unquote name)))
1696 (if (string-match "\\`[a-zA-Z]:/" name)
1697 (replace-match "/" nil t name)
1698 name)))))
1700 ;;; Config Manipulation Functions:
1702 ;;;###tramp-autoload
1703 (defun tramp-set-completion-function (method function-list)
1704 "Sets the list of completion functions for METHOD.
1705 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1706 The FUNCTION is intended to parse FILE according its syntax.
1707 It might be a predefined FUNCTION, or a user defined FUNCTION.
1708 For the list of predefined FUNCTIONs see `tramp-completion-function-alist'.
1710 Example:
1712 (tramp-set-completion-function
1713 \"ssh\"
1714 \\='((tramp-parse-sconfig \"/etc/ssh_config\")
1715 (tramp-parse-sconfig \"~/.ssh/config\")))"
1717 (let ((r function-list)
1718 (v function-list))
1719 (setq tramp-completion-function-alist
1720 (delete (assoc method tramp-completion-function-alist)
1721 tramp-completion-function-alist))
1723 (while v
1724 ;; Remove double entries.
1725 (when (member (car v) (cdr v))
1726 (setcdr v (delete (car v) (cdr v))))
1727 ;; Check for function and file or registry key.
1728 (unless (and (functionp (nth 0 (car v)))
1729 (cond
1730 ;; Windows registry.
1731 ((string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
1732 (and (memq system-type '(cygwin windows-nt))
1733 (zerop
1734 (tramp-call-process
1735 v "reg" nil nil nil "query" (nth 1 (car v))))))
1736 ;; Zeroconf service type.
1737 ((string-match
1738 "^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v))))
1739 ;; Configuration file.
1740 (t (file-exists-p (nth 1 (car v))))))
1741 (setq r (delete (car v) r)))
1742 (setq v (cdr v)))
1744 (when r
1745 (add-to-list 'tramp-completion-function-alist
1746 (cons method r)))))
1748 (defun tramp-get-completion-function (method)
1749 "Returns a list of completion functions for METHOD.
1750 For definition of that list see `tramp-set-completion-function'."
1751 (append
1752 `(;; Default settings are taken into account.
1753 (tramp-parse-default-user-host ,method)
1754 ;; Hosts visited once shall be remembered.
1755 (tramp-parse-connection-properties ,method))
1756 ;; The method related defaults.
1757 (cdr (assoc method tramp-completion-function-alist))))
1760 ;;; Fontification of `read-file-name':
1762 (defvar tramp-rfn-eshadow-overlay)
1763 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
1765 (defun tramp-rfn-eshadow-setup-minibuffer ()
1766 "Set up a minibuffer for `file-name-shadow-mode'.
1767 Adds another overlay hiding filename parts according to Tramp's
1768 special handling of `substitute-in-file-name'."
1769 (when (symbol-value 'minibuffer-completing-file-name)
1770 (setq tramp-rfn-eshadow-overlay
1771 (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
1772 ;; Copy rfn-eshadow-overlay properties.
1773 (let ((props (overlay-properties (symbol-value 'rfn-eshadow-overlay))))
1774 (while props
1775 ;; The `field' property prevents correct minibuffer
1776 ;; completion; we exclude it.
1777 (if (not (eq (car props) 'field))
1778 (overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))
1779 (pop props) (pop props))))))
1781 (add-hook 'rfn-eshadow-setup-minibuffer-hook
1782 'tramp-rfn-eshadow-setup-minibuffer)
1783 (add-hook 'tramp-unload-hook
1784 (lambda ()
1785 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
1786 'tramp-rfn-eshadow-setup-minibuffer)))
1788 (defconst tramp-rfn-eshadow-update-overlay-regexp
1789 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
1791 (defun tramp-rfn-eshadow-update-overlay ()
1792 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
1793 This is intended to be used as a minibuffer `post-command-hook' for
1794 `file-name-shadow-mode'; the minibuffer should have already
1795 been set up by `rfn-eshadow-setup-minibuffer'."
1796 ;; In remote files name, there is a shadowing just for the local part.
1797 (ignore-errors
1798 (let ((end (or (overlay-end (symbol-value 'rfn-eshadow-overlay))
1799 (minibuffer-prompt-end)))
1800 ;; We do not want to send any remote command.
1801 (non-essential t))
1802 (when
1803 (tramp-tramp-file-p
1804 (buffer-substring-no-properties end (point-max)))
1805 (save-excursion
1806 (save-restriction
1807 (narrow-to-region
1808 (1+ (or (string-match
1809 tramp-rfn-eshadow-update-overlay-regexp
1810 (buffer-string) end)
1811 end))
1812 (point-max))
1813 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
1814 (rfn-eshadow-update-overlay-hook nil)
1815 file-name-handler-alist)
1816 (move-overlay rfn-eshadow-overlay (point-max) (point-max))
1817 (rfn-eshadow-update-overlay))))))))
1819 (add-hook 'rfn-eshadow-update-overlay-hook
1820 'tramp-rfn-eshadow-update-overlay)
1821 (add-hook 'tramp-unload-hook
1822 (lambda ()
1823 (remove-hook 'rfn-eshadow-update-overlay-hook
1824 'tramp-rfn-eshadow-update-overlay)))
1826 ;; Inodes don't exist for some file systems. Therefore we must
1827 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
1828 ;; applied might be not so efficient (Ange-FTP uses hashes). But
1829 ;; performance isn't the major issue given that file transfer will
1830 ;; take time.
1831 (defvar tramp-inodes 0
1832 "Keeps virtual inodes numbers.")
1834 ;; Devices must distinguish physical file systems. The device numbers
1835 ;; provided by "lstat" aren't unique, because we operate on different hosts.
1836 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
1837 ;; EFS use device number "-1". In order to be different, we use device number
1838 ;; (-1 . x), whereby "x" is unique for a given (method user host).
1839 (defvar tramp-devices 0
1840 "Keeps virtual device numbers.")
1842 (defun tramp-default-file-modes (filename)
1843 "Return file modes of FILENAME as integer.
1844 If the file modes of FILENAME cannot be determined, return the
1845 value of `default-file-modes', without execute permissions."
1846 (or (file-modes filename)
1847 (logand (default-file-modes) (string-to-number "0666" 8))))
1849 (defun tramp-replace-environment-variables (filename)
1850 "Replace environment variables in FILENAME.
1851 Return the string with the replaced variables."
1852 (or (ignore-errors
1853 ;; Optional arg has been introduced with Emacs 24 (?).
1854 (tramp-compat-funcall 'substitute-env-vars filename 'only-defined))
1855 ;; We need an own implementation.
1856 (save-match-data
1857 (let ((idx (string-match "$\\(\\w+\\)" filename)))
1858 ;; `$' is coded as `$$'.
1859 (when (and idx
1860 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
1861 (getenv (match-string 1 filename)))
1862 (setq filename
1863 (replace-match
1864 (substitute-in-file-name (match-string 0 filename))
1865 t nil filename)))
1866 filename))))
1868 (defun tramp-find-file-name-coding-system-alist (filename tmpname)
1869 "Like `find-operation-coding-system' for Tramp filenames.
1870 Tramp's `insert-file-contents' and `write-region' work over
1871 temporary file names. If `file-coding-system-alist' contains an
1872 expression, which matches more than the file name suffix, the
1873 coding system might not be determined. This function repairs it."
1874 (let (result)
1875 (dolist (elt file-coding-system-alist (nreverse result))
1876 (when (and (consp elt) (string-match (car elt) filename))
1877 ;; We found a matching entry in `file-coding-system-alist'.
1878 ;; So we add a similar entry, but with the temporary file name
1879 ;; as regexp.
1880 (push (cons (regexp-quote tmpname) (cdr elt)) result)))))
1882 ;;;###autoload
1883 (progn (defun tramp-run-real-handler (operation args)
1884 "Invoke normal file name handler for OPERATION.
1885 First arg specifies the OPERATION, second arg is a list of arguments to
1886 pass to the OPERATION."
1887 (let* ((inhibit-file-name-handlers
1888 `(tramp-file-name-handler
1889 tramp-vc-file-name-handler
1890 tramp-completion-file-name-handler
1891 cygwin-mount-name-hook-function
1892 cygwin-mount-map-drive-hook-function
1894 ,(and (eq inhibit-file-name-operation operation)
1895 inhibit-file-name-handlers)))
1896 (inhibit-file-name-operation operation))
1897 (apply operation args))))
1899 ;; We handle here all file primitives. Most of them have the file
1900 ;; name as first parameter; nevertheless we check for them explicitly
1901 ;; in order to be signaled if a new primitive appears. This
1902 ;; scenario is needed because there isn't a way to decide by
1903 ;; syntactical means whether a foreign method must be called. It would
1904 ;; ease the life if `file-name-handler-alist' would support a decision
1905 ;; function as well but regexp only.
1906 (defun tramp-file-name-for-operation (operation &rest args)
1907 "Return file name related to OPERATION file primitive.
1908 ARGS are the arguments OPERATION has been called with."
1909 (cond
1910 ;; FILE resp DIRECTORY.
1911 ((member operation
1912 '(access-file byte-compiler-base-file-name delete-directory
1913 delete-file diff-latest-backup-file directory-file-name
1914 directory-files directory-files-and-attributes
1915 dired-compress-file dired-uncache
1916 file-accessible-directory-p file-attributes
1917 file-directory-p file-executable-p file-exists-p
1918 file-local-copy file-modes
1919 file-name-as-directory file-name-directory
1920 file-name-nondirectory file-name-sans-versions
1921 file-ownership-preserved-p file-readable-p
1922 file-regular-p file-remote-p file-symlink-p file-truename
1923 file-writable-p find-backup-file-name find-file-noselect
1924 get-file-buffer insert-directory insert-file-contents
1925 load make-directory make-directory-internal
1926 set-file-modes set-file-times substitute-in-file-name
1927 unhandled-file-name-directory vc-registered
1928 ;; Emacs 24+ only.
1929 file-acl file-notify-add-watch file-selinux-context
1930 set-file-acl set-file-selinux-context
1931 ;; Emacs 26+ only.
1932 file-name-case-insensitive-p))
1933 (if (file-name-absolute-p (nth 0 args))
1934 (nth 0 args)
1935 (expand-file-name (nth 0 args))))
1936 ;; FILE DIRECTORY resp FILE1 FILE2.
1937 ((member operation
1938 '(add-name-to-file copy-directory copy-file expand-file-name
1939 file-name-all-completions file-name-completion
1940 file-newer-than-file-p make-symbolic-link rename-file
1941 ;; Emacs 24+ only.
1942 file-equal-p file-in-directory-p))
1943 (save-match-data
1944 (cond
1945 ((tramp-tramp-file-p (nth 0 args)) (nth 0 args))
1946 ((tramp-tramp-file-p (nth 1 args)) (nth 1 args))
1947 (t (buffer-file-name (current-buffer))))))
1948 ;; START END FILE.
1949 ((eq operation 'write-region)
1950 (nth 2 args))
1951 ;; BUFFER.
1952 ((member operation
1953 '(make-auto-save-file-name
1954 set-visited-file-modtime verify-visited-file-modtime))
1955 (buffer-file-name
1956 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
1957 ;; COMMAND.
1958 ((member operation
1959 '(process-file shell-command start-file-process
1960 ;; Emacs 26+ only.
1961 make-nearby-temp-file temporary-file-directory))
1962 default-directory)
1963 ;; PROC.
1964 ((member operation
1965 '(;; Emacs 24+ only.
1966 file-notify-rm-watch
1967 ;; Emacs 25+ only.
1968 file-notify-valid-p))
1969 (when (processp (nth 0 args))
1970 (with-current-buffer (process-buffer (nth 0 args))
1971 default-directory)))
1972 ;; Unknown file primitive.
1973 (t (error "unknown file I/O primitive: %s" operation))))
1975 (defun tramp-find-foreign-file-name-handler (filename &optional _operation)
1976 "Return foreign file name handler if exists."
1977 (when (tramp-tramp-file-p filename)
1978 (let ((handler tramp-foreign-file-name-handler-alist)
1979 elt res)
1980 (while handler
1981 (setq elt (car handler)
1982 handler (cdr handler))
1983 (when (funcall (car elt) filename)
1984 (setq handler nil
1985 res (cdr elt))))
1986 res)))
1988 (defvar tramp-debug-on-error nil
1989 "Like `debug-on-error' but used Tramp internal.")
1991 (defmacro tramp-condition-case-unless-debug
1992 (var bodyform &rest handlers)
1993 "Like `condition-case-unless-debug' but `tramp-debug-on-error'."
1994 `(let ((debug-on-error tramp-debug-on-error))
1995 (tramp-compat-condition-case-unless-debug ,var ,bodyform ,@handlers)))
1997 ;; Main function.
1998 (defun tramp-file-name-handler (operation &rest args)
1999 "Invoke Tramp file name handler.
2000 Falls back to normal file name handler if no Tramp file name handler exists."
2001 (let ((filename (apply 'tramp-file-name-for-operation operation args)))
2002 (if (and tramp-mode (tramp-tramp-file-p filename))
2003 (save-match-data
2004 (setq filename (tramp-replace-environment-variables filename))
2005 (with-parsed-tramp-file-name filename nil
2006 (let ((completion (tramp-completion-mode-p))
2007 (foreign
2008 (tramp-find-foreign-file-name-handler filename operation))
2009 result)
2010 ;; Call the backend function.
2011 (if foreign
2012 (tramp-condition-case-unless-debug err
2013 (let ((sf (symbol-function foreign)))
2014 ;; Some packages set the default directory to a
2015 ;; remote path, before respective Tramp packages
2016 ;; are already loaded. This results in
2017 ;; recursive loading. Therefore, we load the
2018 ;; Tramp packages locally.
2019 (when (and (listp sf) (eq (car sf) 'autoload))
2020 (let ((default-directory
2021 (tramp-compat-temporary-file-directory)))
2022 (load (cadr sf) 'noerror 'nomessage)))
2023 ;; If `non-essential' is non-nil, Tramp shall
2024 ;; not open a new connection.
2025 ;; If Tramp detects that it shouldn't continue
2026 ;; to work, it throws the `suppress' event.
2027 ;; This could happen for example, when Tramp
2028 ;; tries to open the same connection twice in a
2029 ;; short time frame.
2030 ;; In both cases, we try the default handler then.
2031 (setq result
2032 (catch 'non-essential
2033 (catch 'suppress
2034 (apply foreign operation args))))
2035 (cond
2036 ((eq result 'non-essential)
2037 (tramp-message
2038 v 5 "Non-essential received in operation %s"
2039 (cons operation args))
2040 (tramp-run-real-handler operation args))
2041 ((eq result 'suppress)
2042 (let (tramp-message-show-message)
2043 (tramp-message
2044 v 1 "Suppress received in operation %s"
2045 (cons operation args))
2046 (tramp-cleanup-connection v t)
2047 (tramp-run-real-handler operation args)))
2048 (t result)))
2050 ;; Trace that somebody has interrupted the operation.
2051 ((debug quit)
2052 (let (tramp-message-show-message)
2053 (tramp-message
2054 v 1 "Interrupt received in operation %s"
2055 (cons operation args)))
2056 ;; Propagate the quit signal.
2057 (signal (car err) (cdr err)))
2059 ;; When we are in completion mode, some failed
2060 ;; operations shall return at least a default
2061 ;; value in order to give the user a chance to
2062 ;; correct the file name in the minibuffer.
2063 ;; In order to get a full backtrace, one could apply
2064 ;; (setq tramp-debug-on-error t)
2065 (error
2066 (cond
2067 ((and completion (zerop (length localname))
2068 (memq operation '(file-exists-p file-directory-p)))
2070 ((and completion (zerop (length localname))
2071 (memq operation
2072 '(expand-file-name file-name-as-directory)))
2073 filename)
2074 ;; Propagate the error.
2075 (t (signal (car err) (cdr err))))))
2077 ;; Nothing to do for us. However, since we are in
2078 ;; `tramp-mode', we must suppress the volume letter on
2079 ;; MS Windows.
2080 (setq result (tramp-run-real-handler operation args))
2081 (if (stringp result)
2082 (tramp-drop-volume-letter result)
2083 result)))))
2085 ;; When `tramp-mode' is not enabled, or the file name is quoted,
2086 ;; we don't do anything.
2087 (tramp-run-real-handler operation args))))
2089 ;; In Emacs, there is some concurrency due to timers. If a timer
2090 ;; interrupts Tramp and wishes to use the same connection buffer as
2091 ;; the "main" Emacs, then garbage might occur in the connection
2092 ;; buffer. Therefore, we need to make sure that a timer does not use
2093 ;; the same connection buffer as the "main" Emacs. We implement a
2094 ;; cheap global lock, instead of locking each connection buffer
2095 ;; separately. The global lock is based on two variables,
2096 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
2097 ;; (with setq) to indicate a lock. But Tramp also calls itself during
2098 ;; processing of a single file operation, so we need to allow
2099 ;; recursive calls. That's where the `tramp-locker' variable comes in
2100 ;; -- it is let-bound to t during the execution of the current
2101 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
2102 ;; then we should just proceed because we have been called
2103 ;; recursively. But if `tramp-locker' is nil, then we are a timer
2104 ;; interrupting the "main" Emacs, and then we signal an error.
2106 (defvar tramp-locked nil
2107 "If non-nil, then Tramp is currently busy.
2108 Together with `tramp-locker', this implements a locking mechanism
2109 preventing reentrant calls of Tramp.")
2111 (defvar tramp-locker nil
2112 "If non-nil, then a caller has locked Tramp.
2113 Together with `tramp-locked', this implements a locking mechanism
2114 preventing reentrant calls of Tramp.")
2116 ;; Avoid recursive loading of tramp.el.
2117 ;; FIXME: This must go better. Checking for `operation' is wrong.
2118 ;;;###autoload(defun tramp-completion-file-name-handler (operation &rest args)
2119 ;;;###autoload (let ((fn
2120 ;;;###autoload (assoc
2121 ;;;###autoload operation tramp-completion-file-name-handler-alist)))
2122 ;;;###autoload (if (and
2123 ;;;###autoload tramp-mode fn (null load-in-progress)
2124 ;;;###autoload (member
2125 ;;;###autoload operation
2126 ;;;###autoload '(file-name-all-completions file-name-completion)))
2127 ;;;###autoload (apply 'tramp-autoload-file-name-handler operation args)
2128 ;;;###autoload (tramp-run-real-handler operation args))))
2130 (defun tramp-completion-file-name-handler (operation &rest args)
2131 "Invoke Tramp file name completion handler.
2132 Falls back to normal file name handler if no Tramp file name handler exists."
2133 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
2134 (if (and fn tramp-mode)
2135 (save-match-data (apply (cdr fn) args))
2136 (tramp-run-real-handler operation args))))
2138 ;;;###autoload
2139 (progn (defun tramp-autoload-file-name-handler (operation &rest args)
2140 "Load Tramp file name handler, and perform OPERATION."
2141 (if (and
2142 ;; Do not load tramp.el just for "/".
2143 (not (and (stringp (car args)) (string-equal (car args) "/")))
2144 ;; Avoid recursive loading of tramp.el.
2145 (let ((default-directory temporary-file-directory))
2146 (and (null load-in-progress) (load "tramp" 'noerror 'nomessage))))
2147 (apply operation args)
2148 ;; tramp.el not needed or not available for loading, fall back.
2149 (tramp-run-real-handler operation args))))
2151 ;; `tramp-autoload-file-name-handler' must be registered before
2152 ;; evaluation of site-start and init files, because there might exist
2153 ;; remote files already, f.e. files kept via recentf-mode. We cannot
2154 ;; autoload `tramp-file-name-handler', because it would result in
2155 ;; recursive loading of tramp.el when `default-directory' is set to
2156 ;; remote.
2157 ;;;###autoload
2158 (progn (defun tramp-register-autoload-file-name-handlers ()
2159 "Add Tramp file name handlers to `file-name-handler-alist' during autoload."
2160 (add-to-list 'file-name-handler-alist
2161 (cons tramp-file-name-regexp
2162 'tramp-autoload-file-name-handler))
2163 (put 'tramp-autoload-file-name-handler 'safe-magic t)
2164 (add-to-list 'file-name-handler-alist
2165 (cons tramp-completion-file-name-regexp
2166 'tramp-completion-file-name-handler))
2167 (put 'tramp-completion-file-name-handler 'safe-magic t)))
2169 ;;;###autoload
2170 (tramp-register-autoload-file-name-handlers)
2172 (defun tramp-register-file-name-handlers ()
2173 "Add Tramp file name handlers to `file-name-handler-alist'."
2174 ;; Remove autoloaded handlers from file name handler alist. Useful,
2175 ;; if `tramp-syntax' has been changed.
2176 (dolist (fnh '(tramp-file-name-handler
2177 tramp-completion-file-name-handler
2178 tramp-autoload-file-name-handler))
2179 (let ((a1 (rassq fnh file-name-handler-alist)))
2180 (setq file-name-handler-alist (delq a1 file-name-handler-alist))))
2181 ;; The initial value of `tramp-file-name-regexp' is too simple
2182 ;; minded, but we cannot give it the real value in the autoload
2183 ;; pattern. See Bug#24889.
2184 (setq tramp-file-name-regexp (car tramp-file-name-structure))
2185 ;; Add the handlers.
2186 (add-to-list 'file-name-handler-alist
2187 (cons tramp-file-name-regexp 'tramp-file-name-handler))
2188 (put 'tramp-file-name-handler 'safe-magic t)
2189 (add-to-list 'file-name-handler-alist
2190 (cons tramp-completion-file-name-regexp
2191 'tramp-completion-file-name-handler))
2192 (put 'tramp-completion-file-name-handler 'safe-magic t)
2193 ;; If jka-compr or epa-file are already loaded, move them to the
2194 ;; front of `file-name-handler-alist'.
2195 (dolist (fnh '(epa-file-handler jka-compr-handler))
2196 (let ((entry (rassoc fnh file-name-handler-alist)))
2197 (when entry
2198 (setq file-name-handler-alist
2199 (cons entry (delete entry file-name-handler-alist)))))))
2201 (eval-after-load 'tramp (tramp-register-file-name-handlers))
2203 (defun tramp-exists-file-name-handler (operation &rest args)
2204 "Check, whether OPERATION runs a file name handler."
2205 ;; The file name handler is determined on base of either an
2206 ;; argument, `buffer-file-name', or `default-directory'.
2207 (ignore-errors
2208 (let* ((buffer-file-name "/")
2209 (default-directory "/")
2210 (fnha file-name-handler-alist)
2211 (check-file-name-operation operation)
2212 (file-name-handler-alist
2213 (list
2214 (cons "/"
2215 (lambda (operation &rest args)
2216 "Returns OPERATION if it is the one to be checked."
2217 (if (equal check-file-name-operation operation)
2218 operation
2219 (let ((file-name-handler-alist fnha))
2220 (apply operation args))))))))
2221 (equal (apply operation args) operation))))
2223 ;;;###autoload
2224 (defun tramp-unload-file-name-handlers ()
2225 "Unload Tramp file name handlers from `file-name-handler-alist'."
2226 (setq file-name-handler-alist
2227 (delete (rassoc 'tramp-file-name-handler
2228 file-name-handler-alist)
2229 (delete (rassoc 'tramp-completion-file-name-handler
2230 file-name-handler-alist)
2231 file-name-handler-alist))))
2233 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
2235 ;;; File name handler functions for completion mode:
2237 ;;;###autoload
2238 (defvar tramp-completion-mode nil
2239 "If non-nil, external packages signal that they are in file name completion.")
2240 (make-obsolete-variable 'tramp-completion-mode 'non-essential "26.1")
2242 (defun tramp-completion-mode-p ()
2243 "Check, whether method / user name / host name completion is active."
2245 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
2246 (and (boundp 'non-essential) (symbol-value 'non-essential))
2247 ;; This variable has been obsoleted in Emacs 26.
2248 tramp-completion-mode))
2250 (defun tramp-connectable-p (filename)
2251 "Check, whether it is possible to connect the remote host w/o side-effects.
2252 This is true, if either the remote host is already connected, or if we are
2253 not in completion mode."
2254 (let (tramp-verbose)
2255 (and (tramp-tramp-file-p filename)
2256 (or (not (tramp-completion-mode-p))
2257 (tramp-compat-process-live-p
2258 (tramp-get-connection-process
2259 (tramp-dissect-file-name filename)))))))
2261 (defun tramp-completion-handle-expand-file-name (name &optional dir)
2262 "Like `expand-file-name' for Tramp files."
2263 ;; If DIR is not given, use `default-directory' or "/".
2264 (setq dir (or dir default-directory "/"))
2265 (cond
2266 ((file-name-absolute-p name) name)
2267 ((zerop (length name)) dir)
2268 (t (concat (file-name-as-directory dir) name))))
2270 ;; Method, host name and user name completion.
2271 ;; `tramp-completion-dissect-file-name' returns a list of
2272 ;; tramp-file-name structures. For all of them we return possible completions.
2273 (defun tramp-completion-handle-file-name-all-completions (filename directory)
2274 "Like `file-name-all-completions' for partial Tramp files."
2276 (let ((fullname
2277 (tramp-drop-volume-letter (expand-file-name filename directory)))
2278 hop result result1)
2280 ;; Suppress hop from completion.
2281 (when (string-match
2282 (concat
2283 tramp-prefix-regexp
2284 "\\(" "\\(" tramp-remote-file-name-spec-regexp
2285 tramp-postfix-hop-regexp
2286 "\\)+" "\\)")
2287 fullname)
2288 (setq hop (match-string 1 fullname)
2289 fullname (replace-match "" nil nil fullname 1)))
2291 ;; Possible completion structures.
2292 (dolist (elt (tramp-completion-dissect-file-name fullname))
2293 (let* ((method (tramp-file-name-method elt))
2294 (user (tramp-file-name-user elt))
2295 (host (tramp-file-name-host elt))
2296 (localname (tramp-file-name-localname elt))
2297 (m (tramp-find-method method user host))
2298 (tramp-current-user user) ; see `tramp-parse-passwd'
2299 all-user-hosts)
2301 (unless localname ;; Nothing to complete.
2303 (if (or user host)
2305 ;; Method dependent user / host combinations.
2306 (progn
2307 (mapc
2308 (lambda (x)
2309 (setq all-user-hosts
2310 (append all-user-hosts
2311 (funcall (nth 0 x) (nth 1 x)))))
2312 (tramp-get-completion-function m))
2314 (setq result
2315 (append result
2316 (mapcar
2317 (lambda (x)
2318 (tramp-get-completion-user-host
2319 method user host (nth 0 x) (nth 1 x)))
2320 (delq nil all-user-hosts)))))
2322 ;; Possible methods.
2323 (setq result
2324 (append result (tramp-get-completion-methods m)))))))
2326 ;; Unify list, add hop, remove nil elements.
2327 (dolist (elt result)
2328 (when elt
2329 (string-match tramp-prefix-regexp elt)
2330 (setq elt (replace-match (concat tramp-prefix-format hop) nil nil elt))
2331 (push
2332 (substring elt (length (tramp-drop-volume-letter directory)))
2333 result1)))
2335 ;; Complete local parts.
2336 (append
2337 result1
2338 (ignore-errors
2339 (tramp-run-real-handler
2340 'file-name-all-completions (list filename directory))))))
2342 ;; Method, host name and user name completion for a file.
2343 (defun tramp-completion-handle-file-name-completion
2344 (filename directory &optional predicate)
2345 "Like `file-name-completion' for Tramp files."
2346 (try-completion
2347 filename
2348 (mapcar 'list (file-name-all-completions filename directory))
2349 (when (and predicate
2350 (tramp-connectable-p (expand-file-name filename directory)))
2351 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
2353 ;; I misuse a little bit the tramp-file-name structure in order to
2354 ;; handle completion possibilities for partial methods / user names /
2355 ;; host names. Return value is a list of tramp-file-name structures
2356 ;; according to possible completions. If "localname" is non-nil it
2357 ;; means there shouldn't be a completion anymore.
2359 ;; Expected results:
2361 ;; "/x" "/[x"
2362 ;; ["x" nil nil nil]
2364 ;; "/x:" "/[x/" "/x:y" "/[x/y" "/x:y:" "/[x/y]"
2365 ;; ["x" nil "" nil] ["x" nil "y" nil] ["x" nil "y" ""]
2366 ;; ["x" "" nil nil] ["x" "y" nil nil]
2368 ;; "/x:y@""/[x/y@" "/x:y@z" "/[x/y@z" "/x:y@z:" "/[x/y@z]"
2369 ;;["x" "y" nil nil] ["x" "y" "z" nil] ["x" "y" "z" ""]
2370 (defun tramp-completion-dissect-file-name (name)
2371 "Returns a list of `tramp-file-name' structures.
2372 They are collected by `tramp-completion-dissect-file-name1'."
2374 (let* ((x-nil "\\|\\(\\)")
2375 (tramp-completion-ipv6-regexp
2376 (format
2377 "[^%s]*"
2378 (if (zerop (length tramp-postfix-ipv6-format))
2379 tramp-postfix-host-format
2380 tramp-postfix-ipv6-format)))
2381 ;; "/method" "/[method"
2382 (tramp-completion-file-name-structure1
2383 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
2384 1 nil nil nil))
2385 ;; "/method:user" "/[method/user"
2386 (tramp-completion-file-name-structure2
2387 (list (concat tramp-prefix-regexp
2388 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2389 "\\(" tramp-user-regexp x-nil "\\)$")
2390 1 2 nil nil))
2391 ;; "/method:host" "/[method/host"
2392 (tramp-completion-file-name-structure3
2393 (list (concat tramp-prefix-regexp
2394 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2395 "\\(" tramp-host-regexp x-nil "\\)$")
2396 1 nil 2 nil))
2397 ;; "/method:[ipv6" "/[method/ipv6"
2398 (tramp-completion-file-name-structure4
2399 (list (concat tramp-prefix-regexp
2400 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2401 tramp-prefix-ipv6-regexp
2402 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2403 1 nil 2 nil))
2404 ;; "/method:user@host" "/[method/user@host"
2405 (tramp-completion-file-name-structure5
2406 (list (concat tramp-prefix-regexp
2407 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2408 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2409 "\\(" tramp-host-regexp x-nil "\\)$")
2410 1 2 3 nil))
2411 ;; "/method:user@[ipv6" "/[method/user@ipv6"
2412 (tramp-completion-file-name-structure6
2413 (list (concat tramp-prefix-regexp
2414 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
2415 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
2416 tramp-prefix-ipv6-regexp
2417 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
2418 1 2 3 nil)))
2421 (delq
2423 (mapcar
2424 (lambda (structure) (tramp-completion-dissect-file-name1 structure name))
2425 (list
2426 tramp-completion-file-name-structure1
2427 tramp-completion-file-name-structure2
2428 tramp-completion-file-name-structure3
2429 tramp-completion-file-name-structure4
2430 tramp-completion-file-name-structure5
2431 tramp-completion-file-name-structure6)))))
2433 (defun tramp-completion-dissect-file-name1 (structure name)
2434 "Returns a `tramp-file-name' structure matching STRUCTURE.
2435 The structure consists of remote method, remote user,
2436 remote host and localname (filename on remote host)."
2438 (save-match-data
2439 (when (string-match (nth 0 structure) name)
2440 (let ((method (and (nth 1 structure)
2441 (match-string (nth 1 structure) name)))
2442 (user (and (nth 2 structure)
2443 (match-string (nth 2 structure) name)))
2444 (host (and (nth 3 structure)
2445 (match-string (nth 3 structure) name)))
2446 (localname (and (nth 4 structure)
2447 (match-string (nth 4 structure) name))))
2448 (vector method user host localname nil)))))
2450 ;; This function returns all possible method completions, adding the
2451 ;; trailing method delimiter.
2452 (defun tramp-get-completion-methods (partial-method)
2453 "Returns all method completions for PARTIAL-METHOD."
2454 (mapcar
2455 (lambda (method)
2456 (and method
2457 (string-match (concat "^" (regexp-quote partial-method)) method)
2458 (tramp-completion-make-tramp-file-name method nil nil nil)))
2459 (mapcar 'car tramp-methods)))
2461 ;; Compares partial user and host names with possible completions.
2462 (defun tramp-get-completion-user-host
2463 (method partial-user partial-host user host)
2464 "Returns the most expanded string for user and host name completion.
2465 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
2466 (cond
2468 ((and partial-user partial-host)
2469 (if (and host
2470 (string-match (concat "^" (regexp-quote partial-host)) host)
2471 (string-equal partial-user (or user partial-user)))
2472 (setq user partial-user)
2473 (setq user nil
2474 host nil)))
2476 (partial-user
2477 (setq host nil)
2478 (unless
2479 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
2480 (setq user nil)))
2482 (partial-host
2483 (setq user nil)
2484 (unless
2485 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
2486 (setq host nil)))
2488 (t (setq user nil
2489 host nil)))
2491 (unless (zerop (+ (length user) (length host)))
2492 (tramp-completion-make-tramp-file-name method user host nil)))
2494 (defun tramp-parse-default-user-host (method)
2495 "Return a list of (user host) tuples allowed to access for METHOD.
2496 This function is added always in `tramp-get-completion-function'
2497 for all methods. Resulting data are derived from default settings."
2498 `((,(tramp-find-user method nil nil) ,(tramp-find-host method nil nil))))
2500 ;; Generic function.
2501 (defun tramp-parse-group (regexp match-level skip-regexp)
2502 "Return a (user host) tuple allowed to access.
2503 User is always nil."
2504 (let (result)
2505 (when (re-search-forward regexp (point-at-eol) t)
2506 (setq result (list nil (match-string match-level))))
2508 (> (skip-chars-forward skip-regexp) 0)
2509 (forward-line 1))
2510 result))
2512 ;; Generic function.
2513 (defun tramp-parse-file (filename function)
2514 "Return a list of (user host) tuples allowed to access.
2515 User is always nil."
2516 ;; On Windows, there are problems in completion when
2517 ;; `default-directory' is remote.
2518 (let ((default-directory (tramp-compat-temporary-file-directory)))
2519 (when (file-readable-p filename)
2520 (with-temp-buffer
2521 (insert-file-contents filename)
2522 (goto-char (point-min))
2523 (loop while (not (eobp)) collect (funcall function))))))
2525 ;;;###tramp-autoload
2526 (defun tramp-parse-rhosts (filename)
2527 "Return a list of (user host) tuples allowed to access.
2528 Either user or host may be nil."
2529 (tramp-parse-file filename 'tramp-parse-rhosts-group))
2531 (defun tramp-parse-rhosts-group ()
2532 "Return a (user host) tuple allowed to access.
2533 Either user or host may be nil."
2534 (let ((result)
2535 (regexp
2536 (concat
2537 "^\\(" tramp-host-regexp "\\)"
2538 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
2539 (when (re-search-forward regexp (point-at-eol) t)
2540 (setq result (append (list (match-string 3) (match-string 1)))))
2541 (forward-line 1)
2542 result))
2544 ;;;###tramp-autoload
2545 (defun tramp-parse-shosts (filename)
2546 "Return a list of (user host) tuples allowed to access.
2547 User is always nil."
2548 (tramp-parse-file filename 'tramp-parse-shosts-group))
2550 (defun tramp-parse-shosts-group ()
2551 "Return a (user host) tuple allowed to access.
2552 User is always nil."
2553 (tramp-parse-group (concat "^\\(" tramp-host-regexp "\\)") 1 ","))
2555 ;;;###tramp-autoload
2556 (defun tramp-parse-sconfig (filename)
2557 "Return a list of (user host) tuples allowed to access.
2558 User is always nil."
2559 (tramp-parse-file filename 'tramp-parse-sconfig-group))
2561 (defun tramp-parse-sconfig-group ()
2562 "Return a (user host) tuple allowed to access.
2563 User is always nil."
2564 (tramp-parse-group
2565 (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)") 1 ","))
2567 ;; Generic function.
2568 (defun tramp-parse-shostkeys-sknownhosts (dirname regexp)
2569 "Return a list of (user host) tuples allowed to access.
2570 User is always nil."
2571 ;; On Windows, there are problems in completion when
2572 ;; `default-directory' is remote.
2573 (let* ((default-directory (tramp-compat-temporary-file-directory))
2574 (files (and (file-directory-p dirname) (directory-files dirname))))
2575 (loop for f in files
2576 when (and (not (string-match "^\\.\\.?$" f)) (string-match regexp f))
2577 collect (list nil (match-string 1 f)))))
2579 ;;;###tramp-autoload
2580 (defun tramp-parse-shostkeys (dirname)
2581 "Return a list of (user host) tuples allowed to access.
2582 User is always nil."
2583 (tramp-parse-shostkeys-sknownhosts
2584 dirname (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$")))
2586 ;;;###tramp-autoload
2587 (defun tramp-parse-sknownhosts (dirname)
2588 "Return a list of (user host) tuples allowed to access.
2589 User is always nil."
2590 (tramp-parse-shostkeys-sknownhosts
2591 dirname
2592 (concat "^\\(" tramp-host-regexp "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")))
2594 ;;;###tramp-autoload
2595 (defun tramp-parse-hosts (filename)
2596 "Return a list of (user host) tuples allowed to access.
2597 User is always nil."
2598 (tramp-parse-file filename 'tramp-parse-hosts-group))
2600 (defun tramp-parse-hosts-group ()
2601 "Return a (user host) tuple allowed to access.
2602 User is always nil."
2603 (tramp-parse-group
2604 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)") 1 " \t"))
2606 ;;;###tramp-autoload
2607 (defun tramp-parse-passwd (filename)
2608 "Return a list of (user host) tuples allowed to access.
2609 Host is always \"localhost\"."
2610 (with-tramp-connection-property nil "parse-passwd"
2611 (if (executable-find "getent")
2612 (with-temp-buffer
2613 (when (zerop (tramp-call-process nil "getent" nil t nil "passwd"))
2614 (goto-char (point-min))
2615 (loop while (not (eobp)) collect
2616 (tramp-parse-etc-group-group))))
2617 (tramp-parse-file filename 'tramp-parse-passwd-group))))
2619 (defun tramp-parse-passwd-group ()
2620 "Return a (user host) tuple allowed to access.
2621 Host is always \"localhost\"."
2622 (let ((result)
2623 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
2624 (when (re-search-forward regexp (point-at-eol) t)
2625 (setq result (list (match-string 1) "localhost")))
2626 (forward-line 1)
2627 result))
2629 ;;;###tramp-autoload
2630 (defun tramp-parse-etc-group (filename)
2631 "Return a list of (group host) tuples allowed to access.
2632 Host is always \"localhost\"."
2633 (with-tramp-connection-property nil "parse-group"
2634 (if (executable-find "getent")
2635 (with-temp-buffer
2636 (when (zerop (tramp-call-process nil "getent" nil t nil "group"))
2637 (goto-char (point-min))
2638 (loop while (not (eobp)) collect
2639 (tramp-parse-etc-group-group))))
2640 (tramp-parse-file filename 'tramp-parse-etc-group-group))))
2642 (defun tramp-parse-etc-group-group ()
2643 "Return a (group host) tuple allowed to access.
2644 Host is always \"localhost\"."
2645 (let ((result)
2646 (split (split-string (buffer-substring (point) (point-at-eol)) ":")))
2647 (when (member (user-login-name) (split-string (nth 3 split) "," 'omit))
2648 (setq result (list (nth 0 split) "localhost")))
2649 (forward-line 1)
2650 result))
2652 ;;;###tramp-autoload
2653 (defun tramp-parse-netrc (filename)
2654 "Return a list of (user host) tuples allowed to access.
2655 User may be nil."
2656 (tramp-parse-file filename 'tramp-parse-netrc-group))
2658 (defun tramp-parse-netrc-group ()
2659 "Return a (user host) tuple allowed to access.
2660 User may be nil."
2661 (let ((result)
2662 (regexp
2663 (concat
2664 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
2665 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
2666 (when (re-search-forward regexp (point-at-eol) t)
2667 (setq result (list (match-string 3) (match-string 1))))
2668 (forward-line 1)
2669 result))
2671 ;;;###tramp-autoload
2672 (defun tramp-parse-putty (registry-or-dirname)
2673 "Return a list of (user host) tuples allowed to access.
2674 User is always nil."
2675 (if (memq system-type '(windows-nt))
2676 (with-tramp-connection-property nil "parse-putty"
2677 (with-temp-buffer
2678 (when (zerop (tramp-call-process
2679 nil "reg" nil t nil "query" registry-or-dirname))
2680 (goto-char (point-min))
2681 (loop while (not (eobp)) collect
2682 (tramp-parse-putty-group registry-or-dirname)))))
2683 ;; UNIX case.
2684 (tramp-parse-shostkeys-sknownhosts
2685 registry-or-dirname (concat "^\\(" tramp-host-regexp "\\)$"))))
2687 (defun tramp-parse-putty-group (registry)
2688 "Return a (user host) tuple allowed to access.
2689 User is always nil."
2690 (let ((result)
2691 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
2692 (when (re-search-forward regexp (point-at-eol) t)
2693 (setq result (list nil (match-string 1))))
2694 (forward-line 1)
2695 result))
2697 ;;; Common file name handler functions for different backends:
2699 (defvar tramp-handle-file-local-copy-hook nil
2700 "Normal hook to be run at the end of `tramp-*-handle-file-local-copy'.")
2702 (defvar tramp-handle-write-region-hook nil
2703 "Normal hook to be run at the end of `tramp-*-handle-write-region'.")
2705 (defun tramp-handle-directory-file-name (directory)
2706 "Like `directory-file-name' for Tramp files."
2707 ;; If localname component of filename is "/", leave it unchanged.
2708 ;; Otherwise, remove any trailing slash from localname component.
2709 ;; Method, host, etc, are unchanged. Does it make sense to try
2710 ;; to avoid parsing the filename?
2711 (with-parsed-tramp-file-name directory nil
2712 (if (and (not (zerop (length localname)))
2713 (eq (aref localname (1- (length localname))) ?/)
2714 (not (string= localname "/")))
2715 (substring directory 0 -1)
2716 directory)))
2718 (defun tramp-handle-directory-files (directory &optional full match nosort)
2719 "Like `directory-files' for Tramp files."
2720 (when (file-directory-p directory)
2721 (setq directory (file-name-as-directory (expand-file-name directory)))
2722 (let ((temp (nreverse (file-name-all-completions "" directory)))
2723 result item)
2725 (while temp
2726 (setq item (directory-file-name (pop temp)))
2727 (when (or (null match) (string-match match item))
2728 (push (if full (concat directory item) item)
2729 result)))
2730 (if nosort result (sort result 'string<)))))
2732 (defun tramp-handle-directory-files-and-attributes
2733 (directory &optional full match nosort id-format)
2734 "Like `directory-files-and-attributes' for Tramp files."
2735 (mapcar
2736 (lambda (x)
2737 (cons x (file-attributes
2738 (if full x (expand-file-name x directory)) id-format)))
2739 (directory-files directory full match nosort)))
2741 (defun tramp-handle-dired-uncache (dir)
2742 "Like `dired-uncache' for Tramp files."
2743 (with-parsed-tramp-file-name
2744 (if (file-directory-p dir) dir (file-name-directory dir)) nil
2745 (tramp-flush-directory-property v localname)))
2747 (defun tramp-handle-file-accessible-directory-p (filename)
2748 "Like `file-accessible-directory-p' for Tramp files."
2749 (and (file-directory-p filename)
2750 (file-readable-p filename)))
2752 (defun tramp-handle-file-equal-p (filename1 filename2)
2753 "Like `file-equalp-p' for Tramp files."
2754 ;; Native `file-equalp-p' calls `file-truename', which requires a
2755 ;; remote connection. This can be avoided, if FILENAME1 and
2756 ;; FILENAME2 are not located on the same remote host.
2757 (when (string-equal
2758 (file-remote-p (expand-file-name filename1))
2759 (file-remote-p (expand-file-name filename2)))
2760 (tramp-run-real-handler 'file-equal-p (list filename1 filename2))))
2762 (defun tramp-handle-file-exists-p (filename)
2763 "Like `file-exists-p' for Tramp files."
2764 (not (null (file-attributes filename))))
2766 (defun tramp-handle-file-in-directory-p (filename directory)
2767 "Like `file-in-directory-p' for Tramp files."
2768 ;; Native `file-in-directory-p' calls `file-truename', which
2769 ;; requires a remote connection. This can be avoided, if FILENAME
2770 ;; and DIRECTORY are not located on the same remote host.
2771 (when (string-equal
2772 (file-remote-p (expand-file-name filename))
2773 (file-remote-p (expand-file-name directory)))
2774 (tramp-run-real-handler 'file-in-directory-p (list filename directory))))
2776 (defun tramp-handle-file-modes (filename)
2777 "Like `file-modes' for Tramp files."
2778 (let ((truename (or (file-truename filename) filename)))
2779 (when (file-exists-p truename)
2780 (tramp-mode-string-to-int
2781 (tramp-compat-file-attribute-modes (file-attributes truename))))))
2783 ;; Localname manipulation functions that grok Tramp localnames...
2784 (defun tramp-handle-file-name-as-directory (file)
2785 "Like `file-name-as-directory' but aware of Tramp files."
2786 ;; `file-name-as-directory' would be sufficient except localname is
2787 ;; the empty string.
2788 (let ((v (tramp-dissect-file-name file t)))
2789 ;; Run the command on the localname portion only unless we are in
2790 ;; completion mode.
2791 (tramp-make-tramp-file-name
2792 (tramp-file-name-method v)
2793 (tramp-file-name-user v)
2794 (tramp-file-name-host v)
2795 (if (and (zerop (length (tramp-file-name-localname v)))
2796 (not (tramp-connectable-p file)))
2798 (tramp-run-real-handler
2799 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))
2800 (tramp-file-name-hop v))))
2802 (defun tramp-handle-file-name-case-insensitive-p (filename)
2803 "Like `file-name-case-insensitive-p' for Tramp files."
2804 ;; We make it a connection property, assuming that all file systems
2805 ;; on the remote host behave similar. This might be wrong for
2806 ;; mounted NFS directories or SMB/AFP shares; such more granular
2807 ;; tests will be added in case they are needed.
2808 (setq filename (expand-file-name filename))
2809 (with-parsed-tramp-file-name filename nil
2810 (or ;; Maybe there is a default value.
2811 (tramp-get-method-parameter v 'tramp-case-insensitive)
2813 ;; There isn't. So we must check, in case there's a connection already.
2814 (and (tramp-connectable-p filename)
2815 (with-tramp-connection-property v "case-insensitive"
2816 ;; The idea is to compare a file with lower case letters
2817 ;; with the same file with upper case letters.
2818 (let ((candidate
2819 (tramp-compat-file-name-unquote
2820 (directory-file-name filename)))
2821 tmpfile)
2822 ;; Check, whether we find an existing file with lower case
2823 ;; letters. This avoids us to create a temporary file.
2824 (while (and (string-match
2825 "[a-z]" (file-remote-p candidate 'localname))
2826 (not (file-exists-p candidate)))
2827 (setq candidate
2828 (directory-file-name (file-name-directory candidate))))
2829 ;; Nothing found, so we must use a temporary file for
2830 ;; comparison. `make-nearby-temp-file' is added to
2831 ;; Emacs 26+ like `file-name-case-insensitive-p', so
2832 ;; there is no compatibility problem calling it.
2833 (unless
2834 (string-match "[a-z]" (file-remote-p candidate 'localname))
2835 (setq tmpfile
2836 (let ((default-directory (file-name-directory filename)))
2837 (tramp-compat-funcall 'make-nearby-temp-file "tramp."))
2838 candidate tmpfile))
2839 ;; Check for the existence of the same file with upper
2840 ;; case letters.
2841 (unwind-protect
2842 (file-exists-p
2843 (concat
2844 (file-remote-p candidate)
2845 (upcase (file-remote-p candidate 'localname))))
2846 ;; Cleanup.
2847 (when tmpfile (delete-file tmpfile)))))))))
2849 (defun tramp-handle-file-name-completion
2850 (filename directory &optional predicate)
2851 "Like `file-name-completion' for Tramp files."
2852 (unless (tramp-tramp-file-p directory)
2853 (error
2854 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2855 directory))
2856 (let (hits-ignored-extensions)
2858 (try-completion
2859 filename (file-name-all-completions filename directory)
2860 (lambda (x)
2861 (when (funcall (or predicate 'identity) (expand-file-name x directory))
2862 (not
2863 (and
2864 completion-ignored-extensions
2865 (string-match
2866 (concat (regexp-opt completion-ignored-extensions 'paren) "$") x)
2867 ;; We remember the hit.
2868 (push x hits-ignored-extensions))))))
2869 ;; No match. So we try again for ignored files.
2870 (try-completion filename hits-ignored-extensions))))
2872 (defun tramp-handle-file-name-directory (file)
2873 "Like `file-name-directory' but aware of Tramp files."
2874 ;; Everything except the last filename thing is the directory. We
2875 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2876 ;; the remote file name parts. This is a problem when we are in
2877 ;; file name completion.
2878 (let ((v (tramp-dissect-file-name file t)))
2879 ;; Run the command on the localname portion only.
2880 (tramp-make-tramp-file-name
2881 (tramp-file-name-method v)
2882 (tramp-file-name-user v)
2883 (tramp-file-name-host v)
2884 (tramp-run-real-handler
2885 'file-name-directory (list (or (tramp-file-name-localname v) "")))
2886 (tramp-file-name-hop v))))
2888 (defun tramp-handle-file-name-nondirectory (file)
2889 "Like `file-name-nondirectory' but aware of Tramp files."
2890 (with-parsed-tramp-file-name file nil
2891 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2893 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2894 "Like `file-newer-than-file-p' for Tramp files."
2895 (cond
2896 ((not (file-exists-p file1)) nil)
2897 ((not (file-exists-p file2)) t)
2898 (t (time-less-p (tramp-compat-file-attribute-modification-time
2899 (file-attributes file2))
2900 (tramp-compat-file-attribute-modification-time
2901 (file-attributes file1))))))
2903 (defun tramp-handle-file-regular-p (filename)
2904 "Like `file-regular-p' for Tramp files."
2905 (and (file-exists-p filename)
2906 (eq ?-
2907 (aref (tramp-compat-file-attribute-modes (file-attributes filename))
2908 0))))
2910 (defun tramp-handle-file-remote-p (filename &optional identification connected)
2911 "Like `file-remote-p' for Tramp files."
2912 ;; We do not want traces in the debug buffer.
2913 (let ((tramp-verbose (min tramp-verbose 3)))
2914 (when (tramp-tramp-file-p filename)
2915 (let* ((v (tramp-dissect-file-name filename))
2916 (p (tramp-get-connection-process v))
2917 (c (and (tramp-compat-process-live-p p)
2918 (tramp-get-connection-property p "connected" nil))))
2919 ;; We expand the file name only, if there is already a connection.
2920 (with-parsed-tramp-file-name
2921 (if c (expand-file-name filename) filename) nil
2922 (and (or (not connected) c)
2923 (cond
2924 ((eq identification 'method) method)
2925 ((eq identification 'user) user)
2926 ((eq identification 'host) host)
2927 ((eq identification 'localname) localname)
2928 ((eq identification 'hop) hop)
2929 (t (tramp-make-tramp-file-name method user host "" hop)))))))))
2931 (defun tramp-handle-file-symlink-p (filename)
2932 "Like `file-symlink-p' for Tramp files."
2933 (with-parsed-tramp-file-name filename nil
2934 (let ((x (tramp-compat-file-attribute-type (file-attributes filename))))
2935 (when (stringp x)
2936 (if (file-name-absolute-p x)
2937 (tramp-make-tramp-file-name method user host x)
2938 x)))))
2940 (defun tramp-handle-find-backup-file-name (filename)
2941 "Like `find-backup-file-name' for Tramp files."
2942 (with-parsed-tramp-file-name filename nil
2943 (let ((backup-directory-alist
2944 (if tramp-backup-directory-alist
2945 (mapcar
2946 (lambda (x)
2947 (cons
2948 (car x)
2949 (if (and (stringp (cdr x))
2950 (file-name-absolute-p (cdr x))
2951 (not (tramp-file-name-p (cdr x))))
2952 (tramp-make-tramp-file-name method user host (cdr x))
2953 (cdr x))))
2954 tramp-backup-directory-alist)
2955 backup-directory-alist)))
2956 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
2958 (defun tramp-handle-insert-directory
2959 (filename switches &optional wildcard full-directory-p)
2960 "Like `insert-directory' for Tramp files."
2961 (unless switches (setq switches ""))
2962 ;; Mark trailing "/".
2963 (when (and (zerop (length (file-name-nondirectory filename)))
2964 (not full-directory-p))
2965 (setq switches (concat switches "F")))
2966 (with-parsed-tramp-file-name (expand-file-name filename) nil
2967 (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
2968 (require 'ls-lisp)
2969 (let (ls-lisp-use-insert-directory-program start)
2970 (tramp-run-real-handler
2971 'insert-directory
2972 (list filename switches wildcard full-directory-p))
2973 ;; `ls-lisp' always returns full listings. We must remove
2974 ;; superfluous parts.
2975 (unless (string-match "l" switches)
2976 (save-excursion
2977 (goto-char (point-min))
2978 (while (setq start
2979 (text-property-not-all
2980 (point) (point-at-eol) 'dired-filename t))
2981 (delete-region
2982 start
2983 (or (text-property-any start (point-at-eol) 'dired-filename t)
2984 (point-at-eol)))
2985 (if (= (point-at-bol) (point-at-eol))
2986 ;; Empty line.
2987 (delete-region (point) (progn (forward-line) (point)))
2988 (forward-line)))))))))
2990 (defun tramp-handle-insert-file-contents
2991 (filename &optional visit beg end replace)
2992 "Like `insert-file-contents' for Tramp files."
2993 (barf-if-buffer-read-only)
2994 (setq filename (expand-file-name filename))
2995 (let (result local-copy remote-copy)
2996 (with-parsed-tramp-file-name filename nil
2997 (unwind-protect
2998 (if (not (file-exists-p filename))
2999 (tramp-error
3000 v tramp-file-missing
3001 "File `%s' not found on remote host" filename)
3003 (with-tramp-progress-reporter
3004 v 3 (format-message "Inserting `%s'" filename)
3005 (condition-case err
3006 (if (and (tramp-local-host-p v)
3007 (let (file-name-handler-alist)
3008 (file-readable-p localname)))
3009 ;; Short track: if we are on the local host, we can
3010 ;; run directly.
3011 (setq result
3012 (tramp-run-real-handler
3013 'insert-file-contents
3014 (list localname visit beg end replace)))
3016 ;; When we shall insert only a part of the file, we
3017 ;; copy this part. This works only for the shell file
3018 ;; name handlers.
3019 (when (and (or beg end)
3020 (tramp-get-method-parameter
3021 v 'tramp-login-program))
3022 (setq remote-copy (tramp-make-tramp-temp-file v))
3023 ;; This is defined in tramp-sh.el. Let's assume
3024 ;; this is loaded already.
3025 (tramp-compat-funcall
3026 'tramp-send-command
3028 (cond
3029 ((and beg end)
3030 (format "dd bs=1 skip=%d if=%s count=%d of=%s"
3031 beg (tramp-shell-quote-argument localname)
3032 (- end beg) remote-copy))
3033 (beg
3034 (format "dd bs=1 skip=%d if=%s of=%s"
3035 beg (tramp-shell-quote-argument localname)
3036 remote-copy))
3037 (end
3038 (format "dd bs=1 count=%d if=%s of=%s"
3039 end (tramp-shell-quote-argument localname)
3040 remote-copy))))
3041 (setq tramp-temp-buffer-file-name nil beg nil end nil))
3043 ;; `insert-file-contents-literally' takes care to
3044 ;; avoid calling jka-compr.el and epa.el. By
3045 ;; let-binding `inhibit-file-name-operation', we
3046 ;; propagate that care to the `file-local-copy'
3047 ;; operation.
3048 (setq local-copy
3049 (let ((inhibit-file-name-operation
3050 (when (eq inhibit-file-name-operation
3051 'insert-file-contents)
3052 'file-local-copy)))
3053 (cond
3054 ((stringp remote-copy)
3055 (file-local-copy
3056 (tramp-make-tramp-file-name
3057 method user host remote-copy)))
3058 ((stringp tramp-temp-buffer-file-name)
3059 (copy-file
3060 filename tramp-temp-buffer-file-name 'ok)
3061 tramp-temp-buffer-file-name)
3062 (t (file-local-copy filename)))))
3064 ;; When the file is not readable for the owner, it
3065 ;; cannot be inserted, even if it is readable for the
3066 ;; group or for everybody.
3067 (set-file-modes local-copy (string-to-number "0600" 8))
3069 (when (and (null remote-copy)
3070 (tramp-get-method-parameter
3071 v 'tramp-copy-keep-tmpfile))
3072 ;; We keep the local file for performance reasons,
3073 ;; useful for "rsync".
3074 (setq tramp-temp-buffer-file-name local-copy))
3076 ;; We must ensure that `file-coding-system-alist'
3077 ;; matches `local-copy'.
3078 (let ((file-coding-system-alist
3079 (tramp-find-file-name-coding-system-alist
3080 filename local-copy)))
3081 (setq result
3082 (insert-file-contents
3083 local-copy visit beg end replace))))
3084 (error
3085 (add-hook 'find-file-not-found-functions
3086 `(lambda () (signal ',(car err) ',(cdr err)))
3087 nil t)
3088 (signal (car err) (cdr err))))))
3090 ;; Save exit.
3091 (progn
3092 (when visit
3093 (setq buffer-file-name filename)
3094 (setq buffer-read-only (not (file-writable-p filename)))
3095 (set-visited-file-modtime)
3096 (set-buffer-modified-p nil))
3097 (when (and (stringp local-copy)
3098 (or remote-copy (null tramp-temp-buffer-file-name)))
3099 (delete-file local-copy))
3100 (when (stringp remote-copy)
3101 (delete-file
3102 (tramp-make-tramp-file-name method user host remote-copy)))))
3104 ;; Result.
3105 (list (expand-file-name filename)
3106 (cadr result)))))
3108 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
3109 "Like `load' for Tramp files."
3110 (with-parsed-tramp-file-name (expand-file-name file) nil
3111 (unless nosuffix
3112 (cond ((file-exists-p (concat file ".elc"))
3113 (setq file (concat file ".elc")))
3114 ((file-exists-p (concat file ".el"))
3115 (setq file (concat file ".el")))))
3116 (when must-suffix
3117 ;; The first condition is always true for absolute file names.
3118 ;; Included for safety's sake.
3119 (unless (or (file-name-directory file)
3120 (string-match "\\.elc?\\'" file))
3121 (tramp-error
3122 v 'file-error
3123 "File `%s' does not include a `.el' or `.elc' suffix" file)))
3124 (unless noerror
3125 (when (not (file-exists-p file))
3126 (tramp-error
3127 v tramp-file-missing "Cannot load nonexistent file `%s'" file)))
3128 (if (not (file-exists-p file))
3130 (let ((tramp-message-show-message (not nomessage)))
3131 (with-tramp-progress-reporter v 0 (format "Loading %s" file)
3132 (let ((local-copy (file-local-copy file)))
3133 (unwind-protect
3134 (load local-copy noerror t nosuffix must-suffix)
3135 (delete-file local-copy)))))
3136 t)))
3138 (defun tramp-handle-make-symbolic-link
3139 (filename linkname &optional _ok-if-already-exists)
3140 "Like `make-symbolic-link' for Tramp files."
3141 (with-parsed-tramp-file-name
3142 (if (tramp-tramp-file-p filename) filename linkname) nil
3143 (tramp-error v 'file-error "make-symbolic-link not supported")))
3145 (defun tramp-handle-shell-command
3146 (command &optional output-buffer error-buffer)
3147 "Like `shell-command' for Tramp files."
3148 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3149 ;; We cannot use `shell-file-name' and `shell-command-switch',
3150 ;; they are variables of the local host.
3151 (args (append
3152 (cons
3153 (tramp-get-method-parameter
3154 (tramp-dissect-file-name default-directory)
3155 'tramp-remote-shell)
3156 (tramp-get-method-parameter
3157 (tramp-dissect-file-name default-directory)
3158 'tramp-remote-shell-args))
3159 (list (substring command 0 asynchronous))))
3160 current-buffer-p
3161 (output-buffer
3162 (cond
3163 ((bufferp output-buffer) output-buffer)
3164 ((stringp output-buffer) (get-buffer-create output-buffer))
3165 (output-buffer
3166 (setq current-buffer-p t)
3167 (current-buffer))
3168 (t (get-buffer-create
3169 (if asynchronous
3170 "*Async Shell Command*"
3171 "*Shell Command Output*")))))
3172 (error-buffer
3173 (cond
3174 ((bufferp error-buffer) error-buffer)
3175 ((stringp error-buffer) (get-buffer-create error-buffer))))
3176 (buffer
3177 (if (and (not asynchronous) error-buffer)
3178 (with-parsed-tramp-file-name default-directory nil
3179 (list output-buffer (tramp-make-tramp-temp-file v)))
3180 output-buffer))
3181 (p (get-buffer-process output-buffer)))
3183 ;; Check whether there is another process running. Tramp does not
3184 ;; support 2 (asynchronous) processes in parallel.
3185 (when p
3186 (if (yes-or-no-p "A command is running. Kill it? ")
3187 (ignore-errors (kill-process p))
3188 (tramp-compat-user-error p "Shell command in progress")))
3190 (if current-buffer-p
3191 (progn
3192 (barf-if-buffer-read-only)
3193 (push-mark nil t))
3194 (with-current-buffer output-buffer
3195 (setq buffer-read-only nil)
3196 (erase-buffer)))
3198 (if (and (not current-buffer-p) (integerp asynchronous))
3199 (prog1
3200 ;; Run the process.
3201 (setq p (apply 'start-file-process "*Async Shell*" buffer args))
3202 ;; Display output.
3203 (with-current-buffer output-buffer
3204 (display-buffer output-buffer '(nil (allow-no-window . t)))
3205 (setq mode-line-process '(":%s"))
3206 (shell-mode)
3207 (set-process-sentinel p 'shell-command-sentinel)
3208 (set-process-filter p 'comint-output-filter)))
3210 (prog1
3211 ;; Run the process.
3212 (apply 'process-file (car args) nil buffer nil (cdr args))
3213 ;; Insert error messages if they were separated.
3214 (when (listp buffer)
3215 (with-current-buffer error-buffer
3216 (insert-file-contents (cadr buffer)))
3217 (delete-file (cadr buffer)))
3218 (if current-buffer-p
3219 ;; This is like exchange-point-and-mark, but doesn't
3220 ;; activate the mark. It is cleaner to avoid activation,
3221 ;; even though the command loop would deactivate the mark
3222 ;; because we inserted text.
3223 (goto-char (prog1 (mark t)
3224 (set-marker (mark-marker) (point)
3225 (current-buffer))))
3226 ;; There's some output, display it.
3227 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
3228 (display-message-or-buffer output-buffer)))))))
3230 (defun tramp-handle-substitute-in-file-name (filename)
3231 "Like `substitute-in-file-name' for Tramp files.
3232 \"//\" and \"/~\" substitute only in the local filename part."
3233 ;; Check, whether the local part is a quoted file name.
3234 (if (tramp-compat-file-name-quoted-p filename)
3235 filename
3236 ;; First, we must replace environment variables.
3237 (setq filename (tramp-replace-environment-variables filename))
3238 (with-parsed-tramp-file-name filename nil
3239 ;; Ignore in LOCALNAME everything before "//" or "/~".
3240 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
3241 (setq filename
3242 (concat (file-remote-p filename)
3243 (replace-match "\\1" nil nil localname)))
3244 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
3245 (when (string-match "~$" filename)
3246 (setq filename (concat filename "/"))))
3247 ;; We do not want to replace environment variables, again.
3248 (let (process-environment)
3249 (tramp-run-real-handler 'substitute-in-file-name (list filename))))))
3251 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
3252 "Like `set-visited-file-modtime' for Tramp files."
3253 (unless (buffer-file-name)
3254 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
3255 (buffer-name)))
3256 (unless time-list
3257 (let ((remote-file-name-inhibit-cache t))
3258 ;; '(-1 65535) means file doesn't exists yet.
3259 (setq time-list
3260 (or (tramp-compat-file-attribute-modification-time
3261 (file-attributes (buffer-file-name)))
3262 '(-1 65535)))))
3263 ;; We use '(0 0) as a don't-know value.
3264 (unless (equal time-list '(0 0))
3265 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))))
3267 (defun tramp-handle-verify-visited-file-modtime (&optional buf)
3268 "Like `verify-visited-file-modtime' for Tramp files.
3269 At the time `verify-visited-file-modtime' calls this function, we
3270 already know that the buffer is visiting a file and that
3271 `visited-file-modtime' does not return 0. Do not call this
3272 function directly, unless those two cases are already taken care
3273 of."
3274 (with-current-buffer (or buf (current-buffer))
3275 (let ((f (buffer-file-name)))
3276 ;; There is no file visiting the buffer, or the buffer has no
3277 ;; recorded last modification time, or there is no established
3278 ;; connection.
3279 (if (or (not f)
3280 (eq (visited-file-modtime) 0)
3281 (not (file-remote-p f nil 'connected)))
3283 (with-parsed-tramp-file-name f nil
3284 (let* ((remote-file-name-inhibit-cache t)
3285 (attr (file-attributes f))
3286 (modtime (tramp-compat-file-attribute-modification-time attr))
3287 (mt (visited-file-modtime)))
3289 (cond
3290 ;; File exists, and has a known modtime.
3291 ((and attr (not (equal modtime '(0 0))))
3292 (< (abs (tramp-time-diff
3293 modtime
3294 ;; For compatibility, deal with both the old
3295 ;; (HIGH . LOW) and the new (HIGH LOW) return
3296 ;; values of `visited-file-modtime'.
3297 (if (atom (cdr mt))
3298 (list (car mt) (cdr mt))
3299 mt)))
3301 ;; Modtime has the don't know value.
3302 (attr t)
3303 ;; If file does not exist, say it is not modified if and
3304 ;; only if that agrees with the buffer's record.
3305 (t (equal mt '(-1 65535))))))))))
3307 (defun tramp-handle-file-notify-add-watch (filename _flags _callback)
3308 "Like `file-notify-add-watch' for Tramp files."
3309 ;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
3310 ;; their own one.
3311 (setq filename (expand-file-name filename))
3312 (with-parsed-tramp-file-name filename nil
3313 (tramp-error
3314 v 'file-notify-error "File notification not supported for `%s'" filename)))
3316 (defun tramp-handle-file-notify-rm-watch (proc)
3317 "Like `file-notify-rm-watch' for Tramp files."
3318 ;; The descriptor must be a process object.
3319 (unless (processp proc)
3320 (tramp-error proc 'file-notify-error "Not a valid descriptor %S" proc))
3321 (tramp-message proc 6 "Kill %S" proc)
3322 (delete-process proc))
3324 (defun tramp-handle-file-notify-valid-p (proc)
3325 "Like `file-notify-valid-p' for Tramp files."
3326 (and (tramp-compat-process-live-p proc)
3327 ;; Sometimes, the process is still in status `run' when the
3328 ;; file or directory to be watched is deleted already.
3329 (with-current-buffer (process-buffer proc)
3330 (file-exists-p
3331 (concat (file-remote-p default-directory)
3332 (process-get proc 'watch-name))))))
3334 ;;; Functions for establishing connection:
3336 ;; The following functions are actions to be taken when seeing certain
3337 ;; prompts from the remote host. See the variable
3338 ;; `tramp-actions-before-shell' for usage of these functions.
3340 (defun tramp-action-login (_proc vec)
3341 "Send the login name."
3342 (when (not (stringp tramp-current-user))
3343 (setq tramp-current-user
3344 (with-tramp-connection-property vec "login-as"
3345 (save-window-excursion
3346 (let ((enable-recursive-minibuffers t))
3347 (pop-to-buffer (tramp-get-connection-buffer vec))
3348 (read-string (match-string 0)))))))
3349 (with-current-buffer (tramp-get-connection-buffer vec)
3350 (tramp-message vec 6 "\n%s" (buffer-string)))
3351 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
3352 (tramp-send-string vec (concat tramp-current-user tramp-local-end-of-line)))
3354 (defun tramp-action-password (proc vec)
3355 "Query the user for a password."
3356 (with-current-buffer (process-buffer proc)
3357 (let ((enable-recursive-minibuffers t)
3358 (case-fold-search t))
3359 ;; Let's check whether a wrong password has been sent already.
3360 ;; Sometimes, the process returns a new password request
3361 ;; immediately after rejecting the previous (wrong) one.
3362 (unless (tramp-get-connection-property vec "first-password-request" nil)
3363 (tramp-clear-passwd vec))
3364 (goto-char (point-min))
3365 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
3366 (tramp-message vec 3 "Sending %s" (match-string 1))
3367 ;; We don't call `tramp-send-string' in order to hide the
3368 ;; password from the debug buffer.
3369 (process-send-string
3370 proc (concat (tramp-read-passwd proc) tramp-local-end-of-line))
3371 ;; Hide password prompt.
3372 (narrow-to-region (point-max) (point-max)))))
3374 (defun tramp-action-succeed (_proc _vec)
3375 "Signal success in finding shell prompt."
3376 (throw 'tramp-action 'ok))
3378 (defun tramp-action-permission-denied (proc _vec)
3379 "Signal permission denied."
3380 (kill-process proc)
3381 (throw 'tramp-action 'permission-denied))
3383 (defun tramp-action-yesno (proc vec)
3384 "Ask the user for confirmation using `yes-or-no-p'.
3385 Send \"yes\" to remote process on confirmation, abort otherwise.
3386 See also `tramp-action-yn'."
3387 (save-window-excursion
3388 (let ((enable-recursive-minibuffers t))
3389 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
3390 (unless (yes-or-no-p (match-string 0))
3391 (kill-process proc)
3392 (throw 'tramp-action 'permission-denied))
3393 (with-current-buffer (tramp-get-connection-buffer vec)
3394 (tramp-message vec 6 "\n%s" (buffer-string)))
3395 (tramp-send-string vec (concat "yes" tramp-local-end-of-line)))))
3397 (defun tramp-action-yn (proc vec)
3398 "Ask the user for confirmation using `y-or-n-p'.
3399 Send \"y\" to remote process on confirmation, abort otherwise.
3400 See also `tramp-action-yesno'."
3401 (save-window-excursion
3402 (let ((enable-recursive-minibuffers t))
3403 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
3404 (unless (y-or-n-p (match-string 0))
3405 (kill-process proc)
3406 (throw 'tramp-action 'permission-denied))
3407 (with-current-buffer (tramp-get-connection-buffer vec)
3408 (tramp-message vec 6 "\n%s" (buffer-string)))
3409 (tramp-send-string vec (concat "y" tramp-local-end-of-line)))))
3411 (defun tramp-action-terminal (_proc vec)
3412 "Tell the remote host which terminal type to use.
3413 The terminal type can be configured with `tramp-terminal-type'."
3414 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
3415 (with-current-buffer (tramp-get-connection-buffer vec)
3416 (tramp-message vec 6 "\n%s" (buffer-string)))
3417 (tramp-send-string vec (concat tramp-terminal-type tramp-local-end-of-line)))
3419 (defun tramp-action-process-alive (proc _vec)
3420 "Check, whether a process has finished."
3421 (unless (tramp-compat-process-live-p proc)
3422 (throw 'tramp-action 'process-died)))
3424 (defun tramp-action-out-of-band (proc vec)
3425 "Check, whether an out-of-band copy has finished."
3426 ;; There might be pending output for the exit status.
3427 (tramp-accept-process-output proc 0.1)
3428 (cond ((and (not (tramp-compat-process-live-p proc))
3429 (zerop (process-exit-status proc)))
3430 (tramp-message vec 3 "Process has finished.")
3431 (throw 'tramp-action 'ok))
3432 ((or (and (memq (process-status proc) '(stop exit))
3433 (not (zerop (process-exit-status proc))))
3434 (memq (process-status proc) '(signal)))
3435 ;; `scp' could have copied correctly, but set modes could have failed.
3436 ;; This can be ignored.
3437 (with-current-buffer (process-buffer proc)
3438 (goto-char (point-min))
3439 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
3440 (progn
3441 (tramp-message vec 5 "'set mode' error ignored.")
3442 (tramp-message vec 3 "Process has finished.")
3443 (throw 'tramp-action 'ok))
3444 (tramp-message vec 3 "Process has died.")
3445 (throw 'tramp-action 'out-of-band-failed))))
3446 (t nil)))
3448 ;;; Functions for processing the actions:
3450 (defun tramp-process-one-action (proc vec actions)
3451 "Wait for output from the shell and perform one action."
3452 (let ((case-fold-search t)
3453 found todo item pattern action)
3454 (while (not found)
3455 ;; Reread output once all actions have been performed.
3456 ;; Obviously, the output was not complete.
3457 (tramp-accept-process-output proc 1)
3458 (setq todo actions)
3459 (while todo
3460 (setq item (pop todo))
3461 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
3462 (setq action (nth 1 item))
3463 (tramp-message
3464 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
3465 (when (tramp-check-for-regexp proc pattern)
3466 (tramp-message vec 5 "Call `%s'" (symbol-name action))
3467 (setq found (funcall action proc vec)))))
3468 found))
3470 (defun tramp-process-actions (proc vec pos actions &optional timeout)
3471 "Perform ACTIONS until success or TIMEOUT.
3472 PROC and VEC indicate the remote connection to be used. POS, if
3473 set, is the starting point of the region to be deleted in the
3474 connection buffer."
3475 ;; Enable `auth-source'. We must use tramp-current-* variables in
3476 ;; case we have several hops.
3477 (tramp-set-connection-property
3478 (tramp-dissect-file-name
3479 (tramp-make-tramp-file-name
3480 tramp-current-method tramp-current-user tramp-current-host ""))
3481 "first-password-request" t)
3482 (save-restriction
3483 (with-tramp-progress-reporter
3484 proc 3 "Waiting for prompts from remote shell"
3485 (let (exit)
3486 (if timeout
3487 (with-timeout (timeout (setq exit 'timeout))
3488 (while (not exit)
3489 (setq exit
3490 (catch 'tramp-action
3491 (tramp-process-one-action proc vec actions)))))
3492 (while (not exit)
3493 (setq exit
3494 (catch 'tramp-action
3495 (tramp-process-one-action proc vec actions)))))
3496 (with-current-buffer (tramp-get-connection-buffer vec)
3497 (widen)
3498 (tramp-message vec 6 "\n%s" (buffer-string)))
3499 (unless (eq exit 'ok)
3500 (tramp-clear-passwd vec)
3501 (delete-process proc)
3502 (tramp-error-with-buffer
3503 (tramp-get-connection-buffer vec) vec 'file-error
3504 (cond
3505 ((eq exit 'permission-denied) "Permission denied")
3506 ((eq exit 'out-of-band-failed)
3507 (format-message
3508 "Copy failed, see buffer `%s' for details"
3509 (tramp-get-connection-buffer vec)))
3510 ((eq exit 'process-died)
3511 (substitute-command-keys
3512 (concat
3513 "Tramp failed to connect. If this happens repeatedly, try\n"
3514 " `\\[tramp-cleanup-this-connection]'")))
3515 ((eq exit 'timeout)
3516 (format-message
3517 "Timeout reached, see buffer `%s' for details"
3518 (tramp-get-connection-buffer vec)))
3519 (t "Login failed")))))
3520 (when (numberp pos)
3521 (with-current-buffer (tramp-get-connection-buffer vec)
3522 (let (buffer-read-only) (delete-region pos (point))))))))
3524 ;;; Utility functions:
3526 (defun tramp-accept-process-output (proc timeout)
3527 "Like `accept-process-output' for Tramp processes.
3528 This is needed in order to hide `last-coding-system-used', which is set
3529 for process communication also."
3530 ;; FIXME: There are problems, when an asynchronous process runs in
3531 ;; parallel, and also timers are active. See
3532 ;; <http://lists.gnu.org/archive/html/tramp-devel/2017-01/msg00010.html>.
3533 (when (and timer-event-last
3534 (string-prefix-p "*tramp/" (process-name proc))
3535 (let (result)
3536 (maphash
3537 (lambda (key _value)
3538 (and (processp key)
3539 (not (string-prefix-p "*tramp/" (process-name key)))
3540 (tramp-compat-process-live-p key)
3541 (setq result t)))
3542 tramp-cache-data)
3543 result))
3544 (sit-for 0.01 'nodisp))
3545 (with-current-buffer (process-buffer proc)
3546 (let (buffer-read-only last-coding-system-used)
3547 ;; Under Windows XP, accept-process-output doesn't return
3548 ;; sometimes. So we add an additional timeout. JUST-THIS-ONE
3549 ;; is set due to Bug#12145.
3550 (tramp-message
3551 proc 10 "%s %s %s\n%s"
3552 proc (process-status proc)
3553 (with-timeout (timeout)
3554 (accept-process-output proc timeout nil t))
3555 (buffer-string)))))
3557 (defun tramp-check-for-regexp (proc regexp)
3558 "Check, whether REGEXP is contained in process buffer of PROC.
3559 Erase echoed commands if exists."
3560 (with-current-buffer (process-buffer proc)
3561 (goto-char (point-min))
3563 ;; Check whether we need to remove echo output.
3564 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
3565 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
3566 (let ((begin (match-beginning 0)))
3567 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
3568 ;; Discard echo from remote output.
3569 (tramp-set-connection-property proc "check-remote-echo" nil)
3570 (tramp-message proc 5 "echo-mark found")
3571 (forward-line 1)
3572 (delete-region begin (point))
3573 (goto-char (point-min)))))
3575 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
3576 ;; Sometimes, the echo string is suppressed on the remote side.
3577 (not (string-equal
3578 (substring-no-properties
3579 tramp-echo-mark-marker
3580 0 (min tramp-echo-mark-marker-length (1- (point-max))))
3581 (buffer-substring-no-properties
3582 (point-min)
3583 (min (+ (point-min) tramp-echo-mark-marker-length)
3584 (point-max))))))
3585 ;; No echo to be handled, now we can look for the regexp.
3586 ;; Sometimes, lines are much to long, and we run into a "Stack
3587 ;; overflow in regexp matcher". For example, //DIRED// lines of
3588 ;; directory listings with some thousand files. Therefore, we
3589 ;; look from the end.
3590 (goto-char (point-max))
3591 (ignore-errors (re-search-backward regexp nil t)))))
3593 (defun tramp-wait-for-regexp (proc timeout regexp)
3594 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3595 Expects the output of PROC to be sent to the current buffer. Returns
3596 the string that matched, or nil. Waits indefinitely if TIMEOUT is
3597 nil."
3598 (with-current-buffer (process-buffer proc)
3599 (let ((found (tramp-check-for-regexp proc regexp)))
3600 (cond (timeout
3601 (with-timeout (timeout)
3602 (while (not found)
3603 (tramp-accept-process-output proc 1)
3604 (unless (tramp-compat-process-live-p proc)
3605 (tramp-error-with-buffer
3606 nil proc 'file-error "Process has died"))
3607 (setq found (tramp-check-for-regexp proc regexp)))))
3609 (while (not found)
3610 (tramp-accept-process-output proc 1)
3611 (unless (tramp-compat-process-live-p proc)
3612 (tramp-error-with-buffer
3613 nil proc 'file-error "Process has died"))
3614 (setq found (tramp-check-for-regexp proc regexp)))))
3615 (tramp-message proc 6 "\n%s" (buffer-string))
3616 (when (not found)
3617 (if timeout
3618 (tramp-error
3619 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
3620 regexp timeout)
3621 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
3622 found)))
3624 ;; It seems that Tru64 Unix does not like it if long strings are sent
3625 ;; to it in one go. (This happens when sending the Perl
3626 ;; `file-attributes' implementation, for instance.) Therefore, we
3627 ;; have this function which sends the string in chunks.
3628 (defun tramp-send-string (vec string)
3629 "Send the STRING via connection VEC.
3631 The STRING is expected to use Unix line-endings, but the lines sent to
3632 the remote host use line-endings as defined in the variable
3633 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
3634 (let* ((p (tramp-get-connection-process vec))
3635 (chunksize (tramp-get-connection-property p "chunksize" nil)))
3636 (unless p
3637 (tramp-error
3638 vec 'file-error "Can't send string to remote host -- not logged in"))
3639 (tramp-set-connection-property p "last-cmd-time" (current-time))
3640 (tramp-message vec 10 "%s" string)
3641 (with-current-buffer (tramp-get-connection-buffer vec)
3642 ;; Clean up the buffer. We cannot call `erase-buffer' because
3643 ;; narrowing might be in effect.
3644 (let (buffer-read-only) (delete-region (point-min) (point-max)))
3645 ;; Replace "\n" by `tramp-rsh-end-of-line'.
3646 (setq string
3647 (mapconcat
3648 'identity (split-string string "\n") tramp-rsh-end-of-line))
3649 (unless (or (string= string "")
3650 (string-equal (substring string -1) tramp-rsh-end-of-line))
3651 (setq string (concat string tramp-rsh-end-of-line)))
3652 ;; Send the string.
3653 (if (and chunksize (not (zerop chunksize)))
3654 (let ((pos 0)
3655 (end (length string)))
3656 (while (< pos end)
3657 (tramp-message
3658 vec 10 "Sending chunk from %s to %s"
3659 pos (min (+ pos chunksize) end))
3660 (process-send-string
3661 p (substring string pos (min (+ pos chunksize) end)))
3662 (setq pos (+ pos chunksize))))
3663 (process-send-string p string)))))
3665 (defun tramp-get-inode (vec)
3666 "Returns the virtual inode number.
3667 If it doesn't exist, generate a new one."
3668 (with-tramp-file-property vec (tramp-file-name-localname vec) "inode"
3669 (setq tramp-inodes (1+ tramp-inodes))))
3671 (defun tramp-get-device (vec)
3672 "Returns the virtual device number.
3673 If it doesn't exist, generate a new one."
3674 (with-tramp-connection-property (tramp-get-connection-process vec) "device"
3675 (cons -1 (setq tramp-devices (1+ tramp-devices)))))
3677 (defun tramp-equal-remote (file1 file2)
3678 "Check, whether the remote parts of FILE1 and FILE2 are identical.
3679 The check depends on method, user and host name of the files. If
3680 one of the components is missing, the default values are used.
3681 The local file name parts of FILE1 and FILE2 are not taken into
3682 account.
3684 Example:
3686 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
3688 would yield t. On the other hand, the following check results in nil:
3690 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
3691 (and (tramp-tramp-file-p file1)
3692 (tramp-tramp-file-p file2)
3693 (string-equal (file-remote-p file1) (file-remote-p file2))))
3695 ;;;###tramp-autoload
3696 (defun tramp-mode-string-to-int (mode-string)
3697 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
3698 (let* (case-fold-search
3699 (mode-chars (string-to-vector mode-string))
3700 (owner-read (aref mode-chars 1))
3701 (owner-write (aref mode-chars 2))
3702 (owner-execute-or-setid (aref mode-chars 3))
3703 (group-read (aref mode-chars 4))
3704 (group-write (aref mode-chars 5))
3705 (group-execute-or-setid (aref mode-chars 6))
3706 (other-read (aref mode-chars 7))
3707 (other-write (aref mode-chars 8))
3708 (other-execute-or-sticky (aref mode-chars 9)))
3709 (save-match-data
3710 (logior
3711 (cond
3712 ((char-equal owner-read ?r) (string-to-number "00400" 8))
3713 ((char-equal owner-read ?-) 0)
3714 (t (error "Second char `%c' must be one of `r-'" owner-read)))
3715 (cond
3716 ((char-equal owner-write ?w) (string-to-number "00200" 8))
3717 ((char-equal owner-write ?-) 0)
3718 (t (error "Third char `%c' must be one of `w-'" owner-write)))
3719 (cond
3720 ((char-equal owner-execute-or-setid ?x) (string-to-number "00100" 8))
3721 ((char-equal owner-execute-or-setid ?S) (string-to-number "04000" 8))
3722 ((char-equal owner-execute-or-setid ?s) (string-to-number "04100" 8))
3723 ((char-equal owner-execute-or-setid ?-) 0)
3724 (t (error "Fourth char `%c' must be one of `xsS-'"
3725 owner-execute-or-setid)))
3726 (cond
3727 ((char-equal group-read ?r) (string-to-number "00040" 8))
3728 ((char-equal group-read ?-) 0)
3729 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
3730 (cond
3731 ((char-equal group-write ?w) (string-to-number "00020" 8))
3732 ((char-equal group-write ?-) 0)
3733 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
3734 (cond
3735 ((char-equal group-execute-or-setid ?x) (string-to-number "00010" 8))
3736 ((char-equal group-execute-or-setid ?S) (string-to-number "02000" 8))
3737 ((char-equal group-execute-or-setid ?s) (string-to-number "02010" 8))
3738 ((char-equal group-execute-or-setid ?-) 0)
3739 (t (error "Seventh char `%c' must be one of `xsS-'"
3740 group-execute-or-setid)))
3741 (cond
3742 ((char-equal other-read ?r) (string-to-number "00004" 8))
3743 ((char-equal other-read ?-) 0)
3744 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
3745 (cond
3746 ((char-equal other-write ?w) (string-to-number "00002" 8))
3747 ((char-equal other-write ?-) 0)
3748 (t (error "Ninth char `%c' must be one of `w-'" other-write)))
3749 (cond
3750 ((char-equal other-execute-or-sticky ?x) (string-to-number "00001" 8))
3751 ((char-equal other-execute-or-sticky ?T) (string-to-number "01000" 8))
3752 ((char-equal other-execute-or-sticky ?t) (string-to-number "01001" 8))
3753 ((char-equal other-execute-or-sticky ?-) 0)
3754 (t (error "Tenth char `%c' must be one of `xtT-'"
3755 other-execute-or-sticky)))))))
3757 (defconst tramp-file-mode-type-map
3758 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
3759 (1 . "p") ; fifo
3760 (2 . "c") ; character device
3761 (3 . "m") ; multiplexed character device (v7)
3762 (4 . "d") ; directory
3763 (5 . "?") ; Named special file (XENIX)
3764 (6 . "b") ; block device
3765 (7 . "?") ; multiplexed block device (v7)
3766 (8 . "-") ; regular file
3767 (9 . "n") ; network special file (HP-UX)
3768 (10 . "l") ; symlink
3769 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
3770 (12 . "s") ; socket
3771 (13 . "D") ; door special (Solaris)
3772 (14 . "w")) ; whiteout (BSD)
3773 "A list of file types returned from the `stat' system call.
3774 This is used to map a mode number to a permission string.")
3776 ;;;###tramp-autoload
3777 (defun tramp-file-mode-from-int (mode)
3778 "Turn an integer representing a file mode into an ls(1)-like string."
3779 (let ((type (cdr
3780 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
3781 (user (logand (lsh mode -6) 7))
3782 (group (logand (lsh mode -3) 7))
3783 (other (logand (lsh mode -0) 7))
3784 (suid (> (logand (lsh mode -9) 4) 0))
3785 (sgid (> (logand (lsh mode -9) 2) 0))
3786 (sticky (> (logand (lsh mode -9) 1) 0)))
3787 (setq user (tramp-file-mode-permissions user suid "s"))
3788 (setq group (tramp-file-mode-permissions group sgid "s"))
3789 (setq other (tramp-file-mode-permissions other sticky "t"))
3790 (concat type user group other)))
3792 (defun tramp-file-mode-permissions (perm suid suid-text)
3793 "Convert a permission bitset into a string.
3794 This is used internally by `tramp-file-mode-from-int'."
3795 (let ((r (> (logand perm 4) 0))
3796 (w (> (logand perm 2) 0))
3797 (x (> (logand perm 1) 0)))
3798 (concat (or (and r "r") "-")
3799 (or (and w "w") "-")
3800 (or (and suid x suid-text) ; suid, execute
3801 (and suid (upcase suid-text)) ; suid, !execute
3802 (and x "x") "-")))) ; !suid
3804 ;;;###tramp-autoload
3805 (defun tramp-get-local-uid (id-format)
3806 "The uid of the local user, in ID-FORMAT.
3807 ID-FORMAT valid values are `string' and `integer'."
3808 (if (equal id-format 'integer) (user-uid) (user-login-name)))
3810 ;;;###tramp-autoload
3811 (defun tramp-get-local-gid (id-format)
3812 "The gid of the local user, in ID-FORMAT.
3813 ID-FORMAT valid values are `string' and `integer'."
3814 ;; `group-gid' has been introduced with Emacs 24.4.
3815 (if (and (fboundp 'group-gid) (equal id-format 'integer))
3816 (tramp-compat-funcall 'group-gid)
3817 (tramp-compat-file-attribute-group-id (file-attributes "~/" id-format))))
3819 (defun tramp-get-local-locale (&optional vec)
3820 "Determine locale, supporting UTF8 if possible.
3821 VEC is used for tracing."
3822 ;; We use key nil for local connection properties.
3823 (with-tramp-connection-property nil "locale"
3824 (let ((candidates '("en_US.utf8" "C.utf8" "en_US.UTF-8"))
3825 locale)
3826 (with-temp-buffer
3827 (unless (or (memq system-type '(windows-nt))
3828 (not (zerop (tramp-call-process
3829 nil "locale" nil t nil "-a"))))
3830 (while candidates
3831 (goto-char (point-min))
3832 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates)))
3833 (buffer-string))
3834 (setq locale (car candidates)
3835 candidates nil)
3836 (setq candidates (cdr candidates))))))
3837 ;; Return value.
3838 (when vec (tramp-message vec 7 "locale %s" (or locale "C")))
3839 (or locale "C"))))
3841 ;;;###tramp-autoload
3842 (defun tramp-check-cached-permissions (vec access)
3843 "Check `file-attributes' caches for VEC.
3844 Return t if according to the cache access type ACCESS is known to
3845 be granted."
3846 (let ((result nil)
3847 (offset (cond
3848 ((eq ?r access) 1)
3849 ((eq ?w access) 2)
3850 ((eq ?x access) 3))))
3851 (dolist (suffix '("string" "integer") result)
3852 (setq
3853 result
3855 result
3856 (let ((file-attr
3858 (tramp-get-file-property
3859 vec (tramp-file-name-localname vec)
3860 (concat "file-attributes-" suffix) nil)
3861 (file-attributes
3862 (tramp-make-tramp-file-name
3863 (tramp-file-name-method vec)
3864 (tramp-file-name-user vec)
3865 (tramp-file-name-host vec)
3866 (tramp-file-name-localname vec)
3867 (tramp-file-name-hop vec))
3868 (intern suffix))))
3869 (remote-uid
3870 (tramp-get-connection-property
3871 vec (concat "uid-" suffix) nil))
3872 (remote-gid
3873 (tramp-get-connection-property
3874 vec (concat "gid-" suffix) nil))
3875 (unknown-id
3876 (if (string-equal suffix "string")
3877 tramp-unknown-id-string tramp-unknown-id-integer)))
3878 (and
3879 file-attr
3881 ;; Not a symlink.
3882 (eq t (tramp-compat-file-attribute-type file-attr))
3883 (null (tramp-compat-file-attribute-type file-attr)))
3885 ;; World accessible.
3886 (eq access
3887 (aref (tramp-compat-file-attribute-modes file-attr)
3888 (+ offset 6)))
3889 ;; User accessible and owned by user.
3890 (and
3891 (eq access
3892 (aref (tramp-compat-file-attribute-modes file-attr) offset))
3893 (or (equal remote-uid
3894 (tramp-compat-file-attribute-user-id file-attr))
3895 (equal unknown-id
3896 (tramp-compat-file-attribute-user-id file-attr))))
3897 ;; Group accessible and owned by user's principal group.
3898 (and
3899 (eq access
3900 (aref (tramp-compat-file-attribute-modes file-attr)
3901 (+ offset 3)))
3902 (or (equal remote-gid
3903 (tramp-compat-file-attribute-group-id file-attr))
3904 (equal unknown-id
3905 (tramp-compat-file-attribute-group-id
3906 file-attr))))))))))))
3908 ;;;###tramp-autoload
3909 (defun tramp-local-host-p (vec)
3910 "Return t if this points to the local host, nil otherwise."
3911 ;; We cannot use `tramp-file-name-real-host'. A port is an
3912 ;; indication for an ssh tunnel or alike.
3913 (let ((host (tramp-file-name-host vec)))
3914 (and
3915 (stringp host)
3916 (string-match tramp-local-host-regexp host)
3917 ;; The method shall be applied to one of the shell file name
3918 ;; handlers. `tramp-local-host-p' is also called for "smb" and
3919 ;; alike, where it must fail.
3920 (tramp-get-method-parameter vec 'tramp-login-program)
3921 ;; The local temp directory must be writable for the other user.
3922 (file-writable-p
3923 (tramp-make-tramp-file-name
3924 (tramp-file-name-method vec)
3925 (tramp-file-name-user vec)
3926 host
3927 (tramp-compat-temporary-file-directory)))
3928 ;; On some systems, chown runs only for root.
3929 (or (zerop (user-uid))
3930 ;; This is defined in tramp-sh.el. Let's assume this is
3931 ;; loaded already.
3932 (zerop (tramp-compat-funcall 'tramp-get-remote-uid vec 'integer))))))
3934 (defun tramp-get-remote-tmpdir (vec)
3935 "Return directory for temporary files on the remote host identified by VEC."
3936 (with-tramp-connection-property vec "tmpdir"
3937 (let ((dir (tramp-make-tramp-file-name
3938 (tramp-file-name-method vec)
3939 (tramp-file-name-user vec)
3940 (tramp-file-name-host vec)
3941 (or (tramp-get-method-parameter vec 'tramp-tmpdir) "/tmp")
3942 (tramp-file-name-hop vec))))
3943 (or (and (file-directory-p dir) (file-writable-p dir)
3944 (file-remote-p dir 'localname))
3945 (tramp-error vec 'file-error "Directory %s not accessible" dir))
3946 dir)))
3948 ;;;###tramp-autoload
3949 (defun tramp-make-tramp-temp-file (vec)
3950 "Create a temporary file on the remote host identified by VEC.
3951 Return the local name of the temporary file."
3952 (let ((prefix (expand-file-name
3953 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))
3954 result)
3955 (while (not result)
3956 ;; `make-temp-file' would be the natural choice for
3957 ;; implementation. But it calls `write-region' internally,
3958 ;; which also needs a temporary file - we would end in an
3959 ;; infinite loop.
3960 (setq result (make-temp-name prefix))
3961 (if (file-exists-p result)
3962 (setq result nil)
3963 ;; This creates the file by side effect.
3964 (set-file-times result)
3965 (set-file-modes result (string-to-number "0700" 8))))
3967 ;; Return the local part.
3968 (with-parsed-tramp-file-name result nil localname)))
3970 (defun tramp-delete-temp-file-function ()
3971 "Remove temporary files related to current buffer."
3972 (when (stringp tramp-temp-buffer-file-name)
3973 (ignore-errors (delete-file tramp-temp-buffer-file-name))))
3975 (add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
3976 (add-hook 'tramp-unload-hook
3977 (lambda ()
3978 (remove-hook 'kill-buffer-hook
3979 'tramp-delete-temp-file-function)))
3981 (defun tramp-handle-make-auto-save-file-name ()
3982 "Like `make-auto-save-file-name' for Tramp files.
3983 Returns a file name in `tramp-auto-save-directory' for autosaving
3984 this file, if that variable is non-nil."
3985 (when (stringp tramp-auto-save-directory)
3986 (setq tramp-auto-save-directory
3987 (expand-file-name tramp-auto-save-directory)))
3988 ;; Create directory.
3989 (unless (or (null tramp-auto-save-directory)
3990 (file-exists-p tramp-auto-save-directory))
3991 (make-directory tramp-auto-save-directory t))
3993 (let ((system-type
3994 (if (and (stringp tramp-auto-save-directory)
3995 (file-remote-p tramp-auto-save-directory))
3996 'not-windows
3997 system-type))
3998 (auto-save-file-name-transforms
3999 (if (null tramp-auto-save-directory)
4000 auto-save-file-name-transforms))
4001 (buffer-file-name
4002 (if (null tramp-auto-save-directory)
4003 buffer-file-name
4004 (expand-file-name
4005 (tramp-subst-strs-in-string
4006 '(("_" . "|")
4007 ("/" . "_a")
4008 (":" . "_b")
4009 ("|" . "__")
4010 ("[" . "_l")
4011 ("]" . "_r"))
4012 (tramp-compat-file-name-unquote (buffer-file-name)))
4013 tramp-auto-save-directory))))
4014 ;; Run plain `make-auto-save-file-name'.
4015 (tramp-run-real-handler 'make-auto-save-file-name nil)))
4017 (defun tramp-subst-strs-in-string (alist string)
4018 "Replace all occurrences of the string FROM with TO in STRING.
4019 ALIST is of the form ((FROM . TO) ...)."
4020 (save-match-data
4021 (while alist
4022 (let* ((pr (car alist))
4023 (from (car pr))
4024 (to (cdr pr)))
4025 (while (string-match (regexp-quote from) string)
4026 (setq string (replace-match to t t string)))
4027 (setq alist (cdr alist))))
4028 string))
4030 (defun tramp-handle-temporary-file-directory ()
4031 "Like `temporary-file-directory' for Tramp files."
4032 (catch 'result
4033 (dolist (dir `(,(ignore-errors
4034 (tramp-get-remote-tmpdir
4035 (tramp-dissect-file-name default-directory)))
4036 ,default-directory))
4037 (when (and (stringp dir) (file-directory-p dir) (file-writable-p dir))
4038 (throw 'result (expand-file-name dir))))))
4040 (defun tramp-handle-make-nearby-temp-file (prefix &optional dir-flag suffix)
4041 "Like `make-nearby-temp-file' for Tramp files."
4042 (let ((temporary-file-directory
4043 (tramp-compat-temporary-file-directory-function)))
4044 (make-temp-file prefix dir-flag suffix)))
4046 ;;; Compatibility functions section:
4048 (defun tramp-call-process
4049 (vec program &optional infile destination display &rest args)
4050 "Calls `call-process' on the local host.
4051 It always returns a return code. The Lisp error raised when
4052 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4053 are written with verbosity of 6."
4054 (let ((default-directory (tramp-compat-temporary-file-directory))
4055 (v (or vec
4056 (vector tramp-current-method tramp-current-user
4057 tramp-current-host nil nil)))
4058 (destination (if (eq destination t) (current-buffer) destination))
4059 output error result)
4060 (tramp-message
4061 v 6 "`%s %s' %s %s"
4062 program (mapconcat 'identity args " ") infile destination)
4063 (condition-case err
4064 (with-temp-buffer
4065 (setq result
4066 (apply
4067 'call-process program infile (or destination t) display args))
4068 ;; `result' could also be an error string.
4069 (when (stringp result)
4070 (setq error result
4071 result 1))
4072 (with-current-buffer
4073 (if (bufferp destination) destination (current-buffer))
4074 (setq output (buffer-string))))
4075 (error
4076 (setq error (error-message-string err)
4077 result 1)))
4078 (if (zerop (length error))
4079 (tramp-message v 6 "%d\n%s" result output)
4080 (tramp-message v 6 "%d\n%s\n%s" result output error))
4081 result))
4083 (defun tramp-call-process-region
4084 (vec start end program &optional delete buffer display &rest args)
4085 "Calls `call-process-region' on the local host.
4086 It always returns a return code. The Lisp error raised when
4087 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4088 are written with verbosity of 6."
4089 (let ((default-directory (tramp-compat-temporary-file-directory))
4090 (v (or vec
4091 (vector tramp-current-method tramp-current-user
4092 tramp-current-host nil nil)))
4093 (buffer (if (eq buffer t) (current-buffer) buffer))
4094 result)
4095 (tramp-message
4096 v 6 "`%s %s' %s %s %s %s"
4097 program (mapconcat 'identity args " ") start end delete buffer)
4098 (condition-case err
4099 (progn
4100 (setq result
4101 (apply
4102 'call-process-region
4103 start end program delete buffer display args))
4104 ;; `result' could also be an error string.
4105 (when (stringp result)
4106 (signal 'file-error (list result)))
4107 (with-current-buffer (if (bufferp buffer) buffer (current-buffer))
4108 (if (zerop result)
4109 (tramp-message v 6 "%d" result)
4110 (tramp-message v 6 "%d\n%s" result (buffer-string)))))
4111 (error
4112 (setq result 1)
4113 (tramp-message v 6 "%d\n%s" result (error-message-string err))))
4114 result))
4116 ;;;###tramp-autoload
4117 (defun tramp-read-passwd (proc &optional prompt)
4118 "Read a password from user (compat function).
4119 Consults the auth-source package.
4120 Invokes `password-read' if available, `read-passwd' else."
4121 (let* ((case-fold-search t)
4122 (key (tramp-make-tramp-file-name
4123 tramp-current-method tramp-current-user
4124 tramp-current-host ""))
4125 (pw-prompt
4126 (or prompt
4127 (with-current-buffer (process-buffer proc)
4128 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
4129 (format "%s for %s " (capitalize (match-string 1)) key))))
4130 ;; We suspend the timers while reading the password.
4131 (stimers (with-timeout-suspend))
4132 auth-info auth-passwd)
4134 (unwind-protect
4135 (with-parsed-tramp-file-name key nil
4136 (prog1
4138 ;; See if auth-sources contains something useful.
4139 ;; `auth-source-user-or-password' is an obsoleted
4140 ;; function since Emacs 24.1, it has been replaced by
4141 ;; `auth-source-search'.
4142 (ignore-errors
4143 (and (tramp-get-connection-property
4144 v "first-password-request" nil)
4145 ;; Try with Tramp's current method.
4146 (if (fboundp 'auth-source-search)
4147 (setq auth-info
4148 (auth-source-search
4149 :max 1
4150 (and tramp-current-user :user)
4151 tramp-current-user
4152 :host tramp-current-host
4153 :port tramp-current-method
4154 :require
4155 (cons
4156 :secret (and tramp-current-user '(:user))))
4157 auth-passwd (plist-get
4158 (nth 0 auth-info) :secret)
4159 auth-passwd (if (functionp auth-passwd)
4160 (funcall auth-passwd)
4161 auth-passwd))
4162 (tramp-compat-funcall
4163 'auth-source-user-or-password
4164 "password" tramp-current-host tramp-current-method))))
4165 ;; Try the password cache.
4166 (let ((password (password-read pw-prompt key)))
4167 (password-cache-add key password)
4168 password)
4169 ;; Else, get the password interactively.
4170 (read-passwd pw-prompt))
4171 (tramp-set-connection-property v "first-password-request" nil)))
4172 ;; Reenable the timers.
4173 (with-timeout-unsuspend stimers))))
4175 ;;;###tramp-autoload
4176 (defun tramp-clear-passwd (vec)
4177 "Clear password cache for connection related to VEC."
4178 (let ((method (tramp-file-name-method vec))
4179 (user (tramp-file-name-user vec))
4180 (host (tramp-file-name-host vec))
4181 (hop (tramp-file-name-hop vec)))
4182 (when hop
4183 ;; Clear also the passwords of the hops.
4184 (tramp-clear-passwd
4185 (tramp-dissect-file-name
4186 (concat
4187 tramp-prefix-format
4188 (replace-regexp-in-string
4189 (concat tramp-postfix-hop-regexp "$")
4190 tramp-postfix-host-format hop)))))
4191 ;; `auth-source-forget-user-or-password' is an obsoleted function
4192 ;; since Emacs 24.1, it has been replaced by `auth-source-forget'.
4193 (if (fboundp 'auth-source-forget)
4194 (auth-source-forget
4195 `(:max 1 ,(and user :user) ,user :host ,host :port ,method))
4196 (tramp-compat-funcall
4197 'auth-source-forget-user-or-password "password" host method))
4198 (password-cache-remove (tramp-make-tramp-file-name method user host ""))))
4200 ;; Snarfed code from time-date.el.
4202 (defconst tramp-half-a-year '(241 17024)
4203 "Evaluated by \"(days-to-time 183)\".")
4205 ;;;###tramp-autoload
4206 (defun tramp-time-diff (t1 t2)
4207 "Return the difference between the two times, in seconds.
4208 T1 and T2 are time values (as returned by `current-time' for example)."
4209 ;; Starting with Emacs 25.1, we could change this to use `time-subtract'.
4210 (float-time (tramp-compat-funcall 'subtract-time t1 t2)))
4212 (defun tramp-unquote-shell-quote-argument (s)
4213 "Remove quotation prefix \"/:\" from string S, and quote it then for shell."
4214 (shell-quote-argument (tramp-compat-file-name-unquote s)))
4216 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
4217 ;; does not deal well with newline characters. Newline is replaced by
4218 ;; backslash newline. But if, say, the string `a backslash newline b'
4219 ;; is passed to a shell, the shell will expand this into "ab",
4220 ;; completely omitting the newline. This is not what was intended.
4221 ;; It does not appear to be possible to make the function
4222 ;; `shell-quote-argument' work with newlines without making it
4223 ;; dependent on the shell used. But within this package, we know that
4224 ;; we will always use a Bourne-like shell, so we use an approach which
4225 ;; groks newlines.
4227 ;; The approach is simple: we call `shell-quote-argument', then
4228 ;; massage the newline part of the result.
4230 ;; This function should produce a string which is grokked by a Unix
4231 ;; shell, even if the Emacs is running on Windows. Since this is the
4232 ;; kludges section, we bind `system-type' in such a way that
4233 ;; `shell-quote-argument' behaves as if on Unix.
4235 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
4236 ;; function to work with Bourne-like shells.
4238 ;; CCC: This function should be rewritten so that
4239 ;; `shell-quote-argument' is not used. This way, we are safe from
4240 ;; changes in `shell-quote-argument'.
4241 ;;;###tramp-autoload
4242 (defun tramp-shell-quote-argument (s)
4243 "Similar to `shell-quote-argument', but groks newlines.
4244 Only works for Bourne-like shells."
4245 (let ((system-type 'not-windows))
4246 (save-match-data
4247 (let ((result (tramp-unquote-shell-quote-argument s))
4248 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
4249 (when (and (>= (length result) 2)
4250 (string= (substring result 0 2) "\\~"))
4251 (setq result (substring result 1)))
4252 (while (string-match nl result)
4253 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
4254 t t result)))
4255 result))))
4257 ;;; Integration of eshell.el:
4259 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
4260 ;; when `default-directory' points to another host.
4261 (defun tramp-eshell-directory-change ()
4262 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
4263 (setq eshell-path-env
4264 (if (tramp-tramp-file-p default-directory)
4265 (with-parsed-tramp-file-name default-directory nil
4266 (mapconcat
4267 'identity
4269 ;; When `tramp-own-remote-path' is in `tramp-remote-path',
4270 ;; the remote path is only set in the session cache.
4271 (tramp-get-connection-property
4272 (tramp-get-connection-process v) "remote-path" nil)
4273 (tramp-get-connection-property v "remote-path" nil))
4274 ":"))
4275 (getenv "PATH"))))
4277 (eval-after-load "esh-util"
4278 '(progn
4279 (add-hook 'eshell-mode-hook
4280 'tramp-eshell-directory-change)
4281 (add-hook 'eshell-directory-change-hook
4282 'tramp-eshell-directory-change)
4283 (add-hook 'tramp-unload-hook
4284 (lambda ()
4285 (remove-hook 'eshell-mode-hook
4286 'tramp-eshell-directory-change)
4287 (remove-hook 'eshell-directory-change-hook
4288 'tramp-eshell-directory-change)))))
4290 ;; Checklist for `tramp-unload-hook'
4291 ;; - Unload all `tramp-*' packages
4292 ;; - Reset `file-name-handler-alist'
4293 ;; - Cleanup hooks where Tramp functions are in
4294 ;; - Cleanup advised functions
4295 ;; - Cleanup autoloads
4296 ;;;###autoload
4297 (defun tramp-unload-tramp ()
4298 "Discard Tramp from loading remote files."
4299 (interactive)
4300 ;; ange-ftp settings must be enabled.
4301 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
4302 ;; Maybe it's not loaded yet.
4303 (ignore-errors (unload-feature 'tramp 'force)))
4305 (provide 'tramp)
4307 ;;; TODO:
4309 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
4310 ;; by the files in that directory. Add this here.
4312 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
4314 ;; * Better error checking. At least whenever we see something
4315 ;; strange when doing zerop, we should kill the process and start
4316 ;; again. (Greg Stark)
4318 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
4320 ;; * I was wondering if it would be possible to use tramp even if I'm
4321 ;; actually using sshfs. But when I launch a command I would like
4322 ;; to get it executed on the remote machine where the files really
4323 ;; are. (Andrea Crotti)
4325 ;; * Run emerge on two remote files. Bug is described here:
4326 ;; <http://www.mail-archive.com/tramp-devel@nongnu.org/msg01041.html>.
4327 ;; (Bug#6850)
4329 ;; * Use also port to distinguish connections. This is needed for
4330 ;; different hosts sitting behind a single router (distinguished by
4331 ;; different port numbers). (Tzvi Edelman)
4333 ;; * Refactor code from different handlers. Start with
4334 ;; *-process-file. One idea is to generalize `tramp-send-command'
4335 ;; and friends, for most of the handlers this is the major
4336 ;; difference between the different backends. Other handlers but
4337 ;; *-process-file would profit from this as well.
4339 ;;; tramp.el ends here
4341 ;; Local Variables:
4342 ;; mode: Emacs-Lisp
4343 ;; coding: utf-8
4344 ;; End: