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