1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998-2018 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
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/>.
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.
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!
58 (require 'tramp-compat
)
61 ;; Pacify byte-compiler.
63 (defvar auto-save-file-name-transforms
)
64 (defvar eshell-path-env
)
65 (defvar ls-lisp-use-insert-directory-program
)
66 (defvar outline-regexp
)
68 ;;; User Customizable Internal Variables:
71 "Edit remote files with a combination of ssh, scp, etc."
74 :link
'(custom-manual "(tramp)Top")
77 ;; Maybe we need once a real Tramp mode, with key bindings etc.
79 (defcustom tramp-mode t
80 "Whether Tramp is enabled.
81 If it is set to nil, all remote file names are used literally."
86 (defcustom tramp-verbose
3
87 "Verbosity level for Tramp messages.
88 Any level x includes messages for all levels 1 .. x-1. The levels are
90 0 silent (no tramp messages at all)
93 3 connection to remote hosts (default level)
96 6 sent and received strings
98 8 connection properties
105 (defcustom tramp-backup-directory-alist nil
106 "Alist of filename patterns and backup directory names.
107 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
108 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
109 is a local file name, the backup directory is prepended with Tramp file
110 name prefix \(method, user, host) of file.
112 \(setq tramp-backup-directory-alist backup-directory-alist)
114 gives the same backup policy for Tramp files on their hosts like the
115 policy for local files."
117 :type
'(repeat (cons (regexp :tag
"Regexp matching filename")
118 (directory :tag
"Backup directory name")))
121 (defcustom tramp-auto-save-directory nil
122 "Put auto-save files in this directory, if set.
123 The idea is to use a local directory so that auto-saving is faster.
124 This setting has precedence over `auto-save-file-name-transforms'."
126 :type
'(choice (const :tag
"Use default" nil
)
127 (directory :tag
"Auto save directory name"))
130 (defcustom tramp-encoding-shell
131 (or (tramp-compat-funcall 'w32-shell-name
) "/bin/sh")
132 "Use this program for encoding and decoding commands on the local host.
133 This shell is used to execute the encoding and decoding command on the
134 local host, so if you want to use `~' in those commands, you should
135 choose a shell here which groks tilde expansion. `/bin/sh' normally
136 does not understand tilde expansion.
138 For encoding and decoding, commands like the following are executed:
140 /bin/sh -c COMMAND < INPUT > OUTPUT
142 This variable can be used to change the \"/bin/sh\" part. See the
143 variable `tramp-encoding-command-switch' for the \"-c\" part.
145 If the shell must be forced to be interactive, see
146 `tramp-encoding-command-interactive'.
148 Note that this variable is not used for remote commands. There are
149 mechanisms in tramp.el which automatically determine the right shell to
150 use for the remote host."
152 :type
'(file :must-match t
)
155 (defcustom tramp-encoding-command-switch
156 (if (tramp-compat-funcall 'w32-shell-dos-semantics
) "/c" "-c")
157 "Use this switch together with `tramp-encoding-shell' for local commands.
158 See the variable `tramp-encoding-shell' for more information."
163 (defcustom tramp-encoding-command-interactive
164 (unless (tramp-compat-funcall 'w32-shell-dos-semantics
) "-i")
165 "Use this switch together with `tramp-encoding-shell' for interactive shells.
166 See the variable `tramp-encoding-shell' for more information."
169 :type
'(choice (const nil
) string
)
173 (defvar tramp-methods nil
174 "Alist of methods for remote files.
175 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
176 Each NAME stands for a remote access method. Each PARAM is a
177 pair of the form (KEY VALUE). The following KEYs are defined:
178 * `tramp-remote-shell'
179 This specifies the shell to use on the remote host. This
180 MUST be a Bourne-like shell. It is normally not necessary to
181 set this to any value other than \"/bin/sh\": Tramp wants to
182 use a shell which groks tilde expansion, but it can search
183 for it. Also note that \"/bin/sh\" exists on all Unixen,
184 this might not be true for the value that you decide to use.
185 You Have Been Warned.
186 * `tramp-remote-shell-login'
187 This specifies the arguments to let `tramp-remote-shell' run
188 as a login shell. It defaults to (\"-l\"), but some shells,
189 like ksh, require another argument. See
190 `tramp-connection-properties' for a way to overwrite the
192 * `tramp-remote-shell-args'
193 For implementation of `shell-command', this specifies the
194 arguments to let `tramp-remote-shell' run a single command.
195 * `tramp-login-program'
196 This specifies the name of the program to use for logging in to the
197 remote host. This may be the name of rsh or a workalike program,
198 or the name of telnet or a workalike, or the name of su or a workalike.
200 This specifies the list of arguments to pass to the above
201 mentioned program. Please note that this is a list of list of arguments,
202 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
203 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
204 There are some patterns: \"%h\" in this list is replaced by the host
205 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
206 port number, and \"%%\" can be used to obtain a literal percent character.
207 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
208 expansion (i.e. no host or no user specified), this list is not used as
209 argument. By this, arguments like (\"-l\" \"%u\") are optional.
210 \"%t\" is replaced by the temporary file name produced with
211 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
212 parameter of a program, if exists. \"%c\" adds additional
213 `tramp-ssh-controlmaster-options' options for the first hop.
215 A list of environment variables and their values, which will
216 be set when calling `tramp-login-program'.
218 When an asynchronous process is started, we know already that
219 the connection works. Therefore, we can pass additional
220 parameters to suppress diagnostic messages, in order not to
221 tamper the process output.
222 * `tramp-copy-program'
223 This specifies the name of the program to use for remotely copying
224 the file; this might be the absolute filename of scp or the name of
225 a workalike program. It is always applied on the local host.
227 This specifies the list of parameters to pass to the above mentioned
228 program, the hints for `tramp-login-args' also apply here.
230 A list of environment variables and their values, which will
231 be set when calling `tramp-copy-program'.
232 * `tramp-remote-copy-program'
233 The listener program to be applied on remote side, if needed.
234 * `tramp-remote-copy-args'
235 The list of parameters to pass to the listener program, the hints
236 for `tramp-login-args' also apply here. Additionally, \"%r\" could
237 be used here and in `tramp-copy-args'. It denotes a randomly
238 chosen port for the remote listener.
239 * `tramp-copy-keep-date'
240 This specifies whether the copying program when the preserves the
241 timestamp of the original file.
242 * `tramp-copy-keep-tmpfile'
243 This specifies whether a temporary local file shall be kept
244 for optimization reasons (useful for \"rsync\" methods).
245 * `tramp-copy-recursive'
246 Whether the operation copies directories recursively.
247 * `tramp-default-port'
248 The default port of a method.
250 A directory on the remote host for temporary files. If not
251 specified, \"/tmp\" is taken as default.
252 * `tramp-connection-timeout'
253 This is the maximum time to be spent for establishing a connection.
254 In general, the global default value shall be used, but for
255 some methods, like \"su\" or \"sudo\", a shorter timeout
257 * `tramp-case-insensitive'
258 Whether the remote file system handles file names case insensitive.
259 Only a non-nil value counts, the default value nil means to
260 perform further checks on the remote host. See
261 `tramp-connection-properties' for a way to overwrite this.
263 What does all this mean? Well, you should specify `tramp-login-program'
264 for all methods; this program is used to log in to the remote site. Then,
265 there are two ways to actually transfer the files between the local and the
266 remote side. One way is using an additional scp-like program. If you want
267 to do this, set `tramp-copy-program' in the method.
269 Another possibility for file transfer is inline transfer, i.e. the
270 file is passed through the same buffer used by `tramp-login-program'. In
271 this case, the file contents need to be protected since the
272 `tramp-login-program' might use escape codes or the connection might not
273 be eight-bit clean. Therefore, file contents are encoded for transit.
274 See the variables `tramp-local-coding-commands' and
275 `tramp-remote-coding-commands' for details.
277 So, to summarize: if the method is an out-of-band method, then you
278 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
279 inline method, then these two parameters should be nil.
283 When using `su' or `sudo' the phrase \"open connection to a remote
284 host\" sounds strange, but it is used nevertheless, for consistency.
285 No connection is opened to a remote host, but `su' or `sudo' is
286 started on the local host. You should specify a remote host
287 `localhost' or the name of the local host. Another host name is
288 useful only in combination with `tramp-default-proxies-alist'.")
290 (defcustom tramp-default-method
291 ;; An external copy method seems to be preferred, because it performs
292 ;; much better for large files, and it hasn't too serious delays
293 ;; for small files. But it must be ensured that there aren't
294 ;; permanent password queries. Either a password agent like
295 ;; "ssh-agent" or "Pageant" shall run, or the optional
296 ;; password-cache.el or auth-sources.el packages shall be active for
297 ;; password caching. If we detect that the user is running OpenSSH
298 ;; 4.0 or newer, we could reuse the connection, which calls also for
299 ;; an external method.
301 ;; PuTTY is installed. We don't take it, if it is installed on a
302 ;; non-windows system, or pscp from the pssh (parallel ssh) package
304 ((and (eq system-type
'windows-nt
) (executable-find "pscp")) "pscp")
305 ;; There is an ssh installation.
306 ((executable-find "scp") "scp")
309 "Default method to use for transferring files.
310 See `tramp-methods' for possibilities.
311 Also see `tramp-default-method-alist'."
317 (defcustom tramp-default-method-alist nil
318 "Default method to use for specific host/user pairs.
319 This is an alist of items (HOST USER METHOD). The first matching item
320 specifies the method to use for a file name which does not specify a
321 method. HOST and USER are regular expressions or nil, which is
322 interpreted as a regular expression which always matches. If no entry
323 matches, the variable `tramp-default-method' takes effect.
325 If the file name does not specify the user, lookup is done using the
326 empty string for the user name.
328 See `tramp-methods' for a list of possibilities for METHOD."
330 :type
'(repeat (list (choice :tag
"Host regexp" regexp sexp
)
331 (choice :tag
"User regexp" regexp sexp
)
332 (choice :tag
"Method name" string
(const nil
))))
335 (defconst tramp-default-method-marker
"-"
336 "Marker for default method in remote file names.")
338 (defcustom tramp-default-user nil
339 "Default user to use for transferring files.
340 It is nil by default; otherwise settings in configuration files like
341 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
343 This variable is regarded as obsolete, and will be removed soon."
345 :type
'(choice (const nil
) string
)
349 (defcustom tramp-default-user-alist nil
350 "Default user to use for specific method/host pairs.
351 This is an alist of items (METHOD HOST USER). The first matching item
352 specifies the user to use for a file name which does not specify a
353 user. METHOD and HOST are regular expressions or nil, which is
354 interpreted as a regular expression which always matches. If no entry
355 matches, the variable `tramp-default-user' takes effect.
357 If the file name does not specify the method, lookup is done using the
358 empty string for the method name."
360 :type
'(repeat (list (choice :tag
"Method regexp" regexp sexp
)
361 (choice :tag
" Host regexp" regexp sexp
)
362 (choice :tag
" User name" string
(const nil
))))
365 (defcustom tramp-default-host
(system-name)
366 "Default host to use for transferring files.
367 Useful for su and sudo methods mostly."
373 (defcustom tramp-default-host-alist nil
374 "Default host to use for specific method/user pairs.
375 This is an alist of items (METHOD USER HOST). The first matching item
376 specifies the host to use for a file name which does not specify a
377 host. METHOD and USER are regular expressions or nil, which is
378 interpreted as a regular expression which always matches. If no entry
379 matches, the variable `tramp-default-host' takes effect.
381 If the file name does not specify the method, lookup is done using the
382 empty string for the method name."
385 :type
'(repeat (list (choice :tag
"Method regexp" regexp sexp
)
386 (choice :tag
" User regexp" regexp sexp
)
387 (choice :tag
" Host name" string
(const nil
))))
390 (defcustom tramp-default-proxies-alist nil
391 "Route to be followed for specific host/user pairs.
392 This is an alist of items (HOST USER PROXY). The first matching
393 item specifies the proxy to be passed for a file name located on
394 a remote target matching USER@HOST. HOST and USER are regular
395 expressions. PROXY must be a Tramp filename without a localname
396 part. Method and user name on PROXY are optional, which is
397 interpreted with the default values. PROXY can contain the
398 patterns %h and %u, which are replaced by the strings matching
399 HOST or USER, respectively.
401 HOST, USER or PROXY could also be Lisp forms, which will be
402 evaluated. The result must be a string or nil, which is
403 interpreted as a regular expression which always matches."
405 :type
'(repeat (list (choice :tag
"Host regexp" regexp sexp
)
406 (choice :tag
"User regexp" regexp sexp
)
407 (choice :tag
" Proxy name" string
(const nil
))))
410 (defcustom tramp-save-ad-hoc-proxies nil
411 "Whether to save ad-hoc proxies persistently."
417 (defcustom tramp-restricted-shell-hosts-alist
418 (when (memq system-type
'(windows-nt))
419 (list (concat "\\`" (regexp-quote (system-name)) "\\'")))
420 "List of hosts, which run a restricted shell.
421 This is a list of regular expressions, which denote hosts running
422 a registered shell like \"rbash\". Those hosts can be used as
423 proxies only, see `tramp-default-proxies-alist'. If the local
424 host runs a registered shell, it shall be added to this list, too."
427 :type
'(repeat (regexp :tag
"Host regexp"))
431 (defcustom tramp-local-host-regexp
435 (list "localhost" "localhost6" (system-name) "127.0.0.1" "::1") t
)
437 "Host names which are regarded as local host.
438 If the local host runs a chrooted environment, set this to nil."
441 :type
'(choice (const :tag
"Chrooted environment" nil
)
442 (regexp :tag
"Host regexp"))
445 (defvar tramp-completion-function-alist nil
446 "Alist of methods for remote files.
447 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...).
448 Each NAME stands for a remote access method. Each PAIR is of the form
449 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
450 names from FILE for completion. The following predefined FUNCTIONs exists:
452 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
453 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
454 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
455 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
456 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
457 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
458 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
459 * `tramp-parse-etc-group' for \"/etc/group\" like files.
460 * `tramp-parse-netrc' for \"~/.netrc\" like files.
461 * `tramp-parse-putty' for PuTTY registered sessions.
463 FUNCTION can also be a user defined function. For more details see
466 (defconst tramp-echo-mark-marker
"_echo"
467 "String marker to surround echoed commands.")
469 (defconst tramp-echo-mark-marker-length
(length tramp-echo-mark-marker
)
470 "String length of `tramp-echo-mark-marker'.")
472 (defconst tramp-echo-mark
473 (concat tramp-echo-mark-marker
474 (make-string tramp-echo-mark-marker-length ?
\b))
475 "String mark to be transmitted around shell commands.
476 Used to separate their echo from the output they produce. This
477 will only be used if we cannot disable remote echo via stty.
478 This string must have no effect on the remote shell except for
479 producing some echo which can later be detected by
480 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
481 followed by an equal number of backspaces to erase them will
484 (defconst tramp-echoed-echo-mark-regexp
485 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
486 tramp-echo-mark-marker tramp-echo-mark-marker-length
)
487 "Regexp which matches `tramp-echo-mark' as it gets echoed by
490 (defcustom tramp-local-end-of-line
491 (if (memq system-type
'(windows-nt)) "\r\n" "\n")
492 "String used for end of line in local processes."
498 (defcustom tramp-rsh-end-of-line
"\n"
499 "String used for end of line in rsh connections.
500 I don't think this ever needs to be changed, so please tell me about it
501 if you need to change this."
506 (defcustom tramp-login-prompt-regexp
507 ".*\\(user\\|login\\)\\( .*\\)?: *"
508 "Regexp matching login-like prompts.
509 The regexp should match at end of buffer.
511 Sometimes the prompt is reported to look like \"login as:\"."
516 (defcustom tramp-shell-prompt-pattern
517 ;; Allow a prompt to start right after a ^M since it indeed would be
518 ;; displayed at the beginning of the line (and Zsh uses it). This
519 ;; regexp works only for GNU Emacs.
520 ;; Allow also [] style prompts. They can appear only during
521 ;; connection initialization; Tramp redefines the prompt afterwards.
522 (concat "\\(?:^\\|\r\\)"
523 "[^]#$%>\n]*#?[]#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
524 "Regexp to match prompts from remote shell.
525 Normally, Tramp expects you to configure `shell-prompt-pattern'
526 correctly, but sometimes it happens that you are connecting to a
527 remote host which sends a different kind of shell prompt. Therefore,
528 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
529 and also things matched by this variable. The default value of this
530 variable is similar to the default value of `shell-prompt-pattern',
531 which should work well in many cases.
533 This regexp must match both `tramp-initial-end-of-output' and
534 `tramp-end-of-output'."
539 (defcustom tramp-password-prompt-regexp
540 (format "^.*\\(%s\\).*:\^@? *"
541 ;; `password-word-equivalents' has been introduced with Emacs 24.4.
542 (regexp-opt (or (bound-and-true-p password-word-equivalents
)
543 '("password" "passphrase"))))
544 "Regexp matching password-like prompts.
545 The regexp should match at end of buffer.
547 The `sudo' program appears to insert a `^@' character into the prompt."
553 (defcustom tramp-wrong-passwd-regexp
555 ;; These strings should be on the last line
556 (regexp-opt '("Permission denied"
561 "Timeout, server not responding."
563 "Name or service not known"
564 "Host key verification failed."
565 "No supported authentication methods left to try!")
570 ;; Here comes a list of regexes, separated by \\|
571 "Received signal [0-9]+"
573 "Regexp matching a `login failed' message.
574 The regexp should match at end of buffer."
579 (defcustom tramp-yesno-prompt-regexp
581 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t
)
583 "Regular expression matching all yes/no queries which need to be confirmed.
584 The confirmation should be done with yes or no.
585 The regexp should match at end of buffer.
586 See also `tramp-yn-prompt-regexp'."
591 (defcustom tramp-yn-prompt-regexp
593 (regexp-opt '("Store key in cache? (y/n)"
594 "Update cached key? (y/n, Return cancels connection)")
597 "Regular expression matching all y/n queries which need to be confirmed.
598 The confirmation should be done with y or n.
599 The regexp should match at end of buffer.
600 See also `tramp-yesno-prompt-regexp'."
605 (defcustom tramp-terminal-prompt-regexp
609 "Terminal type\\? \\[.*\\]"
611 "Regular expression matching all terminal setting prompts.
612 The regexp should match at end of buffer.
613 The answer will be provided by `tramp-action-terminal', which see."
618 (defcustom tramp-operation-not-permitted-regexp
619 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
620 (regexp-opt '("Operation not permitted") t
))
621 "Regular expression matching keep-date problems in (s)cp operations.
622 Copying has been performed successfully already, so this message can
628 (defcustom tramp-copy-failed-regexp
630 (regexp-opt '("Permission denied"
633 "No such file or directory")
636 "Regular expression matching copy problems in (s)cp operations."
641 (defcustom tramp-process-alive-regexp
643 "Regular expression indicating a process has finished.
644 In fact this expression is empty by intention, it will be used only to
645 check regularly the status of the associated process.
646 The answer will be provided by `tramp-action-process-alive',
647 `tramp-action-out-of-band', which see."
652 (defconst tramp-temp-name-prefix
"tramp."
653 "Prefix to use for temporary files.
654 If this is a relative file name (such as \"tramp.\"), it is considered
655 relative to the directory name returned by the function
656 `tramp-compat-temporary-file-directory' (which see). It may also be an
657 absolute file name; don't forget to include a prefix for the filename
660 (defconst tramp-temp-buffer-name
" *tramp temp*"
661 "Buffer name for a temporary buffer.
662 It shall be used in combination with `generate-new-buffer-name'.")
664 (defvar tramp-temp-buffer-file-name nil
665 "File name of a persistent local temporary file.
666 Useful for \"rsync\" like methods.")
667 (make-variable-buffer-local 'tramp-temp-buffer-file-name
)
668 (put 'tramp-temp-buffer-file-name
'permanent-local t
)
671 (defcustom tramp-syntax
'default
672 "Tramp filename syntax to be used.
674 It can have the following values:
676 `default' -- Default syntax
677 `simplified' -- Ange-FTP like syntax
678 `separate' -- Syntax as defined for XEmacs originally
680 Do not change the value by `setq', it must be changed only via
681 Customize. See also `tramp-change-syntax'."
684 :package-version
'(Tramp .
"2.3.3")
685 :type
'(choice (const :tag
"Default" default
)
686 (const :tag
"Ange-FTP" simplified
)
687 (const :tag
"XEmacs" separate
))
689 :initialize
'custom-initialize-set
690 :set
'tramp-set-syntax
)
692 (defun tramp-set-syntax (symbol value
)
693 "Set SYMBOL to value VALUE.
694 Used in user option `tramp-syntax'. There are further variables
695 to be set, depending on VALUE."
696 ;; Check allowed values.
697 (unless (memq value
(tramp-syntax-values))
698 (tramp-user-error "Wrong `tramp-syntax' %s" tramp-syntax
))
699 ;; Cleanup existing buffers.
700 (unless (eq (symbol-value symbol
) value
)
701 (tramp-cleanup-all-buffers))
703 (set-default symbol value
)
704 ;; Reset the depending variables.
706 (setq tramp-prefix-format
(tramp-build-prefix-format)
707 tramp-prefix-regexp
(tramp-build-prefix-regexp)
708 tramp-method-regexp
(tramp-build-method-regexp)
709 tramp-postfix-method-format
(tramp-build-postfix-method-format)
710 tramp-postfix-method-regexp
(tramp-build-postfix-method-regexp)
711 tramp-prefix-ipv6-format
(tramp-build-prefix-ipv6-format)
712 tramp-prefix-ipv6-regexp
(tramp-build-prefix-ipv6-regexp)
713 tramp-postfix-ipv6-format
(tramp-build-postfix-ipv6-format)
714 tramp-postfix-ipv6-regexp
(tramp-build-postfix-ipv6-regexp)
715 tramp-postfix-host-format
(tramp-build-postfix-host-format)
716 tramp-postfix-host-regexp
(tramp-build-postfix-host-regexp)
717 tramp-remote-file-name-spec-regexp
718 (tramp-build-remote-file-name-spec-regexp)
719 tramp-file-name-structure
(tramp-build-file-name-structure)
720 tramp-file-name-regexp
(tramp-build-file-name-regexp)
721 tramp-completion-file-name-regexp
722 (tramp-build-completion-file-name-regexp)))
723 ;; Rearrange file name handlers.
724 (tramp-register-file-name-handlers))
726 ;; Initialize the Tramp syntax variables. We want to override initial
727 ;; value of `tramp-file-name-regexp'. Other Tramp syntax variables
728 ;; must be initialized as well to proper values. We do not call
729 ;; `custom-set-variable', this would load Tramp via custom.el.
730 (eval-after-load 'tramp
731 '(tramp-set-syntax 'tramp-syntax
(tramp-compat-tramp-syntax)))
733 (defun tramp-syntax-values ()
734 "Return possible values of `tramp-syntax', a list"
735 (let ((values (cdr (get 'tramp-syntax
'custom-type
))))
736 (setq values
(mapcar 'last values
)
737 values
(mapcar 'car values
))))
739 (defun tramp-lookup-syntax (alist)
740 "Look up a syntax string in ALIST according to `tramp-compat-tramp-syntax.'
741 Raise an error if `tramp-syntax' is invalid."
742 (or (cdr (assq (tramp-compat-tramp-syntax) alist
))
743 (error "Wrong `tramp-syntax' %s" tramp-syntax
)))
745 (defconst tramp-prefix-format-alist
749 "Alist mapping Tramp syntax to strings beginning Tramp file names.")
751 (defun tramp-build-prefix-format ()
752 (tramp-lookup-syntax tramp-prefix-format-alist
))
754 (defvar tramp-prefix-format
(tramp-build-prefix-format)
755 "String matching the very beginning of Tramp file names.
756 Used in `tramp-make-tramp-file-name'.")
758 (defun tramp-build-prefix-regexp ()
759 (concat "^" (regexp-quote tramp-prefix-format
)))
761 (defvar tramp-prefix-regexp
(tramp-build-prefix-regexp)
762 "Regexp matching the very beginning of Tramp file names.
763 Should always start with \"^\". Derived from `tramp-prefix-format'.")
765 (defconst tramp-method-regexp-alist
766 '((default .
"[a-zA-Z0-9-]+")
768 (separate .
"[a-zA-Z0-9-]*"))
769 "Alist mapping Tramp syntax to regexps matching methods identifiers.")
771 (defun tramp-build-method-regexp ()
772 (tramp-lookup-syntax tramp-method-regexp-alist
))
774 (defvar tramp-method-regexp
(tramp-build-method-regexp)
775 "Regexp matching methods identifiers.
776 The `ftp' syntax does not support methods.")
778 (defconst tramp-postfix-method-format-alist
782 "Alist mapping Tramp syntax to the delimiter after the method.")
784 (defun tramp-build-postfix-method-format ()
785 (tramp-lookup-syntax tramp-postfix-method-format-alist
))
787 (defvar tramp-postfix-method-format
(tramp-build-postfix-method-format)
788 "String matching delimiter between method and user or host names.
789 The `ftp' syntax does not support methods.
790 Used in `tramp-make-tramp-file-name'.")
792 (defun tramp-build-postfix-method-regexp ()
793 (regexp-quote tramp-postfix-method-format
))
795 (defvar tramp-postfix-method-regexp
(tramp-build-postfix-method-regexp)
796 "Regexp matching delimiter between method and user or host names.
797 Derived from `tramp-postfix-method-format'.")
799 (defconst tramp-user-regexp
"[^/|: \t]+"
800 "Regexp matching user names.")
803 (defconst tramp-prefix-domain-format
"%"
804 "String matching delimiter between user and domain names.")
807 (defconst tramp-prefix-domain-regexp
(regexp-quote tramp-prefix-domain-format
)
808 "Regexp matching delimiter between user and domain names.
809 Derived from `tramp-prefix-domain-format'.")
811 (defconst tramp-domain-regexp
"[a-zA-Z0-9_.-]+"
812 "Regexp matching domain names.")
814 (defconst tramp-user-with-domain-regexp
815 (concat "\\(" tramp-user-regexp
"\\)"
816 tramp-prefix-domain-regexp
817 "\\(" tramp-domain-regexp
"\\)")
818 "Regexp matching user names with domain names.")
820 (defconst tramp-postfix-user-format
"@"
821 "String matching delimiter between user and host names.
822 Used in `tramp-make-tramp-file-name'.")
824 (defconst tramp-postfix-user-regexp
(regexp-quote tramp-postfix-user-format
)
825 "Regexp matching delimiter between user and host names.
826 Derived from `tramp-postfix-user-format'.")
828 (defconst tramp-host-regexp
"[a-zA-Z0-9_.%-]+"
829 "Regexp matching host names.")
831 (defconst tramp-prefix-ipv6-format-alist
835 "Alist mapping Tramp syntax to strings prefixing IPv6 addresses.")
837 (defun tramp-build-prefix-ipv6-format ()
838 (tramp-lookup-syntax tramp-prefix-ipv6-format-alist
))
840 (defvar tramp-prefix-ipv6-format
(tramp-build-prefix-ipv6-format)
841 "String matching left hand side of IPv6 addresses.
842 Used in `tramp-make-tramp-file-name'.")
844 (defun tramp-build-prefix-ipv6-regexp ()
845 (regexp-quote tramp-prefix-ipv6-format
))
847 (defvar tramp-prefix-ipv6-regexp
(tramp-build-prefix-ipv6-regexp)
848 "Regexp matching left hand side of IPv6 addresses.
849 Derived from `tramp-prefix-ipv6-format'.")
851 ;; The following regexp is a bit sloppy. But it shall serve our
852 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
853 ;; "::ffff:192.168.0.1".
854 (defconst tramp-ipv6-regexp
"\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
855 "Regexp matching IPv6 addresses.")
857 (defconst tramp-postfix-ipv6-format-alist
861 "Alist mapping Tramp syntax to suffix for IPv6 addresses.")
863 (defun tramp-build-postfix-ipv6-format ()
864 (tramp-lookup-syntax tramp-postfix-ipv6-format-alist
))
866 (defvar tramp-postfix-ipv6-format
(tramp-build-postfix-ipv6-format)
867 "String matching right hand side of IPv6 addresses.
868 Used in `tramp-make-tramp-file-name'.")
870 (defun tramp-build-postfix-ipv6-regexp ()
871 (regexp-quote tramp-postfix-ipv6-format
))
873 (defvar tramp-postfix-ipv6-regexp
(tramp-build-postfix-ipv6-regexp)
874 "Regexp matching right hand side of IPv6 addresses.
875 Derived from `tramp-postfix-ipv6-format'.")
877 (defconst tramp-prefix-port-format
"#"
878 "String matching delimiter between host names and port numbers.")
880 (defconst tramp-prefix-port-regexp
(regexp-quote tramp-prefix-port-format
)
881 "Regexp matching delimiter between host names and port numbers.
882 Derived from `tramp-prefix-port-format'.")
884 (defconst tramp-port-regexp
"[0-9]+"
885 "Regexp matching port numbers.")
887 (defconst tramp-host-with-port-regexp
888 (concat "\\(" tramp-host-regexp
"\\)"
889 tramp-prefix-port-regexp
890 "\\(" tramp-port-regexp
"\\)")
891 "Regexp matching host names with port numbers.")
893 (defconst tramp-postfix-hop-format
"|"
894 "String matching delimiter after ad-hoc hop definitions.")
896 (defconst tramp-postfix-hop-regexp
(regexp-quote tramp-postfix-hop-format
)
897 "Regexp matching delimiter after ad-hoc hop definitions.
898 Derived from `tramp-postfix-hop-format'.")
900 (defconst tramp-postfix-host-format-alist
904 "Alist mapping Tramp syntax to strings between host and local names.")
906 (defun tramp-build-postfix-host-format ()
907 (tramp-lookup-syntax tramp-postfix-host-format-alist
))
909 (defvar tramp-postfix-host-format
(tramp-build-postfix-host-format)
910 "String matching delimiter between host names and localnames.
911 Used in `tramp-make-tramp-file-name'.")
913 (defun tramp-build-postfix-host-regexp ()
914 (regexp-quote tramp-postfix-host-format
))
916 (defvar tramp-postfix-host-regexp
(tramp-build-postfix-host-regexp)
917 "Regexp matching delimiter between host names and localnames.
918 Derived from `tramp-postfix-host-format'.")
920 (defconst tramp-localname-regexp
"[^\n\r]*\\'"
921 "Regexp matching localnames.")
923 (defconst tramp-unknown-id-string
"UNKNOWN"
924 "String used to denote an unknown user or group")
926 (defconst tramp-unknown-id-integer -
1
927 "Integer used to denote an unknown user or group")
929 ;;; File name format:
931 (defun tramp-build-remote-file-name-spec-regexp ()
932 "Construct a regexp matching a Tramp file name for a Tramp syntax.
933 It is expected, that `tramp-syntax' has the proper value."
935 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
936 "\\(?:" "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
"\\)?"
937 "\\(" "\\(?:" tramp-host-regexp
"\\|"
938 tramp-prefix-ipv6-regexp
"\\(?:" tramp-ipv6-regexp
"\\)?"
939 tramp-postfix-ipv6-regexp
"\\)"
940 "\\(?:" tramp-prefix-port-regexp tramp-port-regexp
"\\)?" "\\)?"))
942 (defvar tramp-remote-file-name-spec-regexp
943 (tramp-build-remote-file-name-spec-regexp)
944 "Regular expression matching a Tramp file name between prefix and postfix.")
946 (defun tramp-build-file-name-structure ()
947 "Construct the Tramp file name structure for a Tramp syntax.
948 It is expected, that `tramp-syntax' has the proper value.
949 See `tramp-file-name-structure'."
953 "\\(" "\\(?:" tramp-remote-file-name-spec-regexp
954 tramp-postfix-hop-regexp
"\\)+" "\\)?"
955 tramp-remote-file-name-spec-regexp tramp-postfix-host-regexp
956 "\\(" tramp-localname-regexp
"\\)")
959 (defvar tramp-file-name-structure
(tramp-build-file-name-structure)
960 "List of six elements (REGEXP METHOD USER HOST FILE HOP), detailing \
961 the Tramp file name structure.
963 The first element REGEXP is a regular expression matching a Tramp file
964 name. The regex should contain parentheses around the method name,
965 the user name, the host name, and the file name parts.
967 The second element METHOD is a number, saying which pair of
968 parentheses matches the method name. The third element USER is
969 similar, but for the user name. The fourth element HOST is similar,
970 but for the host name. The fifth element FILE is for the file name.
971 The last element HOP is the ad-hoc hop definition, which could be a
972 cascade of several hops.
974 These numbers are passed directly to `match-string', which see. That
975 means the opening parentheses are counted to identify the pair.
977 See also `tramp-file-name-regexp'.")
979 (defun tramp-build-file-name-regexp ()
980 (car tramp-file-name-structure
))
983 (defconst tramp-initial-file-name-regexp
"\\`/.+:.*:"
984 "Value for `tramp-file-name-regexp' for autoload.
985 It must match the initial `tramp-syntax' settings.")
988 (defvar tramp-file-name-regexp tramp-initial-file-name-regexp
989 "Regular expression matching file names handled by Tramp.
990 This regexp should match Tramp file names but no other file
991 names. When calling `tramp-register-file-name-handlers', the
992 initial value is overwritten by the car of `tramp-file-name-structure'.")
994 (defconst tramp-completion-file-name-regexp-default
997 ;; Optional multi hop.
998 "\\([^/|:]+:[^/|:]*|\\)*"
1000 (if (memq system-type
'(cygwin windows-nt
))
1001 ;; The method is either "-", or at least two characters.
1002 "\\(-\\|[^/|:]\\{2,\\}\\)"
1003 ;; At least one character for method.
1005 ;; Method separator, user name and host name.
1008 "Value for `tramp-completion-file-name-regexp' for default remoting.
1009 See `tramp-file-name-structure' for more explanations.
1011 On W32 systems, the volume letter must be ignored.")
1013 (defconst tramp-completion-file-name-regexp-simplified
1016 ;; Optional multi hop.
1019 (if (memq system-type
'(cygwin windows-nt
))
1020 ;; At least two characters.
1022 ;; At least one character.
1025 "Value for `tramp-completion-file-name-regexp' for simplified style remoting.
1026 See `tramp-file-name-structure' for more explanations.
1028 On W32 systems, the volume letter must be ignored.")
1030 (defconst tramp-completion-file-name-regexp-separate
1031 "\\`/\\(\\[[^]]*\\)?\\'"
1032 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1033 See `tramp-file-name-structure' for more explanations.")
1035 (defconst tramp-completion-file-name-regexp-alist
1036 `((default .
,tramp-completion-file-name-regexp-default
)
1037 (simplified .
,tramp-completion-file-name-regexp-simplified
)
1038 (separate .
,tramp-completion-file-name-regexp-separate
))
1039 "Alist mapping incomplete Tramp file names.")
1041 (defun tramp-build-completion-file-name-regexp ()
1042 (tramp-lookup-syntax tramp-completion-file-name-regexp-alist
))
1044 (defvar tramp-completion-file-name-regexp
1045 (tramp-build-completion-file-name-regexp)
1046 "Regular expression matching file names handled by Tramp completion.
1047 This regexp should match partial Tramp file names only.
1049 Please note that the entry in `file-name-handler-alist' is made when
1050 this file \(tramp.el) is loaded. This means that this variable must be set
1051 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1052 updated after changing this variable.
1054 Also see `tramp-file-name-structure'.")
1057 (defconst tramp-autoload-file-name-regexp
1060 (if (memq system-type
'(cygwin windows-nt
))
1061 ;; The method is either "-", or at least two characters.
1062 "\\(-\\|[^/|:]\\{2,\\}\\)"
1063 ;; At least one character for method.
1066 "Regular expression matching file names handled by Tramp autoload.
1067 It must match the initial `tramp-syntax' settings. It should not
1068 match file names at root of the underlying local file system,
1069 like \"/sys\" or \"/C:\".")
1071 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1072 ;; known to have a bug in `process-send-string'; some ssh connections appear
1073 ;; to drop bytes when data is sent too quickly. There is also a connection
1074 ;; buffer local variable, which is computed depending on remote host properties
1075 ;; when `tramp-chunksize' is zero or nil.
1076 (defcustom tramp-chunksize
(when (memq system-type
'(hpux)) 500)
1077 ;; Parentheses in docstring starting at beginning of line are escaped.
1078 ;; Fontification is messed up when
1079 ;; `open-paren-in-column-0-is-defun-start' set to t.
1080 "If non-nil, chunksize for sending input to local process.
1081 It is necessary only on systems which have a buggy `process-send-string'
1082 implementation. The necessity, whether this variable must be set, can be
1083 checked via the following code:
1086 (let* ((user \"xxx\") (host \"yyy\")
1088 (sent init) (received init))
1089 (while (= sent received)
1090 (setq sent (+ sent step))
1092 (let ((proc (start-process (buffer-name) (current-buffer)
1093 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1094 (when (process-live-p proc)
1095 (process-send-string proc (make-string sent ?\\ ))
1096 (process-send-eof proc)
1097 (process-send-eof proc))
1098 (while (not (progn (goto-char (point-min))
1099 (re-search-forward \"\\\\w+\" (point-max) t)))
1100 (accept-process-output proc 1))
1101 (when (process-live-p proc)
1102 (setq received (string-to-number (match-string 0)))
1103 (delete-process proc)
1104 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1106 (if (> sent (+ init step))
1107 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1109 (message \"Test does not work\")
1110 (display-buffer (current-buffer))
1113 In the Emacs normally running Tramp, evaluate the above code
1114 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1115 respectively). You can do this, for example, by pasting it into
1116 the `*scratch*' buffer and then hitting C-j with the cursor after the
1117 last closing parenthesis. Note that it works only if you have configured
1118 \"ssh\" to run without password query, see ssh-agent(1).
1120 You will see the number of bytes sent successfully to the remote host.
1121 If that number exceeds 1000, you can stop the execution by hitting
1122 C-g, because your Emacs is likely clean.
1124 When it is necessary to set `tramp-chunksize', you might consider to
1125 use an out-of-the-band method \(like \"scp\") instead of an internal one
1126 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1129 If your Emacs is buggy, the code stops and gives you an indication
1130 about the value `tramp-chunksize' should be set. Maybe you could just
1131 experiment a bit, e.g. changing the values of `init' and `step'
1132 in the third line of the code.
1134 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1135 this variable to be set as well."
1137 :type
'(choice (const nil
) integer
)
1140 ;; Logging in to a remote host normally requires obtaining a pty. But
1141 ;; Emacs on macOS has process-connection-type set to nil by default,
1142 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1143 ;; for an override of the system default.
1144 (defcustom tramp-process-connection-type t
1145 "Overrides `process-connection-type' for connections from Tramp.
1146 Tramp binds `process-connection-type' to the value given here before
1147 opening a connection to a remote host."
1149 :type
'(choice (const nil
) (const t
) (const pty
))
1152 (defcustom tramp-connection-timeout
60
1153 "Defines the max time to wait for establishing a connection (in seconds).
1154 This can be overwritten for different connection types in `tramp-methods'.
1156 The timeout does not include the time reading a password."
1162 (defcustom tramp-connection-min-time-diff
5
1163 "Defines seconds between two consecutive connection attempts.
1164 This is necessary as self defense mechanism, in order to avoid
1165 yo-yo connection attempts when the remote host is unavailable.
1167 A value of 0 or nil suppresses this check. This might be
1168 necessary, when several out-of-order copy operations are
1169 performed, or when several asynchronous processes will be started
1170 in a short time frame. In those cases it is recommended to
1171 let-bind this variable."
1174 :type
'(choice (const nil
) integer
)
1177 (defcustom tramp-completion-reread-directory-timeout
10
1178 "Defines seconds since last remote command before rereading a directory.
1179 A remote directory might have changed its contents. In order to
1180 make it visible during file name completion in the minibuffer,
1181 Tramp flushes its cache and rereads the directory contents when
1182 more than `tramp-completion-reread-directory-timeout' seconds
1183 have been gone since last remote command execution. A value of t
1184 would require an immediate reread during filename completion, nil
1185 means to use always cached values for the directory contents."
1187 :type
'(choice (const nil
) (const t
) integer
)
1190 ;;; Internal Variables:
1192 (defvar tramp-current-connection nil
1193 "Last connection timestamp.")
1195 (defvar tramp-password-save-function nil
1196 "Password save function.
1197 Will be called once the password has been verified by successful
1200 (defconst tramp-completion-file-name-handler-alist
1201 '((file-name-all-completions
1202 . tramp-completion-handle-file-name-all-completions
)
1203 (file-name-completion . tramp-completion-handle-file-name-completion
))
1204 "Alist of completion handler functions.
1205 Used for file names matching `tramp-completion-file-name-regexp'.
1206 Operations not mentioned here will be handled by Tramp's file
1207 name handler functions, or the normal Emacs functions.")
1209 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1210 ;;;###tramp-autoload
1211 (defvar tramp-foreign-file-name-handler-alist nil
1212 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1213 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1216 ;;; Internal functions which must come first:
1218 ;; Conversion functions between external representation and
1219 ;; internal data structure. Convenience functions for internal
1222 ;; The basic structure for remote file names. We use a list :type,
1223 ;; in order to be compatible with Emacs 24 and 25.
1224 (cl-defstruct (tramp-file-name (:type list
) :named
)
1225 method user domain host port localname hop
)
1227 (defun tramp-file-name-user-domain (vec)
1228 "Return user and domain components of VEC."
1229 (when (or (tramp-file-name-user vec
) (tramp-file-name-domain vec
))
1230 (concat (tramp-file-name-user vec
)
1231 (and (tramp-file-name-domain vec
)
1232 tramp-prefix-domain-format
)
1233 (tramp-file-name-domain vec
))))
1235 (defun tramp-file-name-host-port (vec)
1236 "Return host and port components of VEC."
1237 (when (or (tramp-file-name-host vec
) (tramp-file-name-port vec
))
1238 (concat (tramp-file-name-host vec
)
1239 (and (tramp-file-name-port vec
)
1240 tramp-prefix-port-format
)
1241 (tramp-file-name-port vec
))))
1243 (defun tramp-file-name-port-or-default (vec)
1244 "Return port component of VEC.
1245 If nil, return `tramp-default-port'."
1246 (or (tramp-file-name-port vec
)
1247 (tramp-get-method-parameter vec
'tramp-default-port
)))
1249 (defun tramp-file-name-equal-p (vec1 vec2
)
1250 "Check, whether VEC1 and VEC2 denote the same `tramp-file-name'."
1251 (and (tramp-file-name-p vec1
) (tramp-file-name-p vec2
)
1252 (string-equal (tramp-file-name-method vec1
)
1253 (tramp-file-name-method vec2
))
1254 (string-equal (tramp-file-name-user-domain vec1
)
1255 (tramp-file-name-user-domain vec2
))
1256 (string-equal (tramp-file-name-host-port vec1
)
1257 (tramp-file-name-host-port vec2
))))
1259 (defun tramp-get-method-parameter (vec param
)
1260 "Return the method parameter PARAM.
1261 If VEC is a vector, check first in connection properties.
1262 Afterwards, check in `tramp-methods'. If the `tramp-methods'
1263 entry does not exist, return nil."
1265 (replace-regexp-in-string "^tramp-" "" (symbol-name param
))))
1266 (if (tramp-connection-property-p vec hash-entry
)
1267 ;; We use the cached property.
1268 (tramp-get-connection-property vec hash-entry nil
)
1269 ;; Use the static value from `tramp-methods'.
1270 (let ((methods-entry
1271 (assoc param
(assoc (tramp-file-name-method vec
) tramp-methods
))))
1272 (when methods-entry
(cadr methods-entry
))))))
1274 ;; The localname can be quoted with "/:". Extract this.
1275 (defun tramp-file-name-unquote-localname (vec)
1276 "Return unquoted localname component of VEC."
1277 (tramp-compat-file-name-unquote (tramp-file-name-localname vec
)))
1279 ;;;###tramp-autoload
1280 (defun tramp-tramp-file-p (name)
1281 "Return t if NAME is a string with Tramp file name syntax."
1283 ;; No "/:" and "/c:". This is not covered by `tramp-file-name-regexp'.
1284 (not (string-match-p
1285 (if (memq system-type
'(cygwin windows-nt
))
1286 "^/[[:alpha:]]?:" "^/:")
1288 (string-match-p tramp-file-name-regexp name
)
1291 (defun tramp-find-method (method user host
)
1292 "Return the right method string to use.
1293 This is METHOD, if non-nil. Otherwise, do a lookup in
1294 `tramp-default-method-alist'."
1296 (or (string-equal method
"")
1297 (string-equal method tramp-default-method-marker
)))
1301 (let ((choices tramp-default-method-alist
)
1304 (setq item
(pop choices
))
1305 (when (and (string-match (or (nth 0 item
) "") (or host
""))
1306 (string-match (or (nth 1 item
) "") (or user
"")))
1307 (setq lmethod
(nth 2 item
))
1308 (setq choices nil
)))
1310 tramp-default-method
)))
1311 ;; We must mark, whether a default value has been used.
1312 (if (or method
(null result
))
1314 (propertize result
'tramp-default t
))))
1316 (defun tramp-find-user (method user host
)
1317 "Return the right user string to use.
1318 This is USER, if non-nil. Otherwise, do a lookup in
1319 `tramp-default-user-alist'."
1322 (let ((choices tramp-default-user-alist
)
1325 (setq item
(pop choices
))
1326 (when (and (string-match (or (nth 0 item
) "") (or method
""))
1327 (string-match (or (nth 1 item
) "") (or host
"")))
1328 (setq luser
(nth 2 item
))
1329 (setq choices nil
)))
1331 tramp-default-user
)))
1332 ;; We must mark, whether a default value has been used.
1333 (if (or user
(null result
))
1335 (propertize result
'tramp-default t
))))
1337 (defun tramp-find-host (method user host
)
1338 "Return the right host string to use.
1339 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
1340 (or (and (> (length host
) 0) host
)
1341 (let ((choices tramp-default-host-alist
)
1344 (setq item
(pop choices
))
1345 (when (and (string-match (or (nth 0 item
) "") (or method
""))
1346 (string-match (or (nth 1 item
) "") (or user
"")))
1347 (setq lhost
(nth 2 item
))
1348 (setq choices nil
)))
1350 tramp-default-host
))
1352 (defun tramp-dissect-file-name (name &optional nodefault
)
1353 "Return a `tramp-file-name' structure of NAME, a remote file name.
1354 The structure consists of method, user, domain, host, port,
1355 localname (file name on remote host), and hop.
1357 Unless NODEFAULT is non-nil, method, user and host are expanded
1358 to their default values. For the other file name parts, no
1359 default values are used."
1361 (unless (tramp-tramp-file-p name
)
1362 (tramp-user-error nil
"Not a Tramp file name: \"%s\"" name
))
1363 (if (not (string-match (nth 0 tramp-file-name-structure
) name
))
1364 (error "`tramp-file-name-structure' didn't match!")
1365 (let ((method (match-string (nth 1 tramp-file-name-structure
) name
))
1366 (user (match-string (nth 2 tramp-file-name-structure
) name
))
1367 (host (match-string (nth 3 tramp-file-name-structure
) name
))
1368 (localname (match-string (nth 4 tramp-file-name-structure
) name
))
1369 (hop (match-string (nth 5 tramp-file-name-structure
) name
))
1372 (when (string-match tramp-user-with-domain-regexp user
)
1373 (setq domain
(match-string 2 user
)
1374 user
(match-string 1 user
))))
1377 (when (string-match tramp-host-with-port-regexp host
)
1378 (setq port
(match-string 2 host
)
1379 host
(match-string 1 host
)))
1380 (when (string-match tramp-prefix-ipv6-regexp host
)
1381 (setq host
(replace-match "" nil t host
)))
1382 (when (string-match tramp-postfix-ipv6-regexp host
)
1383 (setq host
(replace-match "" nil t host
))))
1386 (setq method
(tramp-find-method method user host
)
1387 user
(tramp-find-user method user host
)
1388 host
(tramp-find-host method user host
)))
1390 (make-tramp-file-name
1391 :method method
:user user
:domain domain
:host host
:port port
1392 :localname localname
:hop hop
)))))
1394 (defun tramp-buffer-name (vec)
1395 "A name for the connection buffer VEC."
1396 (let ((method (tramp-file-name-method vec
))
1397 (user-domain (tramp-file-name-user-domain vec
))
1398 (host-port (tramp-file-name-host-port vec
)))
1399 (if (not (zerop (length user-domain
)))
1400 (format "*tramp/%s %s@%s*" method user-domain host-port
)
1401 (format "*tramp/%s %s*" method host-port
))))
1403 (defun tramp-make-tramp-file-name (&rest args
)
1404 "Construct a Tramp file name from ARGS.
1406 ARGS could have two different signatures. The first one is of
1407 type (VEC &optional LOCALNAME HOP).
1408 If LOCALNAME is nil, the value in VEC is used. If it is a
1409 symbol, a null localname will be used. Otherwise, LOCALNAME is
1410 expected to be a string, which will be used.
1411 If HOP is nil, the value in VEC is used. If it is a symbol, a
1412 null hop will be used. Otherwise, HOP is expected to be a
1413 string, which will be used.
1415 The other signature exists for backward compatibility. It has
1416 the form (METHOD USER DOMAIN HOST PORT LOCALNAME &optional HOP)."
1417 (let (method user domain host port localname hop
)
1419 ((tramp-file-name-p (car args
))
1420 (setq method
(tramp-file-name-method (car args
))
1421 user
(tramp-file-name-user (car args
))
1422 domain
(tramp-file-name-domain (car args
))
1423 host
(tramp-file-name-host (car args
))
1424 port
(tramp-file-name-port (car args
))
1425 localname
(tramp-file-name-localname (car args
))
1426 hop
(tramp-file-name-hop (car args
)))
1428 (setq localname
(and (stringp (cadr args
)) (cadr args
))))
1429 (when (cl-caddr args
)
1430 (setq hop
(and (stringp (cl-caddr args
)) (cl-caddr args
)))))
1432 (t (setq method
(nth 0 args
)
1437 localname
(nth 5 args
)
1440 (when (zerop (length method
))
1441 (signal 'wrong-type-argument
(list 'stringp method
)))
1442 (concat tramp-prefix-format hop
1443 (unless (zerop (length tramp-postfix-method-format
))
1444 (concat method tramp-postfix-method-format
))
1446 (unless (zerop (length domain
))
1447 (concat tramp-prefix-domain-format domain
))
1448 (unless (zerop (length user
))
1449 tramp-postfix-user-format
)
1451 (if (string-match tramp-ipv6-regexp host
)
1453 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
1455 (unless (zerop (length port
))
1456 (concat tramp-prefix-port-format port
))
1457 tramp-postfix-host-format
1460 (defun tramp-completion-make-tramp-file-name (method user host localname
)
1461 "Construct a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1462 It must not be a complete Tramp file name, but as long as there are
1463 necessary only. This function will be used in file name completion."
1464 (concat tramp-prefix-format
1465 (unless (or (zerop (length method
))
1466 (zerop (length tramp-postfix-method-format
)))
1467 (concat method tramp-postfix-method-format
))
1468 (unless (zerop (length user
))
1469 (concat user tramp-postfix-user-format
))
1470 (unless (zerop (length host
))
1472 (if (string-match tramp-ipv6-regexp host
)
1474 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
1476 tramp-postfix-host-format
))
1477 (when localname localname
)))
1479 (defun tramp-get-buffer (vec)
1480 "Get the connection buffer to be used for VEC."
1481 (or (get-buffer (tramp-buffer-name vec
))
1482 (with-current-buffer (get-buffer-create (tramp-buffer-name vec
))
1483 ;; We use the existence of connection property "process-buffer"
1484 ;; as indication, whether a connection is active.
1485 (tramp-set-connection-property
1486 vec
"process-buffer"
1487 (tramp-get-connection-property vec
"process-buffer" nil
))
1488 (setq buffer-undo-list t
1489 default-directory
(tramp-make-tramp-file-name vec
"/" 'nohop
))
1492 (defun tramp-get-connection-buffer (vec)
1493 "Get the connection buffer to be used for VEC.
1494 In case a second asynchronous communication has been started, it is different
1495 from `tramp-get-buffer'."
1496 (or (tramp-get-connection-property vec
"process-buffer" nil
)
1497 (tramp-get-buffer vec
)))
1499 (defun tramp-get-connection-name (vec)
1500 "Get the connection name to be used for VEC.
1501 In case a second asynchronous communication has been started, it is different
1502 from the default one."
1503 (or (tramp-get-connection-property vec
"process-name" nil
)
1504 (tramp-buffer-name vec
)))
1506 (defun tramp-get-connection-process (vec)
1507 "Get the connection process to be used for VEC.
1508 In case a second asynchronous communication has been started, it is different
1509 from the default one."
1510 (and (tramp-file-name-p vec
) (get-process (tramp-get-connection-name vec
))))
1512 (defun tramp-set-connection-local-variables (vec)
1513 "Set connection-local variables in the connection buffer used for VEC.
1514 If connection-local variables are not supported by this Emacs
1515 version, the function does nothing."
1516 (with-current-buffer (tramp-get-connection-buffer vec
)
1517 ;; `hack-connection-local-variables-apply' exists since Emacs 26.1.
1518 (tramp-compat-funcall
1519 'hack-connection-local-variables-apply
1520 `(:application tramp
1521 :protocol
,(tramp-file-name-method vec
)
1522 :user
,(tramp-file-name-user-domain vec
)
1523 :machine
,(tramp-file-name-host-port vec
)))))
1525 (defun tramp-set-connection-local-variables-for-buffer ()
1526 "Set connection-local variables in the current buffer.
1527 If connection-local variables are not supported by this Emacs
1528 version, the function does nothing."
1529 (when (file-remote-p default-directory
)
1530 ;; `hack-connection-local-variables-apply' exists since Emacs 26.1.
1531 (tramp-compat-funcall
1532 'hack-connection-local-variables-apply
1533 `(:application tramp
1534 :protocol
,(file-remote-p default-directory
'method
)
1535 :user
,(file-remote-p default-directory
'user
)
1536 :machine
,(file-remote-p default-directory
'host
)))))
1538 (defun tramp-debug-buffer-name (vec)
1539 "A name for the debug buffer for VEC."
1540 (let ((method (tramp-file-name-method vec
))
1541 (user-domain (tramp-file-name-user-domain vec
))
1542 (host-port (tramp-file-name-host-port vec
)))
1543 (if (not (zerop (length user-domain
)))
1544 (format "*debug tramp/%s %s@%s*" method user-domain host-port
)
1545 (format "*debug tramp/%s %s*" method host-port
))))
1547 (defconst tramp-debug-outline-regexp
1548 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1549 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1551 (defun tramp-debug-outline-level ()
1552 "Return the depth to which a statement is nested in the outline.
1553 Point must be at the beginning of a header line.
1555 The outline level is equal to the verbosity of the Tramp message."
1556 (1+ (string-to-number (match-string 1))))
1558 (defun tramp-get-debug-buffer (vec)
1559 "Get the debug buffer for VEC."
1560 (with-current-buffer
1561 (get-buffer-create (tramp-debug-buffer-name vec
))
1563 (setq buffer-undo-list t
)
1564 ;; So it does not get loaded while `outline-regexp' is let-bound.
1566 ;; Activate `outline-mode'. This runs `text-mode-hook' and
1567 ;; `outline-mode-hook'. We must prevent that local processes
1568 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
1569 ;; Furthermore, `outline-regexp' must have the correct value
1570 ;; already, because it is used by `font-lock-compile-keywords'.
1571 (let ((default-directory (tramp-compat-temporary-file-directory))
1572 (outline-regexp tramp-debug-outline-regexp
))
1574 (set (make-local-variable 'outline-regexp
) tramp-debug-outline-regexp
)
1575 (set (make-local-variable 'outline-level
) 'tramp-debug-outline-level
)
1576 ;; Do not edit the debug buffer.
1577 (set-keymap-parent (current-local-map) special-mode-map
))
1580 (defsubst tramp-debug-message
(vec fmt-string
&rest arguments
)
1581 "Append message to debug buffer.
1582 Message is formatted with FMT-STRING as control string and the remaining
1583 ARGUMENTS to actually emit the message (if applicable)."
1584 (with-current-buffer (tramp-get-debug-buffer vec
)
1585 (goto-char (point-max))
1590 ";; Emacs: %s Tramp: %s -*- mode: outline; -*-"
1591 emacs-version tramp-version
))
1592 (when (>= tramp-verbose
10)
1595 "\n;; Location: %s Git: %s"
1596 (locate-library "tramp") (tramp-repository-get-version)))))
1600 (let ((now (current-time)))
1601 (insert (format-time-string "%T." now
))
1602 (insert (format "%06d " (nth 2 now
))))
1603 ;; Calling Tramp function. We suppress compat and trace functions
1604 ;; from being displayed.
1605 (let ((btn 1) btf fn
)
1607 (setq btf
(nth 1 (backtrace-frame btn
)))
1611 (setq fn
(symbol-name btf
))
1614 (string-match "^tramp" fn
)
1621 "tramp-compat-funcall"
1622 "tramp-condition-case-unless-debug"
1623 "tramp-debug-message"
1625 "tramp-error-with-buffer"
1632 (setq btn
(1+ btn
))))
1633 ;; The following code inserts filename and line number. Should
1634 ;; be inactive by default, because it is time consuming.
1635 ; (let ((ffn (find-function-noselect (intern fn))))
1639 ; (file-name-nondirectory (buffer-file-name (car ffn)))
1640 ; (with-current-buffer (car ffn)
1641 ; (1+ (count-lines (point-min) (cdr ffn)))))))
1642 (insert (format "%s " fn
)))
1644 (insert (apply #'format-message fmt-string arguments
))))
1646 (defvar tramp-message-show-message
(null noninteractive
)
1647 "Show Tramp message in the minibuffer.
1648 This variable is used to suppress progress reporter output, and
1649 to disable messages from `tramp-error'. Those messages are
1650 visible anyway, because an error is raised.")
1652 (defsubst tramp-message
(vec-or-proc level fmt-string
&rest arguments
)
1653 "Emit a message depending on verbosity level.
1654 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
1655 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1656 less than LEVEL. The message is emitted only if `tramp-verbose' is
1657 greater than or equal to LEVEL.
1659 The message is also logged into the debug buffer when `tramp-verbose'
1660 is greater than or equal 4.
1662 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1663 control string and the remaining ARGUMENTS to actually emit the message (if
1666 (when (<= level tramp-verbose
)
1667 ;; Match data must be preserved!
1669 ;; Display only when there is a minimum level.
1670 (when (and tramp-message-show-message
(<= level
3))
1676 ((= level
2) "Warning: ")
1680 ;; Log only when there is a minimum level.
1681 (when (>= tramp-verbose
4)
1682 (let ((tramp-verbose 0))
1683 ;; Append connection buffer for error messages.
1685 (with-current-buffer
1686 (if (processp vec-or-proc
)
1687 (process-buffer vec-or-proc
)
1688 (tramp-get-connection-buffer vec-or-proc
))
1689 (setq fmt-string
(concat fmt-string
"\n%s")
1690 arguments
(append arguments
(list (buffer-string))))))
1691 ;; Translate proc to vec.
1692 (when (processp vec-or-proc
)
1693 (setq vec-or-proc
(process-get vec-or-proc
'vector
))))
1695 (when (tramp-file-name-p vec-or-proc
)
1696 (apply 'tramp-debug-message
1698 (concat (format "(%d) # " level
) fmt-string
)
1701 (defsubst tramp-backtrace
(&optional vec-or-proc
)
1702 "Dump a backtrace into the debug buffer.
1703 If VEC-OR-PROC is nil, the buffer *debug tramp* is used. This
1704 function is meant for debugging purposes."
1705 (when (>= tramp-verbose
10)
1708 vec-or-proc
10 "\n%s" (with-output-to-string (backtrace)))
1709 (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
1711 (defsubst tramp-error
(vec-or-proc signal fmt-string
&rest arguments
)
1713 VEC-OR-PROC identifies the connection to use, SIGNAL is the
1714 signal identifier to be raised, remaining arguments passed to
1715 `tramp-message'. Finally, signal SIGNAL is raised."
1716 (let (tramp-message-show-message)
1717 (tramp-backtrace vec-or-proc
)
1719 ;; FMT-STRING could be just a file name, as in
1720 ;; `file-already-exists' errors. It could contain the ?\%
1721 ;; character, as in smb domain spec.
1722 (setq arguments
(list fmt-string
)
1727 (error-message-string
1729 (get signal
'error-message
)
1730 (apply #'format-message fmt-string arguments
)))))
1731 (signal signal
(list (apply #'format-message fmt-string arguments
)))))
1733 (defsubst tramp-error-with-buffer
1734 (buf vec-or-proc signal fmt-string
&rest arguments
)
1735 "Emit an error, and show BUF.
1736 If BUF is nil, show the connection buf. Wait for 30\", or until
1737 an input event arrives. The other arguments are passed to `tramp-error'."
1738 (save-window-excursion
1739 (let* ((buf (or (and (bufferp buf
) buf
)
1740 (and (processp vec-or-proc
) (process-buffer vec-or-proc
))
1741 (and (tramp-file-name-p vec-or-proc
)
1742 (tramp-get-connection-buffer vec-or-proc
))))
1743 (vec (or (and (tramp-file-name-p vec-or-proc
) vec-or-proc
)
1744 (and buf
(with-current-buffer buf
1745 (tramp-dissect-file-name default-directory
))))))
1747 (apply 'tramp-error vec-or-proc signal fmt-string arguments
)
1750 tramp-message-show-message
1751 (not (zerop tramp-verbose
))
1752 ;; Do not show when flagged from outside.
1753 (not (tramp-completion-mode-p))
1754 ;; Show only when Emacs has started already.
1756 (let ((enable-recursive-minibuffers t
))
1757 ;; `tramp-error' does not show messages. So we must do it
1759 (apply 'message fmt-string arguments
)
1764 ;; Reset timestamp. It would be wrong after waiting for a while.
1765 (when (tramp-file-name-equal-p vec
(car tramp-current-connection
))
1766 (setcdr tramp-current-connection
(current-time)))))))
1768 ;; We must make it a defun, because it is used earlier already.
1769 (defun tramp-user-error (vec-or-proc fmt-string
&rest arguments
)
1770 "Signal a pilot error."
1773 'tramp-error vec-or-proc
1774 ;; `user-error' has appeared in Emacs 24.3.
1775 (if (fboundp 'user-error
) 'user-error
'error
) fmt-string arguments
)
1777 (when (and tramp-message-show-message
1778 (not (zerop tramp-verbose
))
1779 ;; Do not show when flagged from outside.
1780 (not (tramp-completion-mode-p))
1781 ;; Show only when Emacs has started already.
1783 (let ((enable-recursive-minibuffers t
))
1784 ;; `tramp-error' does not show messages. So we must do it ourselves.
1785 (apply 'message fmt-string arguments
)
1788 ;; Reset timestamp. It would be wrong after waiting for a while.
1790 (tramp-file-name-equal-p vec-or-proc
(car tramp-current-connection
))
1791 (setcdr tramp-current-connection
(current-time)))))))
1793 (defmacro tramp-with-demoted-errors
(vec-or-proc format
&rest body
)
1794 "Execute BODY while redirecting the error message to `tramp-message'.
1795 BODY is executed like wrapped by `with-demoted-errors'. FORMAT
1796 is a format-string containing a %-sequence meaning to substitute
1797 the resulting error message."
1798 (declare (debug (symbolp body
))
1800 (let ((err (make-symbol "err")))
1801 `(condition-case-unless-debug ,err
1803 (error (tramp-message ,vec-or-proc
3 ,format
,err
) nil
))))
1805 (defmacro with-parsed-tramp-file-name
(filename var
&rest body
)
1806 "Parse a Tramp filename and make components available in the body.
1808 First arg FILENAME is evaluated and dissected into its components.
1809 Second arg VAR is a symbol. It is used as a variable name to hold
1810 the filename structure. It is also used as a prefix for the variables
1811 holding the components. For example, if VAR is the symbol `foo', then
1812 `foo' will be bound to the whole structure, `foo-method' will be bound to
1813 the method component, and so on for `foo-user', `foo-domain', `foo-host',
1814 `foo-port', `foo-localname', `foo-hop'.
1816 Remaining args are Lisp expressions to be evaluated (inside an implicit
1819 If VAR is nil, then we bind `v' to the structure and `method', `user',
1820 `domain', `host', `port', `localname', `hop' to the components."
1822 (mapcar (lambda (elem)
1823 `(,(if var
(intern (format "%s-%s" var elem
)) elem
)
1824 (,(intern (format "tramp-file-name-%s" elem
))
1826 `,(tramp-compat-tramp-file-name-slots))))
1827 `(let* ((,(or var
'v
) (tramp-dissect-file-name ,filename
))
1829 ;; We don't know which of those vars will be used, so we bind them all,
1830 ;; and then add here a dummy use of all those variables, so we don't get
1831 ;; flooded by warnings about those vars `body' didn't use.
1832 (ignore ,@(mapcar #'car bindings
))
1835 (put 'with-parsed-tramp-file-name
'lisp-indent-function
2)
1836 (put 'with-parsed-tramp-file-name
'edebug-form-spec
'(form symbolp body
))
1837 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-parsed-tramp-file-name\\>"))
1839 (defun tramp-progress-reporter-update (reporter &optional value
)
1840 "Report progress of an operation for Tramp."
1841 (let* ((parameters (cdr reporter
))
1842 (message (aref parameters
3)))
1843 (when (string-match message
(or (current-message) ""))
1844 (progress-reporter-update reporter value
))))
1846 (defmacro with-tramp-progress-reporter
(vec level message
&rest body
)
1847 "Executes BODY, spinning a progress reporter with MESSAGE.
1848 If LEVEL does not fit for visible messages, there are only traces
1849 without a visible progress reporter."
1850 (declare (indent 3) (debug t
))
1852 (tramp-message ,vec
,level
"%s..." ,message
)
1853 (let ((cookie "failed")
1855 ;; We start a pulsing progress reporter after 3 seconds.
1856 (when (and tramp-message-show-message
1857 ;; Display only when there is a minimum level.
1858 (<= ,level
(min tramp-verbose
3)))
1859 (let ((pr (make-progress-reporter ,message nil nil
)))
1862 3 0.1 #'tramp-progress-reporter-update pr
))))))
1864 ;; Execute the body.
1865 (prog1 (progn ,@body
) (setq cookie
"done"))
1866 ;; Stop progress reporter.
1867 (if tm
(cancel-timer tm
))
1868 (tramp-message ,vec
,level
"%s...%s" ,message cookie
)))))
1870 (font-lock-add-keywords
1871 'emacs-lisp-mode
'("\\<with-tramp-progress-reporter\\>"))
1873 (defmacro with-tramp-file-property
(vec file property
&rest body
)
1874 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
1875 FILE must be a local file name on a connection identified via VEC."
1876 `(if (file-name-absolute-p ,file
)
1877 (let ((value (tramp-get-file-property ,vec
,file
,property
'undef
)))
1878 (when (eq value
'undef
)
1879 ;; We cannot pass @body as parameter to
1880 ;; `tramp-set-file-property' because it mangles our
1882 (setq value
(progn ,@body
))
1883 (tramp-set-file-property ,vec
,file
,property value
))
1887 (put 'with-tramp-file-property
'lisp-indent-function
3)
1888 (put 'with-tramp-file-property
'edebug-form-spec t
)
1889 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-tramp-file-property\\>"))
1891 (defmacro with-tramp-connection-property
(key property
&rest body
)
1892 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
1893 `(let ((value (tramp-get-connection-property ,key
,property
'undef
)))
1894 (when (eq value
'undef
)
1895 ;; We cannot pass ,@body as parameter to
1896 ;; `tramp-set-connection-property' because it mangles our debug
1898 (setq value
(progn ,@body
))
1899 (tramp-set-connection-property ,key
,property value
))
1902 (put 'with-tramp-connection-property
'lisp-indent-function
2)
1903 (put 'with-tramp-connection-property
'edebug-form-spec t
)
1904 (font-lock-add-keywords
1905 'emacs-lisp-mode
'("\\<with-tramp-connection-property\\>"))
1907 (defun tramp-drop-volume-letter (name)
1908 "Cut off unnecessary drive letter from file NAME.
1909 The functions `tramp-*-handle-expand-file-name' call `expand-file-name'
1910 locally on a remote file name. When the local system is a W32 system
1911 but the remote system is Unix, this introduces a superfluous drive
1912 letter into the file name. This function removes it."
1915 (if (tramp-compat-file-name-quoted-p name
)
1916 'tramp-compat-file-name-quote
'identity
)
1917 (let ((name (tramp-compat-file-name-unquote name
)))
1918 (if (string-match "\\`[a-zA-Z]:/" name
)
1919 (replace-match "/" nil t name
)
1922 ;;; Config Manipulation Functions:
1924 ;;;###tramp-autoload
1925 (defun tramp-set-completion-function (method function-list
)
1926 "Sets the list of completion functions for METHOD.
1927 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1928 The FUNCTION is intended to parse FILE according its syntax.
1929 It might be a predefined FUNCTION, or a user defined FUNCTION.
1930 For the list of predefined FUNCTIONs see `tramp-completion-function-alist'.
1934 (tramp-set-completion-function
1936 \\='((tramp-parse-sconfig \"/etc/ssh_config\")
1937 (tramp-parse-sconfig \"~/.ssh/config\")))"
1939 (let ((r function-list
)
1941 (setq tramp-completion-function-alist
1942 (delete (assoc method tramp-completion-function-alist
)
1943 tramp-completion-function-alist
))
1946 ;; Remove double entries.
1947 (when (member (car v
) (cdr v
))
1948 (setcdr v
(delete (car v
) (cdr v
))))
1949 ;; Check for function and file or registry key.
1950 (unless (and (functionp (nth 0 (car v
)))
1952 ;; Windows registry.
1953 ((string-match "^HKEY_CURRENT_USER" (nth 1 (car v
)))
1954 (and (memq system-type
'(cygwin windows-nt
))
1957 v
"reg" nil nil nil
"query" (nth 1 (car v
))))))
1958 ;; Zeroconf service type.
1960 "^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v
))))
1961 ;; Configuration file.
1962 (t (file-exists-p (nth 1 (car v
))))))
1963 (setq r
(delete (car v
) r
)))
1967 (add-to-list 'tramp-completion-function-alist
1970 (defun tramp-get-completion-function (method)
1971 "Returns a list of completion functions for METHOD.
1972 For definition of that list see `tramp-set-completion-function'."
1974 `(;; Default settings are taken into account.
1975 (tramp-parse-default-user-host ,method
)
1976 ;; Hosts visited once shall be remembered.
1977 (tramp-parse-connection-properties ,method
))
1978 ;; The method related defaults.
1979 (cdr (assoc method tramp-completion-function-alist
))))
1982 ;;; Fontification of `read-file-name':
1984 (defvar tramp-rfn-eshadow-overlay
)
1985 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay
)
1987 (defun tramp-rfn-eshadow-setup-minibuffer ()
1988 "Set up a minibuffer for `file-name-shadow-mode'.
1989 Adds another overlay hiding filename parts according to Tramp's
1990 special handling of `substitute-in-file-name'."
1991 (when (symbol-value 'minibuffer-completing-file-name
)
1992 (setq tramp-rfn-eshadow-overlay
1993 (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
1994 ;; Copy rfn-eshadow-overlay properties.
1995 (let ((props (overlay-properties (symbol-value 'rfn-eshadow-overlay
))))
1997 ;; The `field' property prevents correct minibuffer
1998 ;; completion; we exclude it.
1999 (if (not (eq (car props
) 'field
))
2000 (overlay-put tramp-rfn-eshadow-overlay
(pop props
) (pop props
))
2001 (pop props
) (pop props
))))))
2003 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2004 'tramp-rfn-eshadow-setup-minibuffer
)
2005 (add-hook 'tramp-unload-hook
2007 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2008 'tramp-rfn-eshadow-setup-minibuffer
)))
2010 (defun tramp-rfn-eshadow-update-overlay-regexp ()
2011 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format
))
2013 (defun tramp-rfn-eshadow-update-overlay ()
2014 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2015 This is intended to be used as a minibuffer `post-command-hook' for
2016 `file-name-shadow-mode'; the minibuffer should have already
2017 been set up by `rfn-eshadow-setup-minibuffer'."
2018 ;; In remote files name, there is a shadowing just for the local part.
2020 (let ((end (or (overlay-end (symbol-value 'rfn-eshadow-overlay
))
2021 (minibuffer-prompt-end)))
2022 ;; We do not want to send any remote command.
2026 (buffer-substring-no-properties end
(point-max)))
2030 (1+ (or (string-match
2031 (tramp-rfn-eshadow-update-overlay-regexp)
2032 (buffer-string) end
)
2035 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay
)
2036 (rfn-eshadow-update-overlay-hook nil
)
2037 file-name-handler-alist
)
2038 (move-overlay rfn-eshadow-overlay
(point-max) (point-max))
2039 (rfn-eshadow-update-overlay))))))))
2041 (add-hook 'rfn-eshadow-update-overlay-hook
2042 'tramp-rfn-eshadow-update-overlay
)
2043 (add-hook 'tramp-unload-hook
2045 (remove-hook 'rfn-eshadow-update-overlay-hook
2046 'tramp-rfn-eshadow-update-overlay
)))
2048 ;; Inodes don't exist for some file systems. Therefore we must
2049 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2050 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2051 ;; performance isn't the major issue given that file transfer will
2053 (defvar tramp-inodes
0
2054 "Keeps virtual inodes numbers.")
2056 ;; Devices must distinguish physical file systems. The device numbers
2057 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2058 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2059 ;; EFS use device number "-1". In order to be different, we use device number
2060 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2061 (defvar tramp-devices
0
2062 "Keeps virtual device numbers.")
2064 (defun tramp-default-file-modes (filename)
2065 "Return file modes of FILENAME as integer.
2066 If the file modes of FILENAME cannot be determined, return the
2067 value of `default-file-modes', without execute permissions."
2068 (or (file-modes filename
)
2069 (logand (default-file-modes) (string-to-number "0666" 8))))
2071 (defun tramp-replace-environment-variables (filename)
2072 "Replace environment variables in FILENAME.
2073 Return the string with the replaced variables."
2075 ;; Optional arg has been introduced with Emacs 24.4.
2076 (tramp-compat-funcall 'substitute-env-vars filename
'only-defined
))
2077 ;; We need an own implementation.
2079 (let ((idx (string-match "$\\(\\w+\\)" filename
)))
2080 ;; `$' is coded as `$$'.
2082 (or (zerop idx
) (not (eq ?$
(aref filename
(1- idx
)))))
2083 (getenv (match-string 1 filename
)))
2086 (substitute-in-file-name (match-string 0 filename
))
2090 (defun tramp-find-file-name-coding-system-alist (filename tmpname
)
2091 "Like `find-operation-coding-system' for Tramp filenames.
2092 Tramp's `insert-file-contents' and `write-region' work over
2093 temporary file names. If `file-coding-system-alist' contains an
2094 expression, which matches more than the file name suffix, the
2095 coding system might not be determined. This function repairs it."
2097 (dolist (elt file-coding-system-alist
(nreverse result
))
2098 (when (and (consp elt
) (string-match (car elt
) filename
))
2099 ;; We found a matching entry in `file-coding-system-alist'.
2100 ;; So we add a similar entry, but with the temporary file name
2102 (push (cons (regexp-quote tmpname
) (cdr elt
)) result
)))))
2104 (defun tramp-run-real-handler (operation args
)
2105 "Invoke normal file name handler for OPERATION.
2106 First arg specifies the OPERATION, second arg is a list of arguments to
2107 pass to the OPERATION."
2108 (let* ((inhibit-file-name-handlers
2109 `(tramp-file-name-handler
2110 tramp-vc-file-name-handler
2111 tramp-completion-file-name-handler
2112 tramp-archive-file-name-handler
2113 cygwin-mount-name-hook-function
2114 cygwin-mount-map-drive-hook-function
2116 ,(and (eq inhibit-file-name-operation operation
)
2117 inhibit-file-name-handlers
)))
2118 (inhibit-file-name-operation operation
))
2119 (apply operation args
)))
2121 ;; We handle here all file primitives. Most of them have the file
2122 ;; name as first parameter; nevertheless we check for them explicitly
2123 ;; in order to be signaled if a new primitive appears. This
2124 ;; scenario is needed because there isn't a way to decide by
2125 ;; syntactical means whether a foreign method must be called. It would
2126 ;; ease the life if `file-name-handler-alist' would support a decision
2127 ;; function as well but regexp only.
2128 (defun tramp-file-name-for-operation (operation &rest args
)
2129 "Return file name related to OPERATION file primitive.
2130 ARGS are the arguments OPERATION has been called with."
2132 ;; FILE resp DIRECTORY.
2134 '(access-file byte-compiler-base-file-name delete-directory
2135 delete-file diff-latest-backup-file directory-file-name
2136 directory-files directory-files-and-attributes
2137 dired-compress-file dired-uncache file-acl
2138 file-accessible-directory-p file-attributes
2139 file-directory-p file-executable-p file-exists-p
2140 file-local-copy file-modes file-name-as-directory
2141 file-name-directory file-name-nondirectory
2142 file-name-sans-versions file-notify-add-watch
2143 file-ownership-preserved-p file-readable-p
2144 file-regular-p file-remote-p file-selinux-context
2145 file-symlink-p file-truename file-writable-p
2146 find-backup-file-name find-file-noselect get-file-buffer
2147 insert-directory insert-file-contents load
2148 make-directory make-directory-internal set-file-acl
2149 set-file-modes set-file-selinux-context set-file-times
2150 substitute-in-file-name unhandled-file-name-directory
2153 file-name-case-insensitive-p
2156 (if (file-name-absolute-p (nth 0 args
))
2159 ;; FILE DIRECTORY resp FILE1 FILE2.
2161 '(add-name-to-file copy-directory copy-file expand-file-name
2162 file-equal-p file-in-directory-p
2163 file-name-all-completions file-name-completion
2164 ;; Starting with Emacs 26.1, just the 2nd argument of
2165 ;; `make-symbolic-link' matters. For backward
2166 ;; compatibility, we still accept the first argument as
2167 ;; file name to be checked. Handled properly in
2168 ;; `tramp-handle-*-make-symbolic-link'.
2169 file-newer-than-file-p make-symbolic-link rename-file
))
2172 ((tramp-tramp-file-p (nth 0 args
)) (nth 0 args
))
2173 ((tramp-tramp-file-p (nth 1 args
)) (nth 1 args
))
2174 (t default-directory
))))
2176 ((eq operation
'write-region
)
2177 (if (file-name-absolute-p (nth 2 args
))
2182 '(make-auto-save-file-name
2183 set-visited-file-modtime verify-visited-file-modtime
))
2185 (if (bufferp (nth 0 args
)) (nth 0 args
) (current-buffer))))
2188 '(process-file shell-command start-file-process
2190 make-nearby-temp-file temporary-file-directory
))
2194 '(file-notify-rm-watch
2196 file-notify-valid-p
))
2197 (when (processp (nth 0 args
))
2198 (with-current-buffer (process-buffer (nth 0 args
))
2199 default-directory
)))
2200 ;; Unknown file primitive.
2201 (t (error "unknown file I/O primitive: %s" operation
))))
2203 (defun tramp-find-foreign-file-name-handler (filename &optional _operation
)
2204 "Return foreign file name handler if exists."
2205 (when (tramp-tramp-file-p filename
)
2206 (let ((handler tramp-foreign-file-name-handler-alist
)
2209 (setq elt
(car handler
)
2210 handler
(cdr handler
))
2211 (when (funcall (car elt
) filename
)
2216 (defvar tramp-debug-on-error nil
2217 "Like `debug-on-error' but used Tramp internal.")
2219 (defmacro tramp-condition-case-unless-debug
2220 (var bodyform
&rest handlers
)
2221 "Like `condition-case-unless-debug' but `tramp-debug-on-error'."
2222 `(let ((debug-on-error tramp-debug-on-error
))
2223 (condition-case-unless-debug ,var
,bodyform
,@handlers
)))
2225 ;; In Emacs, there is some concurrency due to timers. If a timer
2226 ;; interrupts Tramp and wishes to use the same connection buffer as
2227 ;; the "main" Emacs, then garbage might occur in the connection
2228 ;; buffer. Therefore, we need to make sure that a timer does not use
2229 ;; the same connection buffer as the "main" Emacs. We implement a
2230 ;; cheap global lock, instead of locking each connection buffer
2231 ;; separately. The global lock is based on two variables,
2232 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
2233 ;; (with setq) to indicate a lock. But Tramp also calls itself during
2234 ;; processing of a single file operation, so we need to allow
2235 ;; recursive calls. That's where the `tramp-locker' variable comes in
2236 ;; -- it is let-bound to t during the execution of the current
2237 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
2238 ;; then we should just proceed because we have been called
2239 ;; recursively. But if `tramp-locker' is nil, then we are a timer
2240 ;; interrupting the "main" Emacs, and then we signal an error.
2242 (defvar tramp-locked nil
2243 "If non-nil, then Tramp is currently busy.
2244 Together with `tramp-locker', this implements a locking mechanism
2245 preventing reentrant calls of Tramp.")
2247 (defvar tramp-locker nil
2248 "If non-nil, then a caller has locked Tramp.
2249 Together with `tramp-locked', this implements a locking mechanism
2250 preventing reentrant calls of Tramp.")
2253 (defun tramp-file-name-handler (operation &rest args
)
2254 "Invoke Tramp file name handler.
2255 Falls back to normal file name handler if no Tramp file name handler exists."
2256 (let ((filename (apply 'tramp-file-name-for-operation operation args
)))
2257 (if (and tramp-mode
(tramp-tramp-file-p filename
))
2259 (setq filename
(tramp-replace-environment-variables filename
))
2260 (with-parsed-tramp-file-name filename nil
2261 (let ((completion (tramp-completion-mode-p))
2263 (tramp-find-foreign-file-name-handler filename operation
))
2265 ;; Call the backend function.
2267 (tramp-condition-case-unless-debug err
2268 (let ((sf (symbol-function foreign
)))
2269 ;; Some packages set the default directory to a
2270 ;; remote path, before respective Tramp packages
2271 ;; are already loaded. This results in
2272 ;; recursive loading. Therefore, we load the
2273 ;; Tramp packages locally.
2274 (when (autoloadp sf
)
2275 (let ((default-directory
2276 (tramp-compat-temporary-file-directory)))
2277 (load (cadr sf
) 'noerror
'nomessage
)))
2279 ;; v 4 "Running `%s'..." (cons operation args))
2280 ;; If `non-essential' is non-nil, Tramp shall
2281 ;; not open a new connection.
2282 ;; If Tramp detects that it shouldn't continue
2283 ;; to work, it throws the `suppress' event.
2284 ;; This could happen for example, when Tramp
2285 ;; tries to open the same connection twice in a
2286 ;; short time frame.
2287 ;; In both cases, we try the default handler then.
2289 (catch 'non-essential
2291 (when (and tramp-locked
(not tramp-locker
))
2292 (setq tramp-locked nil
)
2294 (car-safe tramp-current-connection
)
2296 "Forbidden reentrant call of Tramp"))
2297 (let ((tl tramp-locked
))
2298 (setq tramp-locked t
)
2300 (let ((tramp-locker t
))
2301 (apply foreign operation args
))
2302 (setq tramp-locked tl
))))))
2304 ;; v 4 "Running `%s'...`%s'" (cons operation args) result)
2306 ((eq result
'non-essential
)
2308 v
5 "Non-essential received in operation %s"
2309 (cons operation args
))
2310 (tramp-run-real-handler operation args
))
2311 ((eq result
'suppress
)
2312 (let (tramp-message-show-message)
2314 v
1 "Suppress received in operation %s"
2315 (cons operation args
))
2316 (tramp-cleanup-connection v t
)
2317 (tramp-run-real-handler operation args
)))
2320 ;; Trace that somebody has interrupted the operation.
2322 (let (tramp-message-show-message)
2324 v
1 "Interrupt received in operation %s"
2325 (cons operation args
)))
2326 ;; Propagate the quit signal.
2327 (signal (car err
) (cdr err
)))
2329 ;; When we are in completion mode, some failed
2330 ;; operations shall return at least a default
2331 ;; value in order to give the user a chance to
2332 ;; correct the file name in the minibuffer.
2333 ;; In order to get a full backtrace, one could apply
2334 ;; (setq tramp-debug-on-error t)
2337 ((and completion
(zerop (length localname
))
2338 (memq operation
'(file-exists-p file-directory-p
)))
2340 ((and completion
(zerop (length localname
))
2342 '(expand-file-name file-name-as-directory
)))
2344 ;; Propagate the error.
2345 (t (signal (car err
) (cdr err
))))))
2347 ;; Nothing to do for us. However, since we are in
2348 ;; `tramp-mode', we must suppress the volume letter on
2350 (setq result
(tramp-run-real-handler operation args
))
2351 (if (stringp result
)
2352 (tramp-drop-volume-letter result
)
2355 ;; When `tramp-mode' is not enabled, or the file name is quoted,
2356 ;; we don't do anything.
2357 (tramp-run-real-handler operation args
))))
2359 (defun tramp-completion-file-name-handler (operation &rest args
)
2360 "Invoke Tramp file name completion handler.
2361 Falls back to normal file name handler if no Tramp file name handler exists."
2362 (let ((fn (assoc operation tramp-completion-file-name-handler-alist
)))
2363 (if (and fn tramp-mode
)
2364 (save-match-data (apply (cdr fn
) args
))
2365 (tramp-run-real-handler operation args
))))
2368 (progn (defun tramp-autoload-file-name-handler (operation &rest args
)
2369 "Load Tramp file name handler, and perform OPERATION."
2371 (let ((default-directory temporary-file-directory
))
2372 (load "tramp" 'noerror
'nomessage
))
2373 (tramp-unload-file-name-handlers))
2374 (apply operation args
)))
2376 ;; `tramp-autoload-file-name-handler' must be registered before
2377 ;; evaluation of site-start and init files, because there might exist
2378 ;; remote files already, f.e. files kept via recentf-mode.
2380 (progn (defun tramp-register-autoload-file-name-handlers ()
2381 "Add Tramp file name handlers to `file-name-handler-alist' during autoload."
2382 (add-to-list 'file-name-handler-alist
2383 (cons tramp-autoload-file-name-regexp
2384 'tramp-autoload-file-name-handler
))
2385 (put 'tramp-autoload-file-name-handler
'safe-magic t
)))
2387 ;;;###autoload (tramp-register-autoload-file-name-handlers)
2389 (defun tramp-use-absolute-autoload-file-names ()
2390 "Change Tramp autoload objects to use absolute file names.
2391 This avoids problems during autoload, when `load-path' contains
2393 ;; We expect all other Tramp files in the same directory as tramp.el.
2394 (let* ((dir (expand-file-name (file-name-directory (locate-library "tramp"))))
2400 'file-name-sans-extension
2401 (directory-files dir nil
"^tramp.+\\.elc?$"))
2405 (when (and (functionp atom
)
2406 (autoloadp (symbol-function atom
))
2407 (string-match files-regexp
(cadr (symbol-function atom
))))
2409 (setf (cadr (symbol-function atom
))
2410 (expand-file-name (cadr (symbol-function atom
)) dir
))))))))
2412 (eval-after-load 'tramp
(tramp-use-absolute-autoload-file-names))
2414 (defun tramp-register-file-name-handlers ()
2415 "Add Tramp file name handlers to `file-name-handler-alist'."
2416 ;; Remove autoloaded handlers from file name handler alist. Useful,
2417 ;; if `tramp-syntax' has been changed. We cannot call
2418 ;; `tramp-unload-file-name-handlers', this would result in recursive
2419 ;; loading of Tramp.
2420 (dolist (fnh '(tramp-file-name-handler
2421 tramp-completion-file-name-handler
2422 tramp-archive-file-name-handler
2423 tramp-autoload-file-name-handler
))
2424 (let ((a1 (rassq fnh file-name-handler-alist
)))
2425 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
))))
2427 ;; Add the handlers. We do not add anything to the `operations'
2428 ;; property of `tramp-file-name-handler' and
2429 ;; `tramp-archive-file-name-handler', this shall be done by the
2430 ;; respective foreign handlers.
2431 (add-to-list 'file-name-handler-alist
2432 (cons tramp-file-name-regexp
'tramp-file-name-handler
))
2433 (put 'tramp-file-name-handler
'safe-magic t
)
2435 (add-to-list 'file-name-handler-alist
2436 (cons tramp-completion-file-name-regexp
2437 'tramp-completion-file-name-handler
))
2438 (put 'tramp-completion-file-name-handler
'safe-magic t
)
2439 ;; Mark `operations' the handler is responsible for.
2440 (put 'tramp-completion-file-name-handler
'operations
2441 (mapcar 'car tramp-completion-file-name-handler-alist
))
2443 (when (bound-and-true-p tramp-archive-enabled
)
2444 (add-to-list 'file-name-handler-alist
2445 (cons tramp-archive-file-name-regexp
2446 'tramp-archive-file-name-handler
))
2447 (put 'tramp-archive-file-name-handler
'safe-magic t
))
2449 ;; If jka-compr or epa-file are already loaded, move them to the
2450 ;; front of `file-name-handler-alist'.
2451 (dolist (fnh '(epa-file-handler jka-compr-handler
))
2452 (let ((entry (rassoc fnh file-name-handler-alist
)))
2454 (setq file-name-handler-alist
2455 (cons entry
(delete entry file-name-handler-alist
)))))))
2457 (eval-after-load 'tramp
(tramp-register-file-name-handlers))
2459 ;;;###tramp-autoload
2460 (progn (defun tramp-register-foreign-file-name-handler
2461 (func handler
&optional append
)
2462 "Register (FUNC . HANDLER) in `tramp-foreign-file-name-handler-alist'.
2463 FUNC is the function, which determines whether HANDLER is to be called.
2464 Add operations defined in `HANDLER-alist' to `tramp-file-name-handler'."
2466 'tramp-foreign-file-name-handler-alist
`(,func .
,handler
) append
)
2467 ;; Mark `operations' the handler is responsible for.
2468 (put 'tramp-file-name-handler
2472 (get 'tramp-file-name-handler
'operations
)
2475 (symbol-value (intern (concat (symbol-name handler
) "-alist")))))))))
2477 (defun tramp-exists-file-name-handler (operation &rest args
)
2478 "Check, whether OPERATION runs a file name handler."
2479 ;; The file name handler is determined on base of either an
2480 ;; argument, `buffer-file-name', or `default-directory'.
2482 (let* ((buffer-file-name "/")
2483 (default-directory "/")
2484 (fnha file-name-handler-alist
)
2485 (check-file-name-operation operation
)
2486 (file-name-handler-alist
2489 (lambda (operation &rest args
)
2490 "Returns OPERATION if it is the one to be checked."
2491 (if (equal check-file-name-operation operation
)
2493 (let ((file-name-handler-alist fnha
))
2494 (apply operation args
))))))))
2495 (equal (apply operation args
) operation
))))
2498 (progn (defun tramp-unload-file-name-handlers ()
2499 "Unload Tramp file name handlers from `file-name-handler-alist'."
2500 (dolist (fnh '(tramp-file-name-handler
2501 tramp-completion-file-name-handler
2502 tramp-archive-file-name-handler
2503 tramp-autoload-file-name-handler
))
2504 (let ((a1 (rassq fnh file-name-handler-alist
)))
2505 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
))))))
2507 (add-hook 'tramp-unload-hook
'tramp-unload-file-name-handlers
)
2509 ;;; File name handler functions for completion mode:
2512 (defvar tramp-completion-mode nil
2513 "If non-nil, external packages signal that they are in file name completion.")
2514 (make-obsolete-variable 'tramp-completion-mode
'non-essential
"26.1")
2516 (defun tramp-completion-mode-p ()
2517 "Check, whether method / user name / host name completion is active."
2519 ;; Signal from outside.
2521 ;; This variable has been obsoleted in Emacs 26.
2522 tramp-completion-mode
))
2524 (defun tramp-connectable-p (filename)
2525 "Check, whether it is possible to connect the remote host w/o side-effects.
2526 This is true, if either the remote host is already connected, or if we are
2527 not in completion mode."
2528 (let (tramp-verbose)
2529 (and (tramp-tramp-file-p filename
)
2530 (or (not (tramp-completion-mode-p))
2532 (tramp-get-connection-process
2533 (tramp-dissect-file-name filename
)))))))
2535 ;; Method, host name and user name completion.
2536 ;; `tramp-completion-dissect-file-name' returns a list of
2537 ;; `tramp-file-name' structures. For all of them we return possible
2539 (defun tramp-completion-handle-file-name-all-completions (filename directory
)
2540 "Like `file-name-all-completions' for partial Tramp files."
2543 (tramp-drop-volume-letter (expand-file-name filename directory
)))
2546 ;; Suppress hop from completion.
2550 "\\(" "\\(" tramp-remote-file-name-spec-regexp
2551 tramp-postfix-hop-regexp
2554 (setq hop
(match-string 1 fullname
)
2555 fullname
(replace-match "" nil nil fullname
1)))
2557 ;; Possible completion structures.
2558 (dolist (elt (tramp-completion-dissect-file-name fullname
))
2559 (let* ((method (tramp-file-name-method elt
))
2560 (user (tramp-file-name-user elt
))
2561 (host (tramp-file-name-host elt
))
2562 (localname (tramp-file-name-localname elt
))
2563 (m (tramp-find-method method user host
))
2566 (unless localname
;; Nothing to complete.
2570 ;; Method dependent user / host combinations.
2574 (setq all-user-hosts
2575 (append all-user-hosts
2576 (funcall (nth 0 x
) (nth 1 x
)))))
2577 (tramp-get-completion-function m
))
2583 (tramp-get-completion-user-host
2584 method user host
(nth 0 x
) (nth 1 x
)))
2585 (delq nil all-user-hosts
)))))
2587 ;; Possible methods.
2589 (append result
(tramp-get-completion-methods m
)))))))
2591 ;; Unify list, add hop, remove nil elements.
2592 (dolist (elt result
)
2594 (string-match tramp-prefix-regexp elt
)
2595 (setq elt
(replace-match (concat tramp-prefix-format hop
) nil nil elt
))
2597 (substring elt
(length (tramp-drop-volume-letter directory
)))
2600 ;; Complete local parts.
2604 (tramp-run-real-handler
2605 'file-name-all-completions
(list filename directory
))))))
2607 ;; Method, host name and user name completion for a file.
2608 (defun tramp-completion-handle-file-name-completion
2609 (filename directory
&optional predicate
)
2610 "Like `file-name-completion' for Tramp files."
2613 (mapcar 'list
(file-name-all-completions filename directory
))
2614 (when (and predicate
2615 (tramp-connectable-p (expand-file-name filename directory
)))
2616 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
2618 ;; I misuse a little bit the `tramp-file-name' structure in order to
2619 ;; handle completion possibilities for partial methods / user names /
2620 ;; host names. Return value is a list of `tramp-file-name' structures
2621 ;; according to possible completions. If "localname" is non-nil it
2622 ;; means there shouldn't be a completion anymore.
2624 ;; Expected results:
2627 ;; ["x" nil nil nil]
2629 ;; "/x:" "/[x/" "/x:y" "/[x/y" "/x:y:" "/[x/y]"
2630 ;; ["x" nil "" nil] ["x" nil "y" nil] ["x" nil "y" ""]
2631 ;; ["x" "" nil nil] ["x" "y" nil nil]
2633 ;; "/x:y@""/[x/y@" "/x:y@z" "/[x/y@z" "/x:y@z:" "/[x/y@z]"
2634 ;;["x" "y" nil nil] ["x" "y" "z" nil] ["x" "y" "z" ""]
2635 (defun tramp-completion-dissect-file-name (name)
2636 "Returns a list of `tramp-file-name' structures.
2637 They are collected by `tramp-completion-dissect-file-name1'."
2639 (let* ((x-nil "\\|\\(\\)")
2640 (tramp-completion-ipv6-regexp
2643 (if (zerop (length tramp-postfix-ipv6-format
))
2644 tramp-postfix-host-format
2645 tramp-postfix-ipv6-format
)))
2646 ;; "/method" "/[method"
2647 (tramp-completion-file-name-structure1
2651 "\\(" tramp-method-regexp x-nil
"\\)$")
2653 ;; "/method:user" "/[method/user"
2654 (tramp-completion-file-name-structure2
2658 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2659 "\\(" tramp-user-regexp x-nil
"\\)$")
2661 ;; "/method:host" "/[method/host"
2662 (tramp-completion-file-name-structure3
2666 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2667 "\\(" tramp-host-regexp x-nil
"\\)$")
2669 ;; "/method:[ipv6" "/[method/ipv6"
2670 (tramp-completion-file-name-structure4
2674 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2675 tramp-prefix-ipv6-regexp
2676 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
2678 ;; "/method:user@host" "/[method/user@host"
2679 (tramp-completion-file-name-structure5
2683 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2684 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
2685 "\\(" tramp-host-regexp x-nil
"\\)$")
2687 ;; "/method:user@[ipv6" "/[method/user@ipv6"
2688 (tramp-completion-file-name-structure6
2692 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2693 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
2694 tramp-prefix-ipv6-regexp
2695 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
2700 (lambda (structure) (tramp-completion-dissect-file-name1 structure name
))
2702 tramp-completion-file-name-structure1
2703 tramp-completion-file-name-structure2
2704 tramp-completion-file-name-structure3
2705 tramp-completion-file-name-structure4
2706 tramp-completion-file-name-structure5
2707 tramp-completion-file-name-structure6
)))))
2709 (defun tramp-completion-dissect-file-name1 (structure name
)
2710 "Returns a `tramp-file-name' structure matching STRUCTURE.
2711 The structure consists of remote method, remote user,
2712 remote host and localname (filename on remote host)."
2715 (when (string-match (nth 0 structure
) name
)
2716 (make-tramp-file-name
2717 :method
(and (nth 1 structure
)
2718 (match-string (nth 1 structure
) name
))
2719 :user
(and (nth 2 structure
)
2720 (match-string (nth 2 structure
) name
))
2721 :host
(and (nth 3 structure
)
2722 (match-string (nth 3 structure
) name
))))))
2724 ;; This function returns all possible method completions, adding the
2725 ;; trailing method delimiter.
2726 (defun tramp-get-completion-methods (partial-method)
2727 "Returns all method completions for PARTIAL-METHOD."
2731 (string-match (concat "^" (regexp-quote partial-method
)) method
)
2732 (tramp-completion-make-tramp-file-name method nil nil nil
)))
2733 (mapcar 'car tramp-methods
)))
2735 ;; Compares partial user and host names with possible completions.
2736 (defun tramp-get-completion-user-host
2737 (method partial-user partial-host user host
)
2738 "Returns the most expanded string for user and host name completion.
2739 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
2742 ((and partial-user partial-host
)
2744 (string-match (concat "^" (regexp-quote partial-host
)) host
)
2745 (string-equal partial-user
(or user partial-user
)))
2746 (setq user partial-user
)
2753 (and user
(string-match (concat "^" (regexp-quote partial-user
)) user
))
2759 (and host
(string-match (concat "^" (regexp-quote partial-host
)) host
))
2765 (unless (zerop (+ (length user
) (length host
)))
2766 (tramp-completion-make-tramp-file-name method user host nil
)))
2768 (defun tramp-parse-default-user-host (method)
2769 "Return a list of (user host) tuples allowed to access for METHOD.
2770 This function is added always in `tramp-get-completion-function'
2771 for all methods. Resulting data are derived from default settings."
2772 `((,(tramp-find-user method nil nil
) ,(tramp-find-host method nil nil
))))
2774 ;; Generic function.
2775 (defun tramp-parse-group (regexp match-level skip-regexp
)
2776 "Return a (user host) tuple allowed to access.
2777 User is always nil."
2779 (when (re-search-forward regexp
(point-at-eol) t
)
2780 (setq result
(list nil
(match-string match-level
))))
2782 (> (skip-chars-forward skip-regexp
) 0)
2786 ;; Generic function.
2787 (defun tramp-parse-file (filename function
)
2788 "Return a list of (user host) tuples allowed to access.
2789 User is always nil."
2790 ;; On Windows, there are problems in completion when
2791 ;; `default-directory' is remote.
2792 (let ((default-directory (tramp-compat-temporary-file-directory)))
2793 (when (file-readable-p filename
)
2795 (insert-file-contents filename
)
2796 (goto-char (point-min))
2797 (cl-loop while
(not (eobp)) collect
(funcall function
))))))
2799 ;;;###tramp-autoload
2800 (defun tramp-parse-rhosts (filename)
2801 "Return a list of (user host) tuples allowed to access.
2802 Either user or host may be nil."
2803 (tramp-parse-file filename
'tramp-parse-rhosts-group
))
2805 (defun tramp-parse-rhosts-group ()
2806 "Return a (user host) tuple allowed to access.
2807 Either user or host may be nil."
2811 "^\\(" tramp-host-regexp
"\\)"
2812 "\\([ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
2813 (when (re-search-forward regexp
(point-at-eol) t
)
2814 (setq result
(append (list (match-string 3) (match-string 1)))))
2818 ;;;###tramp-autoload
2819 (defun tramp-parse-shosts (filename)
2820 "Return a list of (user host) tuples allowed to access.
2821 User is always nil."
2822 (tramp-parse-file filename
'tramp-parse-shosts-group
))
2824 (defun tramp-parse-shosts-group ()
2825 "Return a (user host) tuple allowed to access.
2826 User is always nil."
2827 (tramp-parse-group (concat "^\\(" tramp-host-regexp
"\\)") 1 ","))
2829 ;;;###tramp-autoload
2830 (defun tramp-parse-sconfig (filename)
2831 "Return a list of (user host) tuples allowed to access.
2832 User is always nil."
2833 (tramp-parse-file filename
'tramp-parse-sconfig-group
))
2835 (defun tramp-parse-sconfig-group ()
2836 "Return a (user host) tuple allowed to access.
2837 User is always nil."
2839 (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp
"\\)") 1 ","))
2841 ;; Generic function.
2842 (defun tramp-parse-shostkeys-sknownhosts (dirname regexp
)
2843 "Return a list of (user host) tuples allowed to access.
2844 User is always nil."
2845 ;; On Windows, there are problems in completion when
2846 ;; `default-directory' is remote.
2847 (let* ((default-directory (tramp-compat-temporary-file-directory))
2848 (files (and (file-directory-p dirname
) (directory-files dirname
))))
2851 when
(and (not (string-match "^\\.\\.?$" f
)) (string-match regexp f
))
2852 collect
(list nil
(match-string 1 f
)))))
2854 ;;;###tramp-autoload
2855 (defun tramp-parse-shostkeys (dirname)
2856 "Return a list of (user host) tuples allowed to access.
2857 User is always nil."
2858 (tramp-parse-shostkeys-sknownhosts
2859 dirname
(concat "^key_[0-9]+_\\(" tramp-host-regexp
"\\)\\.pub$")))
2861 ;;;###tramp-autoload
2862 (defun tramp-parse-sknownhosts (dirname)
2863 "Return a list of (user host) tuples allowed to access.
2864 User is always nil."
2865 (tramp-parse-shostkeys-sknownhosts
2867 (concat "^\\(" tramp-host-regexp
"\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")))
2869 ;;;###tramp-autoload
2870 (defun tramp-parse-hosts (filename)
2871 "Return a list of (user host) tuples allowed to access.
2872 User is always nil."
2873 (tramp-parse-file filename
'tramp-parse-hosts-group
))
2875 (defun tramp-parse-hosts-group ()
2876 "Return a (user host) tuple allowed to access.
2877 User is always nil."
2879 (concat "^\\(" tramp-ipv6-regexp
"\\|" tramp-host-regexp
"\\)") 1 " \t"))
2881 ;;;###tramp-autoload
2882 (defun tramp-parse-passwd (filename)
2883 "Return a list of (user host) tuples allowed to access.
2884 Host is always \"localhost\"."
2885 (with-tramp-connection-property nil
"parse-passwd"
2886 (if (executable-find "getent")
2888 (when (zerop (tramp-call-process nil
"getent" nil t nil
"passwd"))
2889 (goto-char (point-min))
2890 (cl-loop while
(not (eobp)) collect
2891 (tramp-parse-etc-group-group))))
2892 (tramp-parse-file filename
'tramp-parse-passwd-group
))))
2894 (defun tramp-parse-passwd-group ()
2895 "Return a (user host) tuple allowed to access.
2896 Host is always \"localhost\"."
2898 (regexp (concat "^\\(" tramp-user-regexp
"\\):")))
2899 (when (re-search-forward regexp
(point-at-eol) t
)
2900 (setq result
(list (match-string 1) "localhost")))
2904 ;;;###tramp-autoload
2905 (defun tramp-parse-etc-group (filename)
2906 "Return a list of (group host) tuples allowed to access.
2907 Host is always \"localhost\"."
2908 (with-tramp-connection-property nil
"parse-group"
2909 (if (executable-find "getent")
2911 (when (zerop (tramp-call-process nil
"getent" nil t nil
"group"))
2912 (goto-char (point-min))
2913 (cl-loop while
(not (eobp)) collect
2914 (tramp-parse-etc-group-group))))
2915 (tramp-parse-file filename
'tramp-parse-etc-group-group
))))
2917 (defun tramp-parse-etc-group-group ()
2918 "Return a (group host) tuple allowed to access.
2919 Host is always \"localhost\"."
2921 (split (split-string (buffer-substring (point) (point-at-eol)) ":")))
2922 (when (member (user-login-name) (split-string (nth 3 split
) "," 'omit
))
2923 (setq result
(list (nth 0 split
) "localhost")))
2927 ;;;###tramp-autoload
2928 (defun tramp-parse-netrc (filename)
2929 "Return a list of (user host) tuples allowed to access.
2931 (tramp-parse-file filename
'tramp-parse-netrc-group
))
2933 (defun tramp-parse-netrc-group ()
2934 "Return a (user host) tuple allowed to access.
2939 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp
"\\)"
2940 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
2941 (when (re-search-forward regexp
(point-at-eol) t
)
2942 (setq result
(list (match-string 3) (match-string 1))))
2946 ;;;###tramp-autoload
2947 (defun tramp-parse-putty (registry-or-dirname)
2948 "Return a list of (user host) tuples allowed to access.
2949 User is always nil."
2950 (if (memq system-type
'(windows-nt))
2951 (with-tramp-connection-property nil
"parse-putty"
2953 (when (zerop (tramp-call-process
2954 nil
"reg" nil t nil
"query" registry-or-dirname
))
2955 (goto-char (point-min))
2956 (cl-loop while
(not (eobp)) collect
2957 (tramp-parse-putty-group registry-or-dirname
)))))
2959 (tramp-parse-shostkeys-sknownhosts
2960 registry-or-dirname
(concat "^\\(" tramp-host-regexp
"\\)$"))))
2962 (defun tramp-parse-putty-group (registry)
2963 "Return a (user host) tuple allowed to access.
2964 User is always nil."
2966 (regexp (concat (regexp-quote registry
) "\\\\\\(.+\\)")))
2967 (when (re-search-forward regexp
(point-at-eol) t
)
2968 (setq result
(list nil
(match-string 1))))
2972 ;;; Common file name handler functions for different backends:
2974 (defvar tramp-handle-file-local-copy-hook nil
2975 "Normal hook to be run at the end of `tramp-*-handle-file-local-copy'.")
2977 (defvar tramp-handle-write-region-hook nil
2978 "Normal hook to be run at the end of `tramp-*-handle-write-region'.")
2980 (defun tramp-handle-add-name-to-file
2981 (filename newname
&optional ok-if-already-exists
)
2982 "Like `add-name-to-file' for Tramp files."
2983 (with-parsed-tramp-file-name
2984 (if (tramp-tramp-file-p newname
) newname filename
) nil
2985 (unless (tramp-equal-remote filename newname
)
2988 "add-name-to-file: %s"
2989 "only implemented for same method, same user, same host"))
2990 ;; Do the 'confirm if exists' thing.
2991 (when (file-exists-p newname
)
2993 (if (or (null ok-if-already-exists
) ; not allowed to exist
2994 (and (numberp ok-if-already-exists
)
2997 "File %s already exists; make it a link anyway? "
2999 (tramp-error v
'file-already-exists newname
)
3000 (delete-file newname
)))
3001 (tramp-flush-file-properties v
(file-name-directory localname
))
3002 (tramp-flush-file-properties v localname
)
3004 filename newname
'ok-if-already-exists
'keep-time
3005 'preserve-uid-gid
'preserve-permissions
)))
3007 (defun tramp-handle-directory-file-name (directory)
3008 "Like `directory-file-name' for Tramp files."
3009 ;; If localname component of filename is "/", leave it unchanged.
3010 ;; Otherwise, remove any trailing slash from localname component.
3011 ;; Method, host, etc, are unchanged.
3012 (while (with-parsed-tramp-file-name directory nil
3013 (and (not (zerop (length localname
)))
3014 (eq (aref localname
(1- (length localname
))) ?
/)
3015 (not (string= localname
"/"))))
3016 (setq directory
(substring directory
0 -
1)))
3019 (defun tramp-handle-directory-files (directory &optional full match nosort
)
3020 "Like `directory-files' for Tramp files."
3021 (when (file-directory-p directory
)
3022 (setq directory
(file-name-as-directory (expand-file-name directory
)))
3023 (let ((temp (nreverse (file-name-all-completions "" directory
)))
3027 (setq item
(directory-file-name (pop temp
)))
3028 (when (or (null match
) (string-match match item
))
3029 (push (if full
(concat directory item
) item
)
3031 (if nosort result
(sort result
'string
<)))))
3033 (defun tramp-handle-directory-files-and-attributes
3034 (directory &optional full match nosort id-format
)
3035 "Like `directory-files-and-attributes' for Tramp files."
3038 (cons x
(file-attributes
3039 (if full x
(expand-file-name x directory
)) id-format
)))
3040 (directory-files directory full match nosort
)))
3042 (defun tramp-handle-dired-uncache (dir)
3043 "Like `dired-uncache' for Tramp files."
3044 (with-parsed-tramp-file-name
3045 (if (file-directory-p dir
) dir
(file-name-directory dir
)) nil
3046 (tramp-flush-directory-properties v localname
)))
3048 (defun tramp-handle-file-accessible-directory-p (filename)
3049 "Like `file-accessible-directory-p' for Tramp files."
3050 (and (file-directory-p filename
)
3051 (file-readable-p filename
)))
3053 (defun tramp-handle-file-directory-p (filename)
3054 "Like `file-directory-p' for Tramp files."
3055 (eq (tramp-compat-file-attribute-type
3056 (file-attributes (file-truename filename
)))
3059 (defun tramp-handle-file-equal-p (filename1 filename2
)
3060 "Like `file-equalp-p' for Tramp files."
3061 ;; Native `file-equalp-p' calls `file-truename', which requires a
3062 ;; remote connection. This can be avoided, if FILENAME1 and
3063 ;; FILENAME2 are not located on the same remote host.
3065 (file-remote-p (expand-file-name filename1
))
3066 (file-remote-p (expand-file-name filename2
)))
3067 (tramp-run-real-handler 'file-equal-p
(list filename1 filename2
))))
3069 (defun tramp-handle-file-exists-p (filename)
3070 "Like `file-exists-p' for Tramp files."
3071 (not (null (file-attributes filename
))))
3073 (defun tramp-handle-file-in-directory-p (filename directory
)
3074 "Like `file-in-directory-p' for Tramp files."
3075 ;; Native `file-in-directory-p' calls `file-truename', which
3076 ;; requires a remote connection. This can be avoided, if FILENAME
3077 ;; and DIRECTORY are not located on the same remote host.
3079 (file-remote-p (expand-file-name filename
))
3080 (file-remote-p (expand-file-name directory
)))
3081 (tramp-run-real-handler 'file-in-directory-p
(list filename directory
))))
3083 (defun tramp-handle-file-modes (filename)
3084 "Like `file-modes' for Tramp files."
3085 (let ((truename (or (file-truename filename
) filename
)))
3086 (when (file-exists-p truename
)
3087 (tramp-mode-string-to-int
3088 (tramp-compat-file-attribute-modes (file-attributes truename
))))))
3090 ;; Localname manipulation functions that grok Tramp localnames...
3091 (defun tramp-handle-file-name-as-directory (file)
3092 "Like `file-name-as-directory' but aware of Tramp files."
3093 ;; `file-name-as-directory' would be sufficient except localname is
3094 ;; the empty string.
3095 (let ((v (tramp-dissect-file-name file t
)))
3096 ;; Run the command on the localname portion only unless we are in
3098 (tramp-make-tramp-file-name
3099 v
(unless (and (zerop (length (tramp-file-name-localname v
)))
3100 (not (tramp-connectable-p file
)))
3101 (tramp-run-real-handler
3102 'file-name-as-directory
3103 (list (or (tramp-file-name-localname v
) "")))))))
3105 (defun tramp-handle-file-name-case-insensitive-p (filename)
3106 "Like `file-name-case-insensitive-p' for Tramp files."
3107 ;; We make it a connection property, assuming that all file systems
3108 ;; on the remote host behave similar. This might be wrong for
3109 ;; mounted NFS directories or SMB/AFP shares; such more granular
3110 ;; tests will be added in case they are needed.
3111 (setq filename
(expand-file-name filename
))
3112 (with-parsed-tramp-file-name filename nil
3113 (or ;; Maybe there is a default value.
3114 (tramp-get-method-parameter v
'tramp-case-insensitive
)
3116 ;; There isn't. So we must check, in case there's a connection already.
3117 (and (file-remote-p filename nil
'connected
)
3118 (with-tramp-connection-property v
"case-insensitive"
3120 (with-tramp-progress-reporter v
5 "Checking case-insensitive"
3121 ;; The idea is to compare a file with lower case
3122 ;; letters with the same file with upper case letters.
3124 (tramp-compat-file-name-unquote
3125 (directory-file-name filename
)))
3127 ;; Check, whether we find an existing file with
3128 ;; lower case letters. This avoids us to create a
3130 (while (and (string-match
3131 "[a-z]" (file-remote-p candidate
'localname
))
3132 (not (file-exists-p candidate
)))
3134 (directory-file-name
3135 (file-name-directory candidate
))))
3136 ;; Nothing found, so we must use a temporary file
3137 ;; for comparison. `make-nearby-temp-file' is added
3138 ;; to Emacs 26+ like `file-name-case-insensitive-p',
3139 ;; so there is no compatibility problem calling it.
3142 "[a-z]" (file-remote-p candidate
'localname
))
3144 (let ((default-directory
3145 (file-name-directory filename
)))
3146 (tramp-compat-funcall
3147 'make-nearby-temp-file
"tramp."))
3149 ;; Check for the existence of the same file with
3150 ;; upper case letters.
3154 (file-remote-p candidate
)
3155 (upcase (file-remote-p candidate
'localname
))))
3157 (when tmpfile
(delete-file tmpfile
)))))))))))
3159 (defun tramp-handle-file-name-completion
3160 (filename directory
&optional predicate
)
3161 "Like `file-name-completion' for Tramp files."
3162 (let (hits-ignored-extensions)
3165 filename
(file-name-all-completions filename directory
)
3167 (when (funcall (or predicate
'identity
) (expand-file-name x directory
))
3170 completion-ignored-extensions
3172 (concat (regexp-opt completion-ignored-extensions
'paren
) "$") x
)
3173 ;; We remember the hit.
3174 (push x hits-ignored-extensions
))))))
3175 ;; No match. So we try again for ignored files.
3176 (try-completion filename hits-ignored-extensions
))))
3178 (defun tramp-handle-file-name-directory (file)
3179 "Like `file-name-directory' but aware of Tramp files."
3180 ;; Everything except the last filename thing is the directory. We
3181 ;; cannot apply `with-parsed-tramp-file-name', because this expands
3182 ;; the remote file name parts. This is a problem when we are in
3183 ;; file name completion.
3184 (let ((v (tramp-dissect-file-name file t
)))
3185 ;; Run the command on the localname portion only.
3186 (tramp-make-tramp-file-name
3187 v
(tramp-run-real-handler
3188 'file-name-directory
(list (or (tramp-file-name-localname v
) ""))))))
3190 (defun tramp-handle-file-name-nondirectory (file)
3191 "Like `file-name-nondirectory' but aware of Tramp files."
3192 (with-parsed-tramp-file-name file nil
3193 (tramp-run-real-handler 'file-name-nondirectory
(list localname
))))
3195 (defun tramp-handle-file-newer-than-file-p (file1 file2
)
3196 "Like `file-newer-than-file-p' for Tramp files."
3198 ((not (file-exists-p file1
)) nil
)
3199 ((not (file-exists-p file2
)) t
)
3200 (t (time-less-p (tramp-compat-file-attribute-modification-time
3201 (file-attributes file2
))
3202 (tramp-compat-file-attribute-modification-time
3203 (file-attributes file1
))))))
3205 (defun tramp-handle-file-regular-p (filename)
3206 "Like `file-regular-p' for Tramp files."
3207 (and (file-exists-p filename
)
3209 (aref (tramp-compat-file-attribute-modes (file-attributes filename
))
3212 (defun tramp-handle-file-remote-p (filename &optional identification connected
)
3213 "Like `file-remote-p' for Tramp files."
3214 ;; We do not want traces in the debug buffer.
3215 (let ((tramp-verbose (min tramp-verbose
3)))
3216 (when (tramp-tramp-file-p filename
)
3217 (let* ((v (tramp-dissect-file-name filename
))
3218 (p (tramp-get-connection-process v
))
3219 (c (and (process-live-p p
)
3220 (tramp-get-connection-property p
"connected" nil
))))
3221 ;; We expand the file name only, if there is already a connection.
3222 (with-parsed-tramp-file-name
3223 (if c
(expand-file-name filename
) filename
) nil
3224 (and (or (not connected
) c
)
3226 ((eq identification
'method
) method
)
3227 ;; Domain and port are appended to user and host,
3229 ((eq identification
'user
) (tramp-file-name-user-domain v
))
3230 ((eq identification
'host
) (tramp-file-name-host-port v
))
3231 ((eq identification
'localname
) localname
)
3232 ((eq identification
'hop
) hop
)
3233 (t (tramp-make-tramp-file-name
3234 method user domain host port
"" hop
)))))))))
3236 (defun tramp-handle-file-selinux-context (_filename)
3237 "Like `file-selinux-context' for Tramp files."
3238 ;; Return nil context.
3241 (defun tramp-handle-file-symlink-p (filename)
3242 "Like `file-symlink-p' for Tramp files."
3243 (let ((x (tramp-compat-file-attribute-type (file-attributes filename
))))
3244 (and (stringp x
) x
)))
3246 (defun tramp-handle-file-truename (filename)
3247 "Like `file-truename' for Tramp files."
3248 ;; Preserve trailing "/".
3250 (if (string-equal (file-name-nondirectory filename
) "")
3251 'file-name-as-directory
'identity
)
3252 (let ((result (expand-file-name filename
))
3254 ;; Don't make the following value larger than necessary.
3255 ;; People expect an error message in a timely fashion when
3256 ;; something is wrong; otherwise they might think that Emacs
3257 ;; is hung. Of course, correctness has to come first.
3260 (with-parsed-tramp-file-name result v1
3261 (with-tramp-file-property v1 v1-localname
"file-truename"
3262 (while (and (setq symlink-target
(file-symlink-p result
))
3263 (< numchase numchase-limit
))
3264 (setq numchase
(1+ numchase
)
3266 (with-parsed-tramp-file-name (expand-file-name result
) v2
3267 (tramp-make-tramp-file-name
3268 v2-method v2-user v2-domain v2-host v2-port
3270 (if (tramp-compat-file-name-quoted-p v2-localname
)
3271 'tramp-compat-file-name-quote
'identity
)
3273 (if (stringp symlink-target
)
3274 (if (file-remote-p symlink-target
)
3275 (let (file-name-handler-alist)
3276 (tramp-compat-file-name-quote symlink-target
))
3278 symlink-target
(file-name-directory v2-localname
)))
3280 (when (>= numchase numchase-limit
)
3283 "Maximum number (%d) of symlinks exceeded" numchase-limit
)))
3284 (directory-file-name result
))))))
3286 (defun tramp-handle-find-backup-file-name (filename)
3287 "Like `find-backup-file-name' for Tramp files."
3288 (with-parsed-tramp-file-name filename nil
3289 (let ((backup-directory-alist
3290 (if tramp-backup-directory-alist
3295 (if (and (stringp (cdr x
))
3296 (file-name-absolute-p (cdr x
))
3297 (not (tramp-tramp-file-p (cdr x
))))
3298 (tramp-make-tramp-file-name
3299 method user domain host port
(cdr x
) hop
)
3301 tramp-backup-directory-alist
)
3302 backup-directory-alist
)))
3303 (tramp-run-real-handler 'find-backup-file-name
(list filename
)))))
3305 (defun tramp-handle-insert-directory
3306 (filename switches
&optional wildcard full-directory-p
)
3307 "Like `insert-directory' for Tramp files."
3308 (unless switches
(setq switches
""))
3309 ;; Mark trailing "/".
3310 (when (and (zerop (length (file-name-nondirectory filename
)))
3311 (not full-directory-p
))
3312 (setq switches
(concat switches
"F")))
3313 (with-parsed-tramp-file-name (expand-file-name filename
) nil
3314 (with-tramp-progress-reporter v
0 (format "Opening directory %s" filename
)
3316 (let (ls-lisp-use-insert-directory-program start
)
3317 (tramp-run-real-handler
3319 (list filename switches wildcard full-directory-p
))
3320 ;; `ls-lisp' always returns full listings. We must remove
3321 ;; superfluous parts.
3322 (unless (string-match "l" switches
)
3324 (goto-char (point-min))
3326 (text-property-not-all
3327 (point) (point-at-eol) 'dired-filename t
))
3330 (or (text-property-any start
(point-at-eol) 'dired-filename t
)
3332 (if (= (point-at-bol) (point-at-eol))
3334 (delete-region (point) (progn (forward-line) (point)))
3335 (forward-line)))))))))
3337 (defun tramp-handle-insert-file-contents
3338 (filename &optional visit beg end replace
)
3339 "Like `insert-file-contents' for Tramp files."
3340 (barf-if-buffer-read-only)
3341 (setq filename
(expand-file-name filename
))
3342 (let (result local-copy remote-copy
)
3343 (with-parsed-tramp-file-name filename nil
3345 (if (not (file-exists-p filename
))
3347 v tramp-file-missing
3348 "File `%s' not found on remote host" filename
)
3350 (with-tramp-progress-reporter
3351 v
3 (format-message "Inserting `%s'" filename
)
3353 (if (and (tramp-local-host-p v
)
3354 (let (file-name-handler-alist)
3355 (file-readable-p localname
)))
3356 ;; Short track: if we are on the local host, we can
3359 (tramp-run-real-handler
3360 'insert-file-contents
3361 (list localname visit beg end replace
)))
3363 ;; When we shall insert only a part of the file, we
3364 ;; copy this part. This works only for the shell file
3366 (when (and (or beg end
)
3367 (tramp-get-method-parameter
3368 v
'tramp-login-program
))
3369 (setq remote-copy
(tramp-make-tramp-temp-file v
))
3370 ;; This is defined in tramp-sh.el. Let's assume
3371 ;; this is loaded already.
3372 (tramp-compat-funcall
3377 (format "dd bs=1 skip=%d if=%s count=%d of=%s"
3378 beg
(tramp-shell-quote-argument localname
)
3379 (- end beg
) remote-copy
))
3381 (format "dd bs=1 skip=%d if=%s of=%s"
3382 beg
(tramp-shell-quote-argument localname
)
3385 (format "dd bs=1 count=%d if=%s of=%s"
3386 end
(tramp-shell-quote-argument localname
)
3388 (setq tramp-temp-buffer-file-name nil beg nil end nil
))
3390 ;; `insert-file-contents-literally' takes care to
3391 ;; avoid calling jka-compr.el and epa.el. By
3392 ;; let-binding `inhibit-file-name-operation', we
3393 ;; propagate that care to the `file-local-copy'
3396 (let ((inhibit-file-name-operation
3397 (when (eq inhibit-file-name-operation
3398 'insert-file-contents
)
3401 ((stringp remote-copy
)
3403 (tramp-make-tramp-file-name
3404 method user domain host port remote-copy
)))
3405 ((stringp tramp-temp-buffer-file-name
)
3407 filename tramp-temp-buffer-file-name
'ok
)
3408 tramp-temp-buffer-file-name
)
3409 (t (file-local-copy filename
)))))
3411 ;; When the file is not readable for the owner, it
3412 ;; cannot be inserted, even if it is readable for the
3413 ;; group or for everybody.
3414 (set-file-modes local-copy
(string-to-number "0600" 8))
3416 (when (and (null remote-copy
)
3417 (tramp-get-method-parameter
3418 v
'tramp-copy-keep-tmpfile
))
3419 ;; We keep the local file for performance reasons,
3420 ;; useful for "rsync".
3421 (setq tramp-temp-buffer-file-name local-copy
))
3423 ;; We must ensure that `file-coding-system-alist'
3424 ;; matches `local-copy'.
3425 (let ((file-coding-system-alist
3426 (tramp-find-file-name-coding-system-alist
3427 filename local-copy
)))
3429 (insert-file-contents
3430 local-copy visit beg end replace
))))
3432 (add-hook 'find-file-not-found-functions
3433 `(lambda () (signal ',(car err
) ',(cdr err
)))
3435 (signal (car err
) (cdr err
))))))
3440 (setq buffer-file-name filename
)
3441 (setq buffer-read-only
(not (file-writable-p filename
)))
3442 (set-visited-file-modtime)
3443 (set-buffer-modified-p nil
))
3444 (when (and (stringp local-copy
)
3445 (or remote-copy
(null tramp-temp-buffer-file-name
)))
3446 (delete-file local-copy
))
3447 (when (stringp remote-copy
)
3449 (tramp-make-tramp-file-name
3450 method user domain host port remote-copy
)))))
3453 (list (expand-file-name filename
)
3456 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix
)
3457 "Like `load' for Tramp files."
3458 (with-parsed-tramp-file-name (expand-file-name file
) nil
3460 (cond ((file-exists-p (concat file
".elc"))
3461 (setq file
(concat file
".elc")))
3462 ((file-exists-p (concat file
".el"))
3463 (setq file
(concat file
".el")))))
3465 ;; The first condition is always true for absolute file names.
3466 ;; Included for safety's sake.
3467 (unless (or (file-name-directory file
)
3468 (string-match "\\.elc?\\'" file
))
3471 "File `%s' does not include a `.el' or `.elc' suffix" file
)))
3473 (when (not (file-exists-p file
))
3475 v tramp-file-missing
"Cannot load nonexistent file `%s'" file
)))
3476 (if (not (file-exists-p file
))
3478 (let ((tramp-message-show-message (not nomessage
)))
3479 (with-tramp-progress-reporter v
0 (format "Loading %s" file
)
3480 (let ((local-copy (file-local-copy file
)))
3482 (load local-copy noerror t nosuffix must-suffix
)
3483 (delete-file local-copy
)))))
3486 (defun tramp-handle-make-symbolic-link
3487 (target linkname
&optional ok-if-already-exists
)
3488 "Like `make-symbolic-link' for Tramp files.
3489 This is the fallback implementation for backends which do not
3490 support symbolic links."
3491 (if (tramp-tramp-file-p (expand-file-name linkname
))
3493 (tramp-dissect-file-name (expand-file-name linkname
)) 'file-error
3494 "make-symbolic-link not supported")
3495 ;; This is needed prior Emacs 26.1, where TARGET has also be
3496 ;; checked for a file name handler.
3497 (tramp-run-real-handler
3498 'make-symbolic-link
(list target linkname ok-if-already-exists
))))
3500 (defun tramp-handle-shell-command
3501 (command &optional output-buffer error-buffer
)
3502 "Like `shell-command' for Tramp files."
3503 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
3504 ;; We cannot use `shell-file-name' and `shell-command-switch',
3505 ;; they are variables of the local host.
3508 (tramp-get-method-parameter
3509 (tramp-dissect-file-name default-directory
)
3510 'tramp-remote-shell
)
3511 (tramp-get-method-parameter
3512 (tramp-dissect-file-name default-directory
)
3513 'tramp-remote-shell-args
))
3514 (list (substring command
0 asynchronous
))))
3518 ((bufferp output-buffer
) output-buffer
)
3519 ((stringp output-buffer
) (get-buffer-create output-buffer
))
3521 (setq current-buffer-p t
)
3523 (t (get-buffer-create
3525 "*Async Shell Command*"
3526 "*Shell Command Output*")))))
3529 ((bufferp error-buffer
) error-buffer
)
3530 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
3532 (if (and (not asynchronous
) error-buffer
)
3533 (with-parsed-tramp-file-name default-directory nil
3534 (list output-buffer
(tramp-make-tramp-temp-file v
)))
3536 (p (get-buffer-process output-buffer
)))
3538 ;; Check whether there is another process running. Tramp does not
3539 ;; support 2 (asynchronous) processes in parallel.
3541 (if (yes-or-no-p "A command is running. Kill it? ")
3542 (ignore-errors (kill-process p
))
3543 (tramp-user-error p
"Shell command in progress")))
3545 (if current-buffer-p
3547 (barf-if-buffer-read-only)
3549 (with-current-buffer output-buffer
3550 (setq buffer-read-only nil
)
3553 (if (and (not current-buffer-p
) (integerp asynchronous
))
3556 (setq p
(apply 'start-file-process
"*Async Shell*" buffer args
))
3558 (with-current-buffer output-buffer
3559 (display-buffer output-buffer
'(nil (allow-no-window . t
)))
3560 (setq mode-line-process
'(":%s"))
3562 (set-process-sentinel p
'shell-command-sentinel
)
3563 (set-process-filter p
'comint-output-filter
)))
3567 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
3568 ;; Insert error messages if they were separated.
3569 (when (listp buffer
)
3570 (with-current-buffer error-buffer
3571 (insert-file-contents (cadr buffer
)))
3572 (delete-file (cadr buffer
)))
3573 (if current-buffer-p
3574 ;; This is like exchange-point-and-mark, but doesn't
3575 ;; activate the mark. It is cleaner to avoid activation,
3576 ;; even though the command loop would deactivate the mark
3577 ;; because we inserted text.
3578 (goto-char (prog1 (mark t
)
3579 (set-marker (mark-marker) (point)
3581 ;; There's some output, display it.
3582 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
3583 (display-message-or-buffer output-buffer
)))))))
3585 (defun tramp-handle-substitute-in-file-name (filename)
3586 "Like `substitute-in-file-name' for Tramp files.
3587 \"//\" and \"/~\" substitute only in the local filename part."
3588 ;; Check, whether the local part is a quoted file name.
3589 (if (tramp-compat-file-name-quoted-p filename
)
3591 ;; First, we must replace environment variables.
3592 (setq filename
(tramp-replace-environment-variables filename
))
3593 (with-parsed-tramp-file-name filename nil
3594 ;; We do not want to replace environment variables, again.
3595 (let (process-environment)
3596 ;; Ignore in LOCALNAME everything before "//" or "/~".
3597 (when (stringp localname
)
3598 (if (string-match "//\\(/\\|~\\)" localname
)
3599 (setq filename
(substitute-in-file-name localname
))
3601 (concat (file-remote-p filename
)
3602 (tramp-run-real-handler
3603 'substitute-in-file-name
(list localname
)))))))
3604 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
3605 (if (and (stringp localname
) (string-equal "~" localname
))
3606 (concat filename
"/")
3609 (defun tramp-handle-set-visited-file-modtime (&optional time-list
)
3610 "Like `set-visited-file-modtime' for Tramp files."
3611 (unless (buffer-file-name)
3612 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
3615 (let ((remote-file-name-inhibit-cache t
))
3616 ;; '(-1 65535) means file doesn't exists yet.
3618 (or (tramp-compat-file-attribute-modification-time
3619 (file-attributes (buffer-file-name)))
3621 ;; We use '(0 0) as a don't-know value.
3622 (unless (equal time-list
'(0 0))
3623 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))))
3625 (defun tramp-handle-verify-visited-file-modtime (&optional buf
)
3626 "Like `verify-visited-file-modtime' for Tramp files.
3627 At the time `verify-visited-file-modtime' calls this function, we
3628 already know that the buffer is visiting a file and that
3629 `visited-file-modtime' does not return 0. Do not call this
3630 function directly, unless those two cases are already taken care
3632 (with-current-buffer (or buf
(current-buffer))
3633 (let ((f (buffer-file-name)))
3634 ;; There is no file visiting the buffer, or the buffer has no
3635 ;; recorded last modification time, or there is no established
3638 (eq (visited-file-modtime) 0)
3639 (not (file-remote-p f nil
'connected
)))
3641 (let* ((remote-file-name-inhibit-cache t
)
3642 (attr (file-attributes f
))
3643 (modtime (tramp-compat-file-attribute-modification-time attr
))
3644 (mt (visited-file-modtime)))
3647 ;; File exists, and has a known modtime.
3648 ((and attr
(not (equal modtime
'(0 0))))
3649 (< (abs (tramp-time-diff
3651 ;; For compatibility, deal with both the old
3652 ;; (HIGH . LOW) and the new (HIGH LOW) return
3653 ;; values of `visited-file-modtime'.
3655 (list (car mt
) (cdr mt
))
3658 ;; Modtime has the don't know value.
3660 ;; If file does not exist, say it is not modified if and
3661 ;; only if that agrees with the buffer's record.
3662 (t (equal mt
'(-1 65535)))))))))
3664 ;; This is used in tramp-gvfs.el and tramp-sh.el.
3665 (defconst tramp-gio-events
3666 '("attribute-changed" "changed" "changes-done-hint"
3667 "created" "deleted" "moved" "pre-unmount" "unmounted")
3668 "List of events \"gio monitor\" could send.")
3670 ;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
3672 (defun tramp-handle-file-notify-add-watch (filename _flags _callback
)
3673 "Like `file-notify-add-watch' for Tramp files."
3674 (setq filename
(expand-file-name filename
))
3675 (with-parsed-tramp-file-name filename nil
3677 v
'file-notify-error
"File notification not supported for `%s'" filename
)))
3679 (defun tramp-handle-file-notify-rm-watch (proc)
3680 "Like `file-notify-rm-watch' for Tramp files."
3681 ;; The descriptor must be a process object.
3682 (unless (processp proc
)
3683 (tramp-error proc
'file-notify-error
"Not a valid descriptor %S" proc
))
3684 (tramp-message proc
6 "Kill %S" proc
)
3685 (delete-process proc
))
3687 (defun tramp-handle-file-notify-valid-p (proc)
3688 "Like `file-notify-valid-p' for Tramp files."
3689 (and (process-live-p proc
)
3690 ;; Sometimes, the process is still in status `run' when the
3691 ;; file or directory to be watched is deleted already.
3692 (with-current-buffer (process-buffer proc
)
3694 (concat (file-remote-p default-directory
)
3695 (process-get proc
'watch-name
))))))
3697 ;;; Functions for establishing connection:
3699 ;; The following functions are actions to be taken when seeing certain
3700 ;; prompts from the remote host. See the variable
3701 ;; `tramp-actions-before-shell' for usage of these functions.
3703 (defun tramp-action-login (_proc vec
)
3704 "Send the login name."
3705 (let ((user (or (tramp-file-name-user vec
)
3706 (with-tramp-connection-property vec
"login-as"
3707 (save-window-excursion
3708 (let ((enable-recursive-minibuffers t
))
3709 (pop-to-buffer (tramp-get-connection-buffer vec
))
3710 (read-string (match-string 0))))))))
3711 (with-current-buffer (tramp-get-connection-buffer vec
)
3712 (tramp-message vec
6 "\n%s" (buffer-string)))
3713 (tramp-message vec
3 "Sending login name `%s'" user
)
3714 (tramp-send-string vec
(concat user tramp-local-end-of-line
))))
3716 (defun tramp-action-password (proc vec
)
3717 "Query the user for a password."
3718 (with-current-buffer (process-buffer proc
)
3719 (let ((enable-recursive-minibuffers t
)
3720 (case-fold-search t
))
3721 ;; Let's check whether a wrong password has been sent already.
3722 ;; Sometimes, the process returns a new password request
3723 ;; immediately after rejecting the previous (wrong) one.
3724 (unless (tramp-get-connection-property vec
"first-password-request" nil
)
3725 (tramp-clear-passwd vec
))
3726 (goto-char (point-min))
3727 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
3728 (tramp-message vec
3 "Sending %s" (match-string 1))
3729 ;; We don't call `tramp-send-string' in order to hide the
3730 ;; password from the debug buffer.
3731 (process-send-string
3732 proc
(concat (tramp-read-passwd proc
) tramp-local-end-of-line
))
3733 ;; Hide password prompt.
3734 (narrow-to-region (point-max) (point-max)))))
3736 (defun tramp-action-succeed (_proc _vec
)
3737 "Signal success in finding shell prompt."
3738 (throw 'tramp-action
'ok
))
3740 (defun tramp-action-permission-denied (proc _vec
)
3741 "Signal permission denied."
3743 (throw 'tramp-action
'permission-denied
))
3745 (defun tramp-action-yesno (proc vec
)
3746 "Ask the user for confirmation using `yes-or-no-p'.
3747 Send \"yes\" to remote process on confirmation, abort otherwise.
3748 See also `tramp-action-yn'."
3749 (save-window-excursion
3750 (let ((enable-recursive-minibuffers t
))
3751 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
3752 (unless (yes-or-no-p (match-string 0))
3754 (throw 'tramp-action
'permission-denied
))
3755 (with-current-buffer (tramp-get-connection-buffer vec
)
3756 (tramp-message vec
6 "\n%s" (buffer-string)))
3757 (tramp-send-string vec
(concat "yes" tramp-local-end-of-line
)))))
3759 (defun tramp-action-yn (proc vec
)
3760 "Ask the user for confirmation using `y-or-n-p'.
3761 Send \"y\" to remote process on confirmation, abort otherwise.
3762 See also `tramp-action-yesno'."
3763 (save-window-excursion
3764 (let ((enable-recursive-minibuffers t
))
3765 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
3766 (unless (y-or-n-p (match-string 0))
3768 (throw 'tramp-action
'permission-denied
))
3769 (with-current-buffer (tramp-get-connection-buffer vec
)
3770 (tramp-message vec
6 "\n%s" (buffer-string)))
3771 (tramp-send-string vec
(concat "y" tramp-local-end-of-line
)))))
3773 (defun tramp-action-terminal (_proc vec
)
3774 "Tell the remote host which terminal type to use.
3775 The terminal type can be configured with `tramp-terminal-type'."
3776 (tramp-message vec
5 "Setting `%s' as terminal type." tramp-terminal-type
)
3777 (with-current-buffer (tramp-get-connection-buffer vec
)
3778 (tramp-message vec
6 "\n%s" (buffer-string)))
3779 (tramp-send-string vec
(concat tramp-terminal-type tramp-local-end-of-line
)))
3781 (defun tramp-action-process-alive (proc _vec
)
3782 "Check, whether a process has finished."
3783 (unless (process-live-p proc
)
3784 (throw 'tramp-action
'process-died
)))
3786 (defun tramp-action-out-of-band (proc vec
)
3787 "Check, whether an out-of-band copy has finished."
3788 ;; There might be pending output for the exit status.
3789 (tramp-accept-process-output proc
0.1)
3790 (cond ((and (not (process-live-p proc
))
3791 (zerop (process-exit-status proc
)))
3792 (tramp-message vec
3 "Process has finished.")
3793 (throw 'tramp-action
'ok
))
3794 ((or (and (memq (process-status proc
) '(stop exit
))
3795 (not (zerop (process-exit-status proc
))))
3796 (memq (process-status proc
) '(signal)))
3797 ;; `scp' could have copied correctly, but set modes could have failed.
3798 ;; This can be ignored.
3799 (with-current-buffer (process-buffer proc
)
3800 (goto-char (point-min))
3801 (if (re-search-forward tramp-operation-not-permitted-regexp nil t
)
3803 (tramp-message vec
5 "'set mode' error ignored.")
3804 (tramp-message vec
3 "Process has finished.")
3805 (throw 'tramp-action
'ok
))
3806 (tramp-message vec
3 "Process has died.")
3807 (throw 'tramp-action
'out-of-band-failed
))))
3810 ;;; Functions for processing the actions:
3812 (defun tramp-process-one-action (proc vec actions
)
3813 "Wait for output from the shell and perform one action."
3814 (let ((case-fold-search t
)
3815 found todo item pattern action
)
3817 ;; Reread output once all actions have been performed.
3818 ;; Obviously, the output was not complete.
3819 (tramp-accept-process-output proc
1)
3822 (setq item
(pop todo
))
3823 (setq pattern
(format "\\(%s\\)\\'" (symbol-value (nth 0 item
))))
3824 (setq action
(nth 1 item
))
3826 vec
5 "Looking for regexp \"%s\" from remote shell" pattern
)
3827 (when (tramp-check-for-regexp proc pattern
)
3828 (tramp-message vec
5 "Call `%s'" (symbol-name action
))
3829 (setq found
(funcall action proc vec
)))))
3832 (defun tramp-process-actions (proc vec pos actions
&optional timeout
)
3833 "Perform ACTIONS until success or TIMEOUT.
3834 PROC and VEC indicate the remote connection to be used. POS, if
3835 set, is the starting point of the region to be deleted in the
3837 ;; Enable `auth-source', unless "emacs -Q" has been called. We must
3838 ;; use the "password-vector" property in case we have several hops.
3839 (tramp-set-connection-property
3840 (tramp-get-connection-property
3841 proc
"password-vector" (process-get proc
'vector
))
3842 "first-password-request" tramp-cache-read-persistent-data
)
3844 (with-tramp-progress-reporter
3845 proc
3 "Waiting for prompts from remote shell"
3848 (with-timeout (timeout (setq exit
'timeout
))
3851 (catch 'tramp-action
3852 (tramp-process-one-action proc vec actions
)))))
3855 (catch 'tramp-action
3856 (tramp-process-one-action proc vec actions
)))))
3857 (with-current-buffer (tramp-get-connection-buffer vec
)
3859 (tramp-message vec
6 "\n%s" (buffer-string)))
3861 (ignore-errors (funcall tramp-password-save-function
))
3863 (tramp-clear-passwd vec
)
3864 (delete-process proc
)
3865 (tramp-error-with-buffer
3866 (tramp-get-connection-buffer vec
) vec
'file-error
3868 ((eq exit
'permission-denied
) "Permission denied")
3869 ((eq exit
'out-of-band-failed
)
3871 "Copy failed, see buffer `%s' for details"
3872 (tramp-get-connection-buffer vec
)))
3873 ((eq exit
'process-died
)
3874 (substitute-command-keys
3876 "Tramp failed to connect. If this happens repeatedly, try\n"
3877 " `\\[tramp-cleanup-this-connection]'")))
3880 "Timeout reached, see buffer `%s' for details"
3881 (tramp-get-connection-buffer vec
)))
3882 (t "Login failed")))))
3884 (with-current-buffer (tramp-get-connection-buffer vec
)
3885 (let (buffer-read-only) (delete-region pos
(point))))))))
3887 ;;; Utility functions:
3889 (defun tramp-accept-process-output (proc timeout
)
3890 "Like `accept-process-output' for Tramp processes.
3891 This is needed in order to hide `last-coding-system-used', which is set
3892 for process communication also."
3893 (with-current-buffer (process-buffer proc
)
3894 (let (buffer-read-only last-coding-system-used
3895 ;; We do not want to run timers.
3896 timer-list timer-idle-list
)
3897 ;; Under Windows XP, `accept-process-output' doesn't return
3898 ;; sometimes. So we add an additional timeout. JUST-THIS-ONE
3899 ;; is set due to Bug#12145. It is an integer, in order to avoid
3900 ;; running timers as well.
3902 proc
10 "%s %s %s\n%s"
3903 proc
(process-status proc
)
3904 (with-timeout (timeout)
3905 (accept-process-output proc timeout nil
0))
3908 (defun tramp-check-for-regexp (proc regexp
)
3909 "Check, whether REGEXP is contained in process buffer of PROC.
3910 Erase echoed commands if exists."
3911 (with-current-buffer (process-buffer proc
)
3912 (goto-char (point-min))
3914 ;; Check whether we need to remove echo output.
3915 (when (and (tramp-get-connection-property proc
"check-remote-echo" nil
)
3916 (re-search-forward tramp-echoed-echo-mark-regexp nil t
))
3917 (let ((begin (match-beginning 0)))
3918 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t
)
3919 ;; Discard echo from remote output.
3920 (tramp-set-connection-property proc
"check-remote-echo" nil
)
3921 (tramp-message proc
5 "echo-mark found")
3923 (delete-region begin
(point))
3924 (goto-char (point-min)))))
3926 (when (or (not (tramp-get-connection-property proc
"check-remote-echo" nil
))
3927 ;; Sometimes, the echo string is suppressed on the remote side.
3929 (substring-no-properties
3930 tramp-echo-mark-marker
3931 0 (min tramp-echo-mark-marker-length
(1- (point-max))))
3932 (buffer-substring-no-properties
3934 (min (+ (point-min) tramp-echo-mark-marker-length
)
3936 ;; No echo to be handled, now we can look for the regexp.
3937 ;; Sometimes, lines are much too long, and we run into a "Stack
3938 ;; overflow in regexp matcher". For example, //DIRED// lines of
3939 ;; directory listings with some thousand files. Therefore, we
3940 ;; look from the end.
3941 (goto-char (point-max))
3942 (ignore-errors (re-search-backward regexp nil t
)))))
3944 (defun tramp-wait-for-regexp (proc timeout regexp
)
3945 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3946 Expects the output of PROC to be sent to the current buffer. Returns
3947 the string that matched, or nil. Waits indefinitely if TIMEOUT is
3949 (with-current-buffer (process-buffer proc
)
3950 (let ((found (tramp-check-for-regexp proc regexp
)))
3952 (with-timeout (timeout)
3954 (tramp-accept-process-output proc
1)
3955 (unless (process-live-p proc
)
3956 (tramp-error-with-buffer
3957 nil proc
'file-error
"Process has died"))
3958 (setq found
(tramp-check-for-regexp proc regexp
)))))
3961 (tramp-accept-process-output proc
1)
3962 (unless (process-live-p proc
)
3963 (tramp-error-with-buffer
3964 nil proc
'file-error
"Process has died"))
3965 (setq found
(tramp-check-for-regexp proc regexp
)))))
3966 (tramp-message proc
6 "\n%s" (buffer-string))
3970 proc
'file-error
"[[Regexp `%s' not found in %d secs]]"
3972 (tramp-error proc
'file-error
"[[Regexp `%s' not found]]" regexp
)))
3975 ;; It seems that Tru64 Unix does not like it if long strings are sent
3976 ;; to it in one go. (This happens when sending the Perl
3977 ;; `file-attributes' implementation, for instance.) Therefore, we
3978 ;; have this function which sends the string in chunks.
3979 (defun tramp-send-string (vec string
)
3980 "Send the STRING via connection VEC.
3982 The STRING is expected to use Unix line-endings, but the lines sent to
3983 the remote host use line-endings as defined in the variable
3984 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
3985 (let* ((p (tramp-get-connection-process vec
))
3986 (chunksize (tramp-get-connection-property p
"chunksize" nil
)))
3989 vec
'file-error
"Can't send string to remote host -- not logged in"))
3990 (tramp-set-connection-property p
"last-cmd-time" (current-time))
3991 (tramp-message vec
10 "%s" string
)
3992 (with-current-buffer (tramp-get-connection-buffer vec
)
3993 ;; Clean up the buffer. We cannot call `erase-buffer' because
3994 ;; narrowing might be in effect.
3995 (let (buffer-read-only) (delete-region (point-min) (point-max)))
3996 ;; Replace "\n" by `tramp-rsh-end-of-line'.
3999 'identity
(split-string string
"\n") tramp-rsh-end-of-line
))
4000 (unless (or (string= string
"")
4001 (string-equal (substring string -
1) tramp-rsh-end-of-line
))
4002 (setq string
(concat string tramp-rsh-end-of-line
)))
4004 (if (and chunksize
(not (zerop chunksize
)))
4006 (end (length string
)))
4009 vec
10 "Sending chunk from %s to %s"
4010 pos
(min (+ pos chunksize
) end
))
4011 (process-send-string
4012 p
(substring string pos
(min (+ pos chunksize
) end
)))
4013 (setq pos
(+ pos chunksize
))))
4014 (process-send-string p string
)))))
4016 (defun tramp-get-inode (vec)
4017 "Returns the virtual inode number.
4018 If it doesn't exist, generate a new one."
4019 (with-tramp-file-property vec
(tramp-file-name-localname vec
) "inode"
4020 (setq tramp-inodes
(1+ tramp-inodes
))))
4022 (defun tramp-get-device (vec)
4023 "Returns the virtual device number.
4024 If it doesn't exist, generate a new one."
4025 (with-tramp-connection-property (tramp-get-connection-process vec
) "device"
4026 (cons -
1 (setq tramp-devices
(1+ tramp-devices
)))))
4028 (defun tramp-equal-remote (file1 file2
)
4029 "Check, whether the remote parts of FILE1 and FILE2 are identical.
4030 The check depends on method, user and host name of the files. If
4031 one of the components is missing, the default values are used.
4032 The local file name parts of FILE1 and FILE2 are not taken into
4037 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
4039 would yield t. On the other hand, the following check results in nil:
4041 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
4042 (and (tramp-tramp-file-p file1
)
4043 (tramp-tramp-file-p file2
)
4044 (string-equal (file-remote-p file1
) (file-remote-p file2
))))
4046 ;;;###tramp-autoload
4047 (defun tramp-mode-string-to-int (mode-string)
4048 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
4049 (let* (case-fold-search
4050 (mode-chars (string-to-vector mode-string
))
4051 (owner-read (aref mode-chars
1))
4052 (owner-write (aref mode-chars
2))
4053 (owner-execute-or-setid (aref mode-chars
3))
4054 (group-read (aref mode-chars
4))
4055 (group-write (aref mode-chars
5))
4056 (group-execute-or-setid (aref mode-chars
6))
4057 (other-read (aref mode-chars
7))
4058 (other-write (aref mode-chars
8))
4059 (other-execute-or-sticky (aref mode-chars
9)))
4063 ((char-equal owner-read ?r
) (string-to-number "00400" 8))
4064 ((char-equal owner-read ?-
) 0)
4065 (t (error "Second char `%c' must be one of `r-'" owner-read
)))
4067 ((char-equal owner-write ?w
) (string-to-number "00200" 8))
4068 ((char-equal owner-write ?-
) 0)
4069 (t (error "Third char `%c' must be one of `w-'" owner-write
)))
4071 ((char-equal owner-execute-or-setid ?x
) (string-to-number "00100" 8))
4072 ((char-equal owner-execute-or-setid ?S
) (string-to-number "04000" 8))
4073 ((char-equal owner-execute-or-setid ?s
) (string-to-number "04100" 8))
4074 ((char-equal owner-execute-or-setid ?-
) 0)
4075 (t (error "Fourth char `%c' must be one of `xsS-'"
4076 owner-execute-or-setid
)))
4078 ((char-equal group-read ?r
) (string-to-number "00040" 8))
4079 ((char-equal group-read ?-
) 0)
4080 (t (error "Fifth char `%c' must be one of `r-'" group-read
)))
4082 ((char-equal group-write ?w
) (string-to-number "00020" 8))
4083 ((char-equal group-write ?-
) 0)
4084 (t (error "Sixth char `%c' must be one of `w-'" group-write
)))
4086 ((char-equal group-execute-or-setid ?x
) (string-to-number "00010" 8))
4087 ((char-equal group-execute-or-setid ?S
) (string-to-number "02000" 8))
4088 ((char-equal group-execute-or-setid ?s
) (string-to-number "02010" 8))
4089 ((char-equal group-execute-or-setid ?-
) 0)
4090 (t (error "Seventh char `%c' must be one of `xsS-'"
4091 group-execute-or-setid
)))
4093 ((char-equal other-read ?r
) (string-to-number "00004" 8))
4094 ((char-equal other-read ?-
) 0)
4095 (t (error "Eighth char `%c' must be one of `r-'" other-read
)))
4097 ((char-equal other-write ?w
) (string-to-number "00002" 8))
4098 ((char-equal other-write ?-
) 0)
4099 (t (error "Ninth char `%c' must be one of `w-'" other-write
)))
4101 ((char-equal other-execute-or-sticky ?x
) (string-to-number "00001" 8))
4102 ((char-equal other-execute-or-sticky ?T
) (string-to-number "01000" 8))
4103 ((char-equal other-execute-or-sticky ?t
) (string-to-number "01001" 8))
4104 ((char-equal other-execute-or-sticky ?-
) 0)
4105 (t (error "Tenth char `%c' must be one of `xtT-'"
4106 other-execute-or-sticky
)))))))
4108 (defconst tramp-file-mode-type-map
4109 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
4111 (2 .
"c") ; character device
4112 (3 .
"m") ; multiplexed character device (v7)
4113 (4 .
"d") ; directory
4114 (5 .
"?") ; Named special file (XENIX)
4115 (6 .
"b") ; block device
4116 (7 .
"?") ; multiplexed block device (v7)
4117 (8 .
"-") ; regular file
4118 (9 .
"n") ; network special file (HP-UX)
4119 (10 .
"l") ; symlink
4120 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
4122 (13 .
"D") ; door special (Solaris)
4123 (14 .
"w")) ; whiteout (BSD)
4124 "A list of file types returned from the `stat' system call.
4125 This is used to map a mode number to a permission string.")
4127 ;;;###tramp-autoload
4128 (defun tramp-file-mode-from-int (mode)
4129 "Turn an integer representing a file mode into an ls(1)-like string."
4131 (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
4132 (user (logand (lsh mode -
6) 7))
4133 (group (logand (lsh mode -
3) 7))
4134 (other (logand (lsh mode -
0) 7))
4135 (suid (> (logand (lsh mode -
9) 4) 0))
4136 (sgid (> (logand (lsh mode -
9) 2) 0))
4137 (sticky (> (logand (lsh mode -
9) 1) 0)))
4138 (setq user
(tramp-file-mode-permissions user suid
"s"))
4139 (setq group
(tramp-file-mode-permissions group sgid
"s"))
4140 (setq other
(tramp-file-mode-permissions other sticky
"t"))
4141 (concat type user group other
)))
4143 (defun tramp-file-mode-permissions (perm suid suid-text
)
4144 "Convert a permission bitset into a string.
4145 This is used internally by `tramp-file-mode-from-int'."
4146 (let ((r (> (logand perm
4) 0))
4147 (w (> (logand perm
2) 0))
4148 (x (> (logand perm
1) 0)))
4149 (concat (or (and r
"r") "-")
4150 (or (and w
"w") "-")
4151 (or (and suid x suid-text
) ; suid, execute
4152 (and suid
(upcase suid-text
)) ; suid, !execute
4153 (and x
"x") "-")))) ; !suid
4155 ;;;###tramp-autoload
4156 (defun tramp-get-local-uid (id-format)
4157 "The uid of the local user, in ID-FORMAT.
4158 ID-FORMAT valid values are `string' and `integer'."
4159 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
4161 ;;;###tramp-autoload
4162 (defun tramp-get-local-gid (id-format)
4163 "The gid of the local user, in ID-FORMAT.
4164 ID-FORMAT valid values are `string' and `integer'."
4165 ;; `group-gid' has been introduced with Emacs 24.4.
4166 (if (and (fboundp 'group-gid
) (equal id-format
'integer
))
4167 (tramp-compat-funcall 'group-gid
)
4168 (tramp-compat-file-attribute-group-id (file-attributes "~/" id-format
))))
4170 (defun tramp-get-local-locale (&optional vec
)
4171 "Determine locale, supporting UTF8 if possible.
4172 VEC is used for tracing."
4173 ;; We use key nil for local connection properties.
4174 (with-tramp-connection-property nil
"locale"
4175 (let ((candidates '("en_US.utf8" "C.utf8" "en_US.UTF-8"))
4178 (unless (or (memq system-type
'(windows-nt))
4179 (not (zerop (tramp-call-process
4180 nil
"locale" nil t nil
"-a"))))
4182 (goto-char (point-min))
4183 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates
)))
4185 (setq locale
(car candidates
)
4187 (setq candidates
(cdr candidates
))))))
4189 (when vec
(tramp-message vec
7 "locale %s" (or locale
"C")))
4192 ;;;###tramp-autoload
4193 (defun tramp-check-cached-permissions (vec access
)
4194 "Check `file-attributes' caches for VEC.
4195 Return t if according to the cache access type ACCESS is known to
4201 ((eq ?x access
) 3))))
4202 (dolist (suffix '("string" "integer") result
)
4209 (tramp-get-file-property
4210 vec
(tramp-file-name-localname vec
)
4211 (concat "file-attributes-" suffix
) nil
)
4213 (tramp-make-tramp-file-name vec
) (intern suffix
))))
4215 (tramp-get-connection-property
4216 vec
(concat "uid-" suffix
) nil
))
4218 (tramp-get-connection-property
4219 vec
(concat "gid-" suffix
) nil
))
4221 (if (string-equal suffix
"string")
4222 tramp-unknown-id-string tramp-unknown-id-integer
)))
4227 (eq t
(tramp-compat-file-attribute-type file-attr
))
4228 (null (tramp-compat-file-attribute-type file-attr
)))
4230 ;; World accessible.
4232 (aref (tramp-compat-file-attribute-modes file-attr
)
4234 ;; User accessible and owned by user.
4237 (aref (tramp-compat-file-attribute-modes file-attr
) offset
))
4238 (or (equal remote-uid
4239 (tramp-compat-file-attribute-user-id file-attr
))
4241 (tramp-compat-file-attribute-user-id file-attr
))))
4242 ;; Group accessible and owned by user's principal group.
4245 (aref (tramp-compat-file-attribute-modes file-attr
)
4247 (or (equal remote-gid
4248 (tramp-compat-file-attribute-group-id file-attr
))
4250 (tramp-compat-file-attribute-group-id
4251 file-attr
))))))))))))
4253 ;;;###tramp-autoload
4254 (defun tramp-local-host-p (vec)
4255 "Return t if this points to the local host, nil otherwise.
4256 This handles also chrooted environments, which are not regarded as local."
4257 (let ((host (tramp-file-name-host vec
))
4258 (port (tramp-file-name-port vec
)))
4260 (stringp tramp-local-host-regexp
) (stringp host
)
4261 (string-match tramp-local-host-regexp host
)
4262 ;; A port is an indication for an ssh tunnel or alike.
4264 ;; The method shall be applied to one of the shell file name
4265 ;; handlers. `tramp-local-host-p' is also called for "smb" and
4266 ;; alike, where it must fail.
4267 (tramp-get-method-parameter vec
'tramp-login-program
)
4268 ;; The local temp directory must be writable for the other user.
4270 (tramp-make-tramp-file-name
4271 vec
(tramp-compat-temporary-file-directory) 'nohop
))
4272 ;; On some systems, chown runs only for root.
4273 (or (zerop (user-uid))
4274 ;; This is defined in tramp-sh.el. Let's assume this is
4276 (zerop (tramp-compat-funcall 'tramp-get-remote-uid vec
'integer
))))))
4278 (defun tramp-get-remote-tmpdir (vec)
4279 "Return directory for temporary files on the remote host identified by VEC."
4280 (with-tramp-connection-property vec
"tmpdir"
4282 (tramp-make-tramp-file-name
4283 vec
(or (tramp-get-method-parameter vec
'tramp-tmpdir
) "/tmp"))))
4284 (or (and (file-directory-p dir
) (file-writable-p dir
)
4285 (file-remote-p dir
'localname
))
4286 (tramp-error vec
'file-error
"Directory %s not accessible" dir
))
4289 ;;;###tramp-autoload
4290 (defun tramp-make-tramp-temp-file (vec)
4291 "Create a temporary file on the remote host identified by VEC.
4292 Return the local name of the temporary file."
4293 (let ((prefix (expand-file-name
4294 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))
4297 ;; `make-temp-file' would be the natural choice for
4298 ;; implementation. But it calls `write-region' internally,
4299 ;; which also needs a temporary file - we would end in an
4301 (setq result
(make-temp-name prefix
))
4302 (if (file-exists-p result
)
4304 ;; This creates the file by side effect.
4305 (set-file-times result
)
4306 (set-file-modes result
(string-to-number "0700" 8))))
4308 ;; Return the local part.
4309 (with-parsed-tramp-file-name result nil localname
)))
4311 (defun tramp-delete-temp-file-function ()
4312 "Remove temporary files related to current buffer."
4313 (when (stringp tramp-temp-buffer-file-name
)
4314 (ignore-errors (delete-file tramp-temp-buffer-file-name
))))
4316 (add-hook 'kill-buffer-hook
'tramp-delete-temp-file-function
)
4317 (add-hook 'tramp-unload-hook
4319 (remove-hook 'kill-buffer-hook
4320 'tramp-delete-temp-file-function
)))
4322 (defun tramp-handle-make-auto-save-file-name ()
4323 "Like `make-auto-save-file-name' for Tramp files.
4324 Returns a file name in `tramp-auto-save-directory' for autosaving
4325 this file, if that variable is non-nil."
4326 (when (stringp tramp-auto-save-directory
)
4327 (setq tramp-auto-save-directory
4328 (expand-file-name tramp-auto-save-directory
)))
4329 ;; Create directory.
4330 (unless (or (null tramp-auto-save-directory
)
4331 (file-exists-p tramp-auto-save-directory
))
4332 (make-directory tramp-auto-save-directory t
))
4335 (if (and (stringp tramp-auto-save-directory
)
4336 (file-remote-p tramp-auto-save-directory
))
4339 (auto-save-file-name-transforms
4340 (if (null tramp-auto-save-directory
)
4341 auto-save-file-name-transforms
))
4343 (if (null tramp-auto-save-directory
)
4346 (tramp-subst-strs-in-string
4353 (tramp-compat-file-name-unquote (buffer-file-name)))
4354 tramp-auto-save-directory
))))
4355 ;; Run plain `make-auto-save-file-name'.
4356 (tramp-run-real-handler 'make-auto-save-file-name nil
)))
4358 (defun tramp-subst-strs-in-string (alist string
)
4359 "Replace all occurrences of the string FROM with TO in STRING.
4360 ALIST is of the form ((FROM . TO) ...)."
4363 (let* ((pr (car alist
))
4366 (while (string-match (regexp-quote from
) string
)
4367 (setq string
(replace-match to t t string
)))
4368 (setq alist
(cdr alist
))))
4371 (defun tramp-handle-temporary-file-directory ()
4372 "Like `temporary-file-directory' for Tramp files."
4374 (dolist (dir `(,(ignore-errors
4375 (tramp-get-remote-tmpdir
4376 (tramp-dissect-file-name default-directory
)))
4377 ,default-directory
))
4378 (when (and (stringp dir
) (file-directory-p dir
) (file-writable-p dir
))
4379 (throw 'result
(expand-file-name dir
))))))
4381 (defun tramp-handle-make-nearby-temp-file (prefix &optional dir-flag suffix
)
4382 "Like `make-nearby-temp-file' for Tramp files."
4383 (let ((temporary-file-directory
4384 (tramp-compat-temporary-file-directory-function)))
4385 (make-temp-file prefix dir-flag suffix
)))
4387 ;;; Compatibility functions section:
4389 (defun tramp-call-process
4390 (vec program
&optional infile destination display
&rest args
)
4391 "Calls `call-process' on the local host.
4392 It always returns a return code. The Lisp error raised when
4393 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4394 are written with verbosity of 6."
4395 (let ((default-directory (tramp-compat-temporary-file-directory))
4396 (destination (if (eq destination t
) (current-buffer) destination
))
4397 output error result
)
4399 vec
6 "`%s %s' %s %s"
4400 program
(mapconcat 'identity args
" ") infile destination
)
4405 'call-process program infile
(or destination t
) display args
))
4406 ;; `result' could also be an error string.
4407 (when (stringp result
)
4410 (with-current-buffer
4411 (if (bufferp destination
) destination
(current-buffer))
4412 (setq output
(buffer-string))))
4414 (setq error
(error-message-string err
)
4416 (if (zerop (length error
))
4417 (tramp-message vec
6 "%d\n%s" result output
)
4418 (tramp-message vec
6 "%d\n%s\n%s" result output error
))
4421 (defun tramp-call-process-region
4422 (vec start end program
&optional delete buffer display
&rest args
)
4423 "Calls `call-process-region' on the local host.
4424 It always returns a return code. The Lisp error raised when
4425 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4426 are written with verbosity of 6."
4427 (let ((default-directory (tramp-compat-temporary-file-directory))
4428 (buffer (if (eq buffer t
) (current-buffer) buffer
))
4431 vec
6 "`%s %s' %s %s %s %s"
4432 program
(mapconcat 'identity args
" ") start end delete buffer
)
4437 'call-process-region
4438 start end program delete buffer display args
))
4439 ;; `result' could also be an error string.
4440 (when (stringp result
)
4441 (signal 'file-error
(list result
)))
4442 (with-current-buffer (if (bufferp buffer
) buffer
(current-buffer))
4444 (tramp-message vec
6 "%d" result
)
4445 (tramp-message vec
6 "%d\n%s" result
(buffer-string)))))
4448 (tramp-message vec
6 "%d\n%s" result
(error-message-string err
))))
4451 ;;;###tramp-autoload
4452 (defun tramp-read-passwd (proc &optional prompt
)
4453 "Read a password from user (compat function).
4454 Consults the auth-source package.
4455 Invokes `password-read' if available, `read-passwd' else."
4456 (let* ((case-fold-search t
)
4457 (key (tramp-make-tramp-file-name
4458 ;; In tramp-sh.el, we must use "password-vector" due to
4460 (tramp-get-connection-property
4461 proc
"password-vector" (process-get proc
'vector
))
4465 (with-current-buffer (process-buffer proc
)
4466 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
4467 (format "%s for %s " (capitalize (match-string 1)) key
))))
4468 (auth-source-creation-prompts `((secret .
,pw-prompt
)))
4469 ;; We suspend the timers while reading the password.
4470 (stimers (with-timeout-suspend))
4471 auth-info auth-passwd
)
4474 (with-parsed-tramp-file-name key nil
4475 (setq tramp-password-save-function nil
4477 (or user
(tramp-get-connection-property key
"login-as" nil
)))
4480 ;; See if auth-sources contains something useful.
4482 (and (tramp-get-connection-property
4483 v
"first-password-request" nil
)
4484 ;; Try with Tramp's current method.
4492 user tramp-prefix-domain-format domain
)
4497 host tramp-prefix-port-format port
)
4500 :require
(cons :secret
(and user
'(:user
)))
4502 tramp-password-save-function
4503 (plist-get auth-info
:save-function
)
4504 auth-passwd
(plist-get auth-info
:secret
)))
4505 (while (functionp auth-passwd
)
4506 (setq auth-passwd
(funcall auth-passwd
)))
4509 ;; Try the password cache.
4511 (setq auth-passwd
(password-read pw-prompt key
)
4512 tramp-password-save-function
4513 (lambda () (password-cache-add key auth-passwd
)))
4516 ;; Else, get the password interactively w/o cache.
4517 (read-passwd pw-prompt
))
4519 (tramp-set-connection-property v
"first-password-request" nil
)))
4521 ;; Reenable the timers.
4522 (with-timeout-unsuspend stimers
))))
4524 ;;;###tramp-autoload
4525 (defun tramp-clear-passwd (vec)
4526 "Clear password cache for connection related to VEC."
4527 (let ((method (tramp-file-name-method vec
))
4528 (user-domain (tramp-file-name-user-domain vec
))
4529 (host-port (tramp-file-name-host-port vec
))
4530 (hop (tramp-file-name-hop vec
)))
4532 ;; Clear also the passwords of the hops.
4534 (tramp-dissect-file-name
4537 (replace-regexp-in-string
4538 (concat tramp-postfix-hop-regexp
"$")
4539 tramp-postfix-host-format hop
)))))
4541 `(:max
1 ,(and user-domain
:user
) ,user-domain
4542 :host
,host-port
:port
,method
))
4543 (password-cache-remove (tramp-make-tramp-file-name vec
'noloc
'nohop
))))
4545 ;; Snarfed code from time-date.el.
4547 (defconst tramp-half-a-year
'(241 17024)
4548 "Evaluated by \"(days-to-time 183)\".")
4550 ;;;###tramp-autoload
4551 (defun tramp-time-diff (t1 t2
)
4552 "Return the difference between the two times, in seconds.
4553 T1 and T2 are time values (as returned by `current-time' for example)."
4554 ;; Starting with Emacs 25.1, we could change this to use `time-subtract'.
4555 (float-time (tramp-compat-funcall 'subtract-time t1 t2
)))
4557 (defun tramp-unquote-shell-quote-argument (s)
4558 "Remove quotation prefix \"/:\" from string S, and quote it then for shell."
4559 (shell-quote-argument (tramp-compat-file-name-unquote s
)))
4561 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
4562 ;; does not deal well with newline characters. Newline is replaced by
4563 ;; backslash newline. But if, say, the string `a backslash newline b'
4564 ;; is passed to a shell, the shell will expand this into "ab",
4565 ;; completely omitting the newline. This is not what was intended.
4566 ;; It does not appear to be possible to make the function
4567 ;; `shell-quote-argument' work with newlines without making it
4568 ;; dependent on the shell used. But within this package, we know that
4569 ;; we will always use a Bourne-like shell, so we use an approach which
4572 ;; The approach is simple: we call `shell-quote-argument', then
4573 ;; massage the newline part of the result.
4575 ;; This function should produce a string which is grokked by a Unix
4576 ;; shell, even if the Emacs is running on Windows. Since this is the
4577 ;; kludges section, we bind `system-type' in such a way that
4578 ;; `shell-quote-argument' behaves as if on Unix.
4580 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
4581 ;; function to work with Bourne-like shells.
4582 ;;;###tramp-autoload
4583 (defun tramp-shell-quote-argument (s)
4584 "Similar to `shell-quote-argument', but groks newlines.
4585 Only works for Bourne-like shells."
4586 (let ((system-type 'not-windows
))
4588 (let ((result (tramp-unquote-shell-quote-argument s
))
4589 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line
))))
4590 (when (and (>= (length result
) 2)
4591 (string= (substring result
0 2) "\\~"))
4592 (setq result
(substring result
1)))
4593 (while (string-match nl result
)
4594 (setq result
(replace-match (format "'%s'" tramp-rsh-end-of-line
)
4598 ;;; Signal handling. This works for remote processes, which have set
4599 ;;; the process property `remote-pid'.
4601 (defun tramp-interrupt-process (&optional process _current-group
)
4602 "Interrupt remote process PROC."
4603 ;; CURRENT-GROUP is not implemented yet.
4605 ((processp process
) process
)
4606 ((bufferp process
) (get-buffer-process process
))
4607 ((stringp process
) (or (get-process process
)
4608 (get-buffer-process process
)))
4609 ((null process
) (get-buffer-process (current-buffer)))
4612 ;; If it's a Tramp process, send the INT signal remotely.
4613 (when (and (processp proc
) (setq pid
(process-get proc
'remote-pid
)))
4614 (if (not (process-live-p proc
))
4615 (tramp-error proc
'error
"Process %s is not active" proc
)
4616 (tramp-message proc
5 "Interrupt process %s with pid %s" proc pid
)
4617 ;; This is for tramp-sh.el. Other backends do not support this (yet).
4618 (tramp-compat-funcall
4620 (process-get proc
'vector
)
4621 (format "kill -2 %d" pid
))
4622 ;; Wait, until the process has disappeared. If it doesn't,
4623 ;; fall back to the default implementation.
4624 (with-timeout (1 (ignore))
4625 (while (process-live-p proc
)
4626 ;; We cannot run `tramp-accept-process-output', it blocks timers.
4627 (accept-process-output proc
0.1))
4631 ;; `interrupt-process-functions' exists since Emacs 26.1.
4632 (when (boundp 'interrupt-process-functions
)
4633 (add-hook 'interrupt-process-functions
'tramp-interrupt-process
)
4637 (remove-hook 'interrupt-process-functions
'tramp-interrupt-process
))))
4639 ;;; Integration of eshell.el:
4641 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
4642 ;; when `default-directory' points to another host.
4643 (defun tramp-eshell-directory-change ()
4644 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
4645 (setq eshell-path-env
4646 (if (tramp-tramp-file-p default-directory
)
4647 (with-parsed-tramp-file-name default-directory nil
4651 ;; When `tramp-own-remote-path' is in `tramp-remote-path',
4652 ;; the remote path is only set in the session cache.
4653 (tramp-get-connection-property
4654 (tramp-get-connection-process v
) "remote-path" nil
)
4655 (tramp-get-connection-property v
"remote-path" nil
))
4659 (eval-after-load "esh-util"
4661 (add-hook 'eshell-mode-hook
4662 'tramp-eshell-directory-change
)
4663 (add-hook 'eshell-directory-change-hook
4664 'tramp-eshell-directory-change
)
4665 (add-hook 'tramp-unload-hook
4667 (remove-hook 'eshell-mode-hook
4668 'tramp-eshell-directory-change
)
4669 (remove-hook 'eshell-directory-change-hook
4670 'tramp-eshell-directory-change
)))))
4672 ;; Checklist for `tramp-unload-hook'
4673 ;; - Unload all `tramp-*' packages
4674 ;; - Reset `file-name-handler-alist'
4675 ;; - Cleanup hooks where Tramp functions are in
4676 ;; - Cleanup advised functions
4677 ;; - Cleanup autoloads
4679 (defun tramp-unload-tramp ()
4680 "Discard Tramp from loading remote files."
4682 ;; ange-ftp settings must be enabled.
4683 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp
)
4684 ;; Maybe it's not loaded yet.
4685 (ignore-errors (unload-feature 'tramp
'force
)))
4691 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
4693 ;; * Better error checking. At least whenever we see something
4694 ;; strange when doing zerop, we should kill the process and start
4695 ;; again. (Greg Stark)
4697 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
4699 ;; * I was wondering if it would be possible to use tramp even if I'm
4700 ;; actually using sshfs. But when I launch a command I would like
4701 ;; to get it executed on the remote machine where the files really
4702 ;; are. (Andrea Crotti)
4704 ;; * Run emerge on two remote files. Bug is described here:
4705 ;; <https://www.mail-archive.com/tramp-devel@nongnu.org/msg01041.html>.
4708 ;; * Refactor code from different handlers. Start with
4709 ;; *-process-file. One idea is to generalize `tramp-send-command'
4710 ;; and friends, for most of the handlers this is the major
4711 ;; difference between the different backends. Other handlers but
4712 ;; *-process-file would profit from this as well.
4714 ;;; tramp.el ends here