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 (defconst 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.")
439 (defvar tramp-completion-function-alist nil
440 "Alist of methods for remote files.
441 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...).
442 Each NAME stands for a remote access method. Each PAIR is of the form
443 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
444 names from FILE for completion. The following predefined FUNCTIONs exists:
446 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
447 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
448 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
449 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
450 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
451 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
452 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
453 * `tramp-parse-etc-group' for \"/etc/group\" like files.
454 * `tramp-parse-netrc' for \"~/.netrc\" like files.
455 * `tramp-parse-putty' for PuTTY registered sessions.
457 FUNCTION can also be a user defined function. For more details see
460 (defconst tramp-echo-mark-marker
"_echo"
461 "String marker to surround echoed commands.")
463 (defconst tramp-echo-mark-marker-length
(length tramp-echo-mark-marker
)
464 "String length of `tramp-echo-mark-marker'.")
466 (defconst tramp-echo-mark
467 (concat tramp-echo-mark-marker
468 (make-string tramp-echo-mark-marker-length ?
\b))
469 "String mark to be transmitted around shell commands.
470 Used to separate their echo from the output they produce. This
471 will only be used if we cannot disable remote echo via stty.
472 This string must have no effect on the remote shell except for
473 producing some echo which can later be detected by
474 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
475 followed by an equal number of backspaces to erase them will
478 (defconst tramp-echoed-echo-mark-regexp
479 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
480 tramp-echo-mark-marker tramp-echo-mark-marker-length
)
481 "Regexp which matches `tramp-echo-mark' as it gets echoed by
484 (defcustom tramp-local-end-of-line
485 (if (memq system-type
'(windows-nt)) "\r\n" "\n")
486 "String used for end of line in local processes."
492 (defcustom tramp-rsh-end-of-line
"\n"
493 "String used for end of line in rsh connections.
494 I don't think this ever needs to be changed, so please tell me about it
495 if you need to change this."
500 (defcustom tramp-login-prompt-regexp
501 ".*\\(user\\|login\\)\\( .*\\)?: *"
502 "Regexp matching login-like prompts.
503 The regexp should match at end of buffer.
505 Sometimes the prompt is reported to look like \"login as:\"."
510 (defcustom tramp-shell-prompt-pattern
511 ;; Allow a prompt to start right after a ^M since it indeed would be
512 ;; displayed at the beginning of the line (and Zsh uses it). This
513 ;; regexp works only for GNU Emacs.
514 ;; Allow also [] style prompts. They can appear only during
515 ;; connection initialization; Tramp redefines the prompt afterwards.
516 (concat "\\(?:^\\|\r\\)"
517 "[^]#$%>\n]*#?[]#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
518 "Regexp to match prompts from remote shell.
519 Normally, Tramp expects you to configure `shell-prompt-pattern'
520 correctly, but sometimes it happens that you are connecting to a
521 remote host which sends a different kind of shell prompt. Therefore,
522 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
523 and also things matched by this variable. The default value of this
524 variable is similar to the default value of `shell-prompt-pattern',
525 which should work well in many cases.
527 This regexp must match both `tramp-initial-end-of-output' and
528 `tramp-end-of-output'."
533 (defcustom tramp-password-prompt-regexp
534 (format "^.*\\(%s\\).*:\^@? *"
535 ;; `password-word-equivalents' has been introduced with Emacs 24.4.
536 (regexp-opt (or (bound-and-true-p password-word-equivalents
)
537 '("password" "passphrase"))))
538 "Regexp matching password-like prompts.
539 The regexp should match at end of buffer.
541 The `sudo' program appears to insert a `^@' character into the prompt."
547 (defcustom tramp-wrong-passwd-regexp
549 ;; These strings should be on the last line
550 (regexp-opt '("Permission denied"
555 "Timeout, server not responding."
557 "Name or service not known"
558 "Host key verification failed."
559 "No supported authentication methods left to try!")
564 ;; Here comes a list of regexes, separated by \\|
565 "Received signal [0-9]+"
567 "Regexp matching a `login failed' message.
568 The regexp should match at end of buffer."
573 (defcustom tramp-yesno-prompt-regexp
575 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t
)
577 "Regular expression matching all yes/no queries which need to be confirmed.
578 The confirmation should be done with yes or no.
579 The regexp should match at end of buffer.
580 See also `tramp-yn-prompt-regexp'."
585 (defcustom tramp-yn-prompt-regexp
587 (regexp-opt '("Store key in cache? (y/n)"
588 "Update cached key? (y/n, Return cancels connection)")
591 "Regular expression matching all y/n queries which need to be confirmed.
592 The confirmation should be done with y or n.
593 The regexp should match at end of buffer.
594 See also `tramp-yesno-prompt-regexp'."
599 (defcustom tramp-terminal-prompt-regexp
603 "Terminal type\\? \\[.*\\]"
605 "Regular expression matching all terminal setting prompts.
606 The regexp should match at end of buffer.
607 The answer will be provided by `tramp-action-terminal', which see."
612 (defcustom tramp-operation-not-permitted-regexp
613 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
614 (regexp-opt '("Operation not permitted") t
))
615 "Regular expression matching keep-date problems in (s)cp operations.
616 Copying has been performed successfully already, so this message can
622 (defcustom tramp-copy-failed-regexp
624 (regexp-opt '("Permission denied"
627 "No such file or directory")
630 "Regular expression matching copy problems in (s)cp operations."
635 (defcustom tramp-process-alive-regexp
637 "Regular expression indicating a process has finished.
638 In fact this expression is empty by intention, it will be used only to
639 check regularly the status of the associated process.
640 The answer will be provided by `tramp-action-process-alive',
641 `tramp-action-out-of-band', which see."
646 (defconst tramp-temp-name-prefix
"tramp."
647 "Prefix to use for temporary files.
648 If this is a relative file name (such as \"tramp.\"), it is considered
649 relative to the directory name returned by the function
650 `tramp-compat-temporary-file-directory' (which see). It may also be an
651 absolute file name; don't forget to include a prefix for the filename
654 (defconst tramp-temp-buffer-name
" *tramp temp*"
655 "Buffer name for a temporary buffer.
656 It shall be used in combination with `generate-new-buffer-name'.")
658 (defvar tramp-temp-buffer-file-name nil
659 "File name of a persistent local temporary file.
660 Useful for \"rsync\" like methods.")
661 (make-variable-buffer-local 'tramp-temp-buffer-file-name
)
662 (put 'tramp-temp-buffer-file-name
'permanent-local t
)
665 (defcustom tramp-syntax
'default
666 "Tramp filename syntax to be used.
668 It can have the following values:
670 `default' -- Default syntax
671 `simplified' -- Ange-FTP like syntax
672 `separate' -- Syntax as defined for XEmacs originally
674 Do not change the value by `setq', it must be changed only via
675 Customize. See also `tramp-change-syntax'."
678 :package-version
'(Tramp .
"2.3.3")
679 :type
'(choice (const :tag
"Default" default
)
680 (const :tag
"Ange-FTP" simplified
)
681 (const :tag
"XEmacs" separate
))
683 :initialize
'custom-initialize-set
684 :set
'tramp-set-syntax
)
686 (defun tramp-set-syntax (symbol value
)
687 "Set SYMBOL to value VALUE.
688 Used in user option `tramp-syntax'. There are further variables
689 to be set, depending on VALUE."
690 ;; Check allowed values.
691 (unless (memq value
(tramp-syntax-values))
692 (tramp-compat-user-error "Wrong `tramp-syntax' %s" tramp-syntax
))
693 ;; Cleanup existing buffers.
694 (unless (eq (symbol-value symbol
) value
)
695 (tramp-cleanup-all-buffers))
697 (set-default symbol value
)
698 ;; Reset the depending variables.
700 (setq tramp-prefix-format
(tramp-build-prefix-format)
701 tramp-prefix-regexp
(tramp-build-prefix-regexp)
702 tramp-method-regexp
(tramp-build-method-regexp)
703 tramp-postfix-method-format
(tramp-build-postfix-method-format)
704 tramp-postfix-method-regexp
(tramp-build-postfix-method-regexp)
705 tramp-prefix-ipv6-format
(tramp-build-prefix-ipv6-format)
706 tramp-prefix-ipv6-regexp
(tramp-build-prefix-ipv6-regexp)
707 tramp-postfix-ipv6-format
(tramp-build-postfix-ipv6-format)
708 tramp-postfix-ipv6-regexp
(tramp-build-postfix-ipv6-regexp)
709 tramp-postfix-host-format
(tramp-build-postfix-host-format)
710 tramp-postfix-host-regexp
(tramp-build-postfix-host-regexp)
711 tramp-remote-file-name-spec-regexp
712 (tramp-build-remote-file-name-spec-regexp)
713 tramp-file-name-structure
(tramp-build-file-name-structure)
714 tramp-file-name-regexp
(tramp-build-file-name-regexp)
715 tramp-completion-file-name-regexp
716 (tramp-build-completion-file-name-regexp)))
717 ;; Rearrange file name handlers.
718 (tramp-register-file-name-handlers))
720 ;; Initialize the Tramp syntax variables. We want to override initial
721 ;; value of `tramp-file-name-regexp'. Other Tramp syntax variables
722 ;; must be initialized as well to proper values. We do not call
723 ;; `custom-set-variable', this would load Tramp via custom.el.
724 (eval-after-load 'tramp
725 '(tramp-set-syntax 'tramp-syntax
(tramp-compat-tramp-syntax)))
727 (defun tramp-syntax-values ()
728 "Return possible values of `tramp-syntax', a list"
729 (let ((values (cdr (get 'tramp-syntax
'custom-type
))))
730 (setq values
(mapcar 'last values
)
731 values
(mapcar 'car values
))))
733 (defun tramp-lookup-syntax (alist)
734 "Look up a syntax string in ALIST according to `tramp-compat-tramp-syntax.'
735 Raise an error if `tramp-syntax' is invalid."
736 (or (cdr (assq (tramp-compat-tramp-syntax) alist
))
737 (error "Wrong `tramp-syntax' %s" tramp-syntax
)))
739 (defconst tramp-prefix-format-alist
743 "Alist mapping Tramp syntax to strings beginning Tramp file names.")
745 (defun tramp-build-prefix-format ()
746 (tramp-lookup-syntax tramp-prefix-format-alist
))
748 (defvar tramp-prefix-format
(tramp-build-prefix-format)
749 "String matching the very beginning of Tramp file names.
750 Used in `tramp-make-tramp-file-name'.")
752 (defun tramp-build-prefix-regexp ()
753 (concat "^" (regexp-quote tramp-prefix-format
)))
755 (defvar tramp-prefix-regexp
(tramp-build-prefix-regexp)
756 "Regexp matching the very beginning of Tramp file names.
757 Should always start with \"^\". Derived from `tramp-prefix-format'.")
759 (defconst tramp-method-regexp-alist
760 '((default .
"[a-zA-Z0-9-]+")
762 (separate .
"[a-zA-Z0-9-]*"))
763 "Alist mapping Tramp syntax to regexps matching methods identifiers.")
765 (defun tramp-build-method-regexp ()
766 (tramp-lookup-syntax tramp-method-regexp-alist
))
768 (defvar tramp-method-regexp
(tramp-build-method-regexp)
769 "Regexp matching methods identifiers.
770 The `ftp' syntax does not support methods.")
772 (defconst tramp-postfix-method-format-alist
776 "Alist mapping Tramp syntax to the delimiter after the method.")
778 (defun tramp-build-postfix-method-format ()
779 (tramp-lookup-syntax tramp-postfix-method-format-alist
))
781 (defvar tramp-postfix-method-format
(tramp-build-postfix-method-format)
782 "String matching delimiter between method and user or host names.
783 The `ftp' syntax does not support methods.
784 Used in `tramp-make-tramp-file-name'.")
786 (defun tramp-build-postfix-method-regexp ()
787 (regexp-quote tramp-postfix-method-format
))
789 (defvar tramp-postfix-method-regexp
(tramp-build-postfix-method-regexp)
790 "Regexp matching delimiter between method and user or host names.
791 Derived from `tramp-postfix-method-format'.")
793 (defconst tramp-user-regexp
"[^/|: \t]+"
794 "Regexp matching user names.")
797 (defconst tramp-prefix-domain-format
"%"
798 "String matching delimiter between user and domain names.")
801 (defconst tramp-prefix-domain-regexp
(regexp-quote tramp-prefix-domain-format
)
802 "Regexp matching delimiter between user and domain names.
803 Derived from `tramp-prefix-domain-format'.")
805 (defconst tramp-domain-regexp
"[a-zA-Z0-9_.-]+"
806 "Regexp matching domain names.")
808 (defconst tramp-user-with-domain-regexp
809 (concat "\\(" tramp-user-regexp
"\\)"
810 tramp-prefix-domain-regexp
811 "\\(" tramp-domain-regexp
"\\)")
812 "Regexp matching user names with domain names.")
814 (defconst tramp-postfix-user-format
"@"
815 "String matching delimiter between user and host names.
816 Used in `tramp-make-tramp-file-name'.")
818 (defconst tramp-postfix-user-regexp
(regexp-quote tramp-postfix-user-format
)
819 "Regexp matching delimiter between user and host names.
820 Derived from `tramp-postfix-user-format'.")
822 (defconst tramp-host-regexp
"[a-zA-Z0-9_.%-]+"
823 "Regexp matching host names.")
825 (defconst tramp-prefix-ipv6-format-alist
829 "Alist mapping Tramp syntax to strings prefixing IPv6 addresses.")
831 (defun tramp-build-prefix-ipv6-format ()
832 (tramp-lookup-syntax tramp-prefix-ipv6-format-alist
))
834 (defvar tramp-prefix-ipv6-format
(tramp-build-prefix-ipv6-format)
835 "String matching left hand side of IPv6 addresses.
836 Used in `tramp-make-tramp-file-name'.")
838 (defun tramp-build-prefix-ipv6-regexp ()
839 (regexp-quote tramp-prefix-ipv6-format
))
841 (defvar tramp-prefix-ipv6-regexp
(tramp-build-prefix-ipv6-regexp)
842 "Regexp matching left hand side of IPv6 addresses.
843 Derived from `tramp-prefix-ipv6-format'.")
845 ;; The following regexp is a bit sloppy. But it shall serve our
846 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
847 ;; "::ffff:192.168.0.1".
848 (defconst tramp-ipv6-regexp
"\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
849 "Regexp matching IPv6 addresses.")
851 (defconst tramp-postfix-ipv6-format-alist
855 "Alist mapping Tramp syntax to suffix for IPv6 addresses.")
857 (defun tramp-build-postfix-ipv6-format ()
858 (tramp-lookup-syntax tramp-postfix-ipv6-format-alist
))
860 (defvar tramp-postfix-ipv6-format
(tramp-build-postfix-ipv6-format)
861 "String matching right hand side of IPv6 addresses.
862 Used in `tramp-make-tramp-file-name'.")
864 (defun tramp-build-postfix-ipv6-regexp ()
865 (regexp-quote tramp-postfix-ipv6-format
))
867 (defvar tramp-postfix-ipv6-regexp
(tramp-build-postfix-ipv6-regexp)
868 "Regexp matching right hand side of IPv6 addresses.
869 Derived from `tramp-postfix-ipv6-format'.")
871 (defconst tramp-prefix-port-format
"#"
872 "String matching delimiter between host names and port numbers.")
874 (defconst tramp-prefix-port-regexp
(regexp-quote tramp-prefix-port-format
)
875 "Regexp matching delimiter between host names and port numbers.
876 Derived from `tramp-prefix-port-format'.")
878 (defconst tramp-port-regexp
"[0-9]+"
879 "Regexp matching port numbers.")
881 (defconst tramp-host-with-port-regexp
882 (concat "\\(" tramp-host-regexp
"\\)"
883 tramp-prefix-port-regexp
884 "\\(" tramp-port-regexp
"\\)")
885 "Regexp matching host names with port numbers.")
887 (defconst tramp-postfix-hop-format
"|"
888 "String matching delimiter after ad-hoc hop definitions.")
890 (defconst tramp-postfix-hop-regexp
(regexp-quote tramp-postfix-hop-format
)
891 "Regexp matching delimiter after ad-hoc hop definitions.
892 Derived from `tramp-postfix-hop-format'.")
894 (defconst tramp-postfix-host-format-alist
898 "Alist mapping Tramp syntax to strings between host and local names.")
900 (defun tramp-build-postfix-host-format ()
901 (tramp-lookup-syntax tramp-postfix-host-format-alist
))
903 (defvar tramp-postfix-host-format
(tramp-build-postfix-host-format)
904 "String matching delimiter between host names and localnames.
905 Used in `tramp-make-tramp-file-name'.")
907 (defun tramp-build-postfix-host-regexp ()
908 (regexp-quote tramp-postfix-host-format
))
910 (defvar tramp-postfix-host-regexp
(tramp-build-postfix-host-regexp)
911 "Regexp matching delimiter between host names and localnames.
912 Derived from `tramp-postfix-host-format'.")
914 (defconst tramp-localname-regexp
".*$"
915 "Regexp matching localnames.")
917 (defconst tramp-unknown-id-string
"UNKNOWN"
918 "String used to denote an unknown user or group")
920 (defconst tramp-unknown-id-integer -
1
921 "Integer used to denote an unknown user or group")
923 ;;; File name format:
925 (defun tramp-build-remote-file-name-spec-regexp ()
926 "Construct a regexp matching a Tramp file name for a Tramp syntax.
927 It is expected, that `tramp-syntax' has the proper value."
929 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
930 "\\(?:" "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
"\\)?"
931 "\\(" "\\(?:" tramp-host-regexp
"\\|"
932 tramp-prefix-ipv6-regexp
"\\(?:" tramp-ipv6-regexp
"\\)?"
933 tramp-postfix-ipv6-regexp
"\\)"
934 "\\(?:" tramp-prefix-port-regexp tramp-port-regexp
"\\)?" "\\)?"))
936 (defvar tramp-remote-file-name-spec-regexp
937 (tramp-build-remote-file-name-spec-regexp)
938 "Regular expression matching a Tramp file name between prefix and postfix.")
940 (defun tramp-build-file-name-structure ()
941 "Construct the Tramp file name structure for a Tramp syntax.
942 It is expected, that `tramp-syntax' has the proper value.
943 See `tramp-file-name-structure'."
947 "\\(" "\\(?:" tramp-remote-file-name-spec-regexp
948 tramp-postfix-hop-regexp
"\\)+" "\\)?"
949 tramp-remote-file-name-spec-regexp tramp-postfix-host-regexp
950 "\\(" tramp-localname-regexp
"\\)")
953 (defvar tramp-file-name-structure
(tramp-build-file-name-structure)
954 "List of six elements (REGEXP METHOD USER HOST FILE HOP), detailing \
955 the Tramp file name structure.
957 The first element REGEXP is a regular expression matching a Tramp file
958 name. The regex should contain parentheses around the method name,
959 the user name, the host name, and the file name parts.
961 The second element METHOD is a number, saying which pair of
962 parentheses matches the method name. The third element USER is
963 similar, but for the user name. The fourth element HOST is similar,
964 but for the host name. The fifth element FILE is for the file name.
965 The last element HOP is the ad-hoc hop definition, which could be a
966 cascade of several hops.
968 These numbers are passed directly to `match-string', which see. That
969 means the opening parentheses are counted to identify the pair.
971 See also `tramp-file-name-regexp'.")
973 (defun tramp-build-file-name-regexp ()
974 (car tramp-file-name-structure
))
977 (defconst tramp-initial-file-name-regexp
"\\`/.+:.*:"
978 "Value for `tramp-file-name-regexp' for autoload.
979 It must match the initial `tramp-syntax' settings.")
982 (defvar tramp-file-name-regexp tramp-initial-file-name-regexp
983 "Regular expression matching file names handled by Tramp.
984 This regexp should match Tramp file names but no other file
985 names. When calling `tramp-register-file-name-handlers', the
986 initial value is overwritten by the car of `tramp-file-name-structure'.")
988 (defconst tramp-completion-file-name-regexp-default
991 ;; Optional multi hop.
992 "\\([^/|:]+:[^/|:]*|\\)*"
994 (if (memq system-type
'(cygwin windows-nt
))
995 ;; The method is either "-", or at least two characters.
996 "\\(-\\|[^/|:]\\{2,\\}\\)"
997 ;; At least one character for method.
999 ;; Method separator, user name and host name.
1002 "Value for `tramp-completion-file-name-regexp' for default remoting.
1003 See `tramp-file-name-structure' for more explanations.
1005 On W32 systems, the volume letter must be ignored.")
1007 (defconst tramp-completion-file-name-regexp-simplified
1010 ;; Optional multi hop.
1013 (if (memq system-type
'(cygwin windows-nt
))
1014 ;; At least two characters.
1016 ;; At least one character.
1019 "Value for `tramp-completion-file-name-regexp' for simplified style remoting.
1020 See `tramp-file-name-structure' for more explanations.
1022 On W32 systems, the volume letter must be ignored.")
1024 (defconst tramp-completion-file-name-regexp-separate
1025 "\\`/\\(\\[[^]]*\\)?\\'"
1026 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1027 See `tramp-file-name-structure' for more explanations.")
1029 (defconst tramp-completion-file-name-regexp-alist
1030 `((default .
,tramp-completion-file-name-regexp-default
)
1031 (simplified .
,tramp-completion-file-name-regexp-simplified
)
1032 (separate .
,tramp-completion-file-name-regexp-separate
))
1033 "Alist mapping incomplete Tramp file names.")
1035 (defun tramp-build-completion-file-name-regexp ()
1036 (tramp-lookup-syntax tramp-completion-file-name-regexp-alist
))
1038 (defvar tramp-completion-file-name-regexp
1039 (tramp-build-completion-file-name-regexp)
1040 "Regular expression matching file names handled by Tramp completion.
1041 This regexp should match partial Tramp file names only.
1043 Please note that the entry in `file-name-handler-alist' is made when
1044 this file \(tramp.el) is loaded. This means that this variable must be set
1045 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1046 updated after changing this variable.
1048 Also see `tramp-file-name-structure'.")
1051 (defconst tramp-autoload-file-name-regexp
1054 (if (memq system-type
'(cygwin windows-nt
))
1055 ;; The method is either "-", or at least two characters.
1056 "\\(-\\|[^/|:]\\{2,\\}\\)"
1057 ;; At least one character for method.
1060 "Regular expression matching file names handled by Tramp autoload.
1061 It must match the initial `tramp-syntax' settings. It should not
1062 match file names at root of the underlying local file system,
1063 like \"/sys\" or \"/C:\".")
1065 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1066 ;; known to have a bug in `process-send-string'; some ssh connections appear
1067 ;; to drop bytes when data is sent too quickly. There is also a connection
1068 ;; buffer local variable, which is computed depending on remote host properties
1069 ;; when `tramp-chunksize' is zero or nil.
1070 (defcustom tramp-chunksize
(when (memq system-type
'(hpux)) 500)
1071 ;; Parentheses in docstring starting at beginning of line are escaped.
1072 ;; Fontification is messed up when
1073 ;; `open-paren-in-column-0-is-defun-start' set to t.
1074 "If non-nil, chunksize for sending input to local process.
1075 It is necessary only on systems which have a buggy `process-send-string'
1076 implementation. The necessity, whether this variable must be set, can be
1077 checked via the following code:
1080 (let* ((user \"xxx\") (host \"yyy\")
1082 (sent init) (received init))
1083 (while (= sent received)
1084 (setq sent (+ sent step))
1086 (let ((proc (start-process (buffer-name) (current-buffer)
1087 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1088 (when (process-live-p proc)
1089 (process-send-string proc (make-string sent ?\\ ))
1090 (process-send-eof proc)
1091 (process-send-eof proc))
1092 (while (not (progn (goto-char (point-min))
1093 (re-search-forward \"\\\\w+\" (point-max) t)))
1094 (accept-process-output proc 1))
1095 (when (process-live-p proc)
1096 (setq received (string-to-number (match-string 0)))
1097 (delete-process proc)
1098 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1100 (if (> sent (+ init step))
1101 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1103 (message \"Test does not work\")
1104 (display-buffer (current-buffer))
1107 In the Emacs normally running Tramp, evaluate the above code
1108 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1109 respectively). You can do this, for example, by pasting it into
1110 the `*scratch*' buffer and then hitting C-j with the cursor after the
1111 last closing parenthesis. Note that it works only if you have configured
1112 \"ssh\" to run without password query, see ssh-agent(1).
1114 You will see the number of bytes sent successfully to the remote host.
1115 If that number exceeds 1000, you can stop the execution by hitting
1116 C-g, because your Emacs is likely clean.
1118 When it is necessary to set `tramp-chunksize', you might consider to
1119 use an out-of-the-band method \(like \"scp\") instead of an internal one
1120 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1123 If your Emacs is buggy, the code stops and gives you an indication
1124 about the value `tramp-chunksize' should be set. Maybe you could just
1125 experiment a bit, e.g. changing the values of `init' and `step'
1126 in the third line of the code.
1128 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1129 this variable to be set as well."
1131 :type
'(choice (const nil
) integer
)
1134 ;; Logging in to a remote host normally requires obtaining a pty. But
1135 ;; Emacs on macOS has process-connection-type set to nil by default,
1136 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1137 ;; for an override of the system default.
1138 (defcustom tramp-process-connection-type t
1139 "Overrides `process-connection-type' for connections from Tramp.
1140 Tramp binds `process-connection-type' to the value given here before
1141 opening a connection to a remote host."
1143 :type
'(choice (const nil
) (const t
) (const pty
))
1146 (defcustom tramp-connection-timeout
60
1147 "Defines the max time to wait for establishing a connection (in seconds).
1148 This can be overwritten for different connection types in `tramp-methods'.
1150 The timeout does not include the time reading a password."
1156 (defcustom tramp-connection-min-time-diff
5
1157 "Defines seconds between two consecutive connection attempts.
1158 This is necessary as self defense mechanism, in order to avoid
1159 yo-yo connection attempts when the remote host is unavailable.
1161 A value of 0 or nil suppresses this check. This might be
1162 necessary, when several out-of-order copy operations are
1163 performed, or when several asynchronous processes will be started
1164 in a short time frame. In those cases it is recommended to
1165 let-bind this variable."
1168 :type
'(choice (const nil
) integer
)
1171 (defcustom tramp-completion-reread-directory-timeout
10
1172 "Defines seconds since last remote command before rereading a directory.
1173 A remote directory might have changed its contents. In order to
1174 make it visible during file name completion in the minibuffer,
1175 Tramp flushes its cache and rereads the directory contents when
1176 more than `tramp-completion-reread-directory-timeout' seconds
1177 have been gone since last remote command execution. A value of t
1178 would require an immediate reread during filename completion, nil
1179 means to use always cached values for the directory contents."
1181 :type
'(choice (const nil
) (const t
) integer
)
1184 ;;; Internal Variables:
1186 (defvar tramp-current-connection nil
1187 "Last connection timestamp.")
1189 (defconst tramp-completion-file-name-handler-alist
1190 '((file-name-all-completions
1191 . tramp-completion-handle-file-name-all-completions
)
1192 (file-name-completion . tramp-completion-handle-file-name-completion
))
1193 "Alist of completion handler functions.
1194 Used for file names matching `tramp-completion-file-name-regexp'.
1195 Operations not mentioned here will be handled by Tramp's file
1196 name handler functions, or the normal Emacs functions.")
1198 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1199 ;;;###tramp-autoload
1200 (defvar tramp-foreign-file-name-handler-alist nil
1201 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1202 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1205 ;;; Internal functions which must come first:
1207 ;; Conversion functions between external representation and
1208 ;; internal data structure. Convenience functions for internal
1211 ;; The basic structure for remote file names. We use a list :type,
1212 ;; in order to be compatible with Emacs 24 and 25.
1213 (cl-defstruct (tramp-file-name (:type list
) :named
)
1214 method user domain host port localname hop
)
1216 (defun tramp-file-name-user-domain (vec)
1217 "Return user and domain components of VEC."
1218 (when (or (tramp-file-name-user vec
) (tramp-file-name-domain vec
))
1219 (concat (tramp-file-name-user vec
)
1220 (and (tramp-file-name-domain vec
)
1221 tramp-prefix-domain-format
)
1222 (tramp-file-name-domain vec
))))
1224 (defun tramp-file-name-host-port (vec)
1225 "Return host and port components of VEC."
1226 (when (or (tramp-file-name-host vec
) (tramp-file-name-port vec
))
1227 (concat (tramp-file-name-host vec
)
1228 (and (tramp-file-name-port vec
)
1229 tramp-prefix-port-format
)
1230 (tramp-file-name-port vec
))))
1232 (defun tramp-file-name-port-or-default (vec)
1233 "Return port component of VEC.
1234 If nil, return `tramp-default-port'."
1235 (or (tramp-file-name-port vec
)
1236 (tramp-get-method-parameter vec
'tramp-default-port
)))
1238 (defun tramp-file-name-equal-p (vec1 vec2
)
1239 "Check, whether VEC1 and VEC2 denote the same `tramp-file-name'."
1240 (and (tramp-file-name-p vec1
) (tramp-file-name-p vec2
)
1241 (string-equal (tramp-file-name-method vec1
)
1242 (tramp-file-name-method vec2
))
1243 (string-equal (tramp-file-name-user-domain vec1
)
1244 (tramp-file-name-user-domain vec2
))
1245 (string-equal (tramp-file-name-host-port vec1
)
1246 (tramp-file-name-host-port vec2
))))
1248 (defun tramp-get-method-parameter (vec param
)
1249 "Return the method parameter PARAM.
1250 If VEC is a vector, check first in connection properties.
1251 Afterwards, check in `tramp-methods'. If the `tramp-methods'
1252 entry does not exist, return nil."
1254 (replace-regexp-in-string "^tramp-" "" (symbol-name param
))))
1255 (if (tramp-connection-property-p vec hash-entry
)
1256 ;; We use the cached property.
1257 (tramp-get-connection-property vec hash-entry nil
)
1258 ;; Use the static value from `tramp-methods'.
1259 (let ((methods-entry
1260 (assoc param
(assoc (tramp-file-name-method vec
) tramp-methods
))))
1261 (when methods-entry
(cadr methods-entry
))))))
1263 ;; The localname can be quoted with "/:". Extract this.
1264 (defun tramp-file-name-unquote-localname (vec)
1265 "Return unquoted localname component of VEC."
1266 (tramp-compat-file-name-unquote (tramp-file-name-localname vec
)))
1268 ;;;###tramp-autoload
1269 (defun tramp-tramp-file-p (name)
1270 "Return t if NAME is a string with Tramp file name syntax."
1272 ;; No "/:" and "/c:". This is not covered by `tramp-file-name-regexp'.
1273 (not (string-match-p
1274 (if (memq system-type
'(cygwin windows-nt
))
1275 "^/[[:alpha:]]?:" "^/:")
1277 (string-match-p tramp-file-name-regexp name
)
1280 (defun tramp-find-method (method user host
)
1281 "Return the right method string to use.
1282 This is METHOD, if non-nil. Otherwise, do a lookup in
1283 `tramp-default-method-alist'."
1285 (or (string-equal method
"")
1286 (string-equal method tramp-default-method-marker
)))
1290 (let ((choices tramp-default-method-alist
)
1293 (setq item
(pop choices
))
1294 (when (and (string-match (or (nth 0 item
) "") (or host
""))
1295 (string-match (or (nth 1 item
) "") (or user
"")))
1296 (setq lmethod
(nth 2 item
))
1297 (setq choices nil
)))
1299 tramp-default-method
)))
1300 ;; We must mark, whether a default value has been used.
1301 (if (or method
(null result
))
1303 (propertize result
'tramp-default t
))))
1305 (defun tramp-find-user (method user host
)
1306 "Return the right user string to use.
1307 This is USER, if non-nil. Otherwise, do a lookup in
1308 `tramp-default-user-alist'."
1311 (let ((choices tramp-default-user-alist
)
1314 (setq item
(pop choices
))
1315 (when (and (string-match (or (nth 0 item
) "") (or method
""))
1316 (string-match (or (nth 1 item
) "") (or host
"")))
1317 (setq luser
(nth 2 item
))
1318 (setq choices nil
)))
1320 tramp-default-user
)))
1321 ;; We must mark, whether a default value has been used.
1322 (if (or user
(null result
))
1324 (propertize result
'tramp-default t
))))
1326 (defun tramp-find-host (method user host
)
1327 "Return the right host string to use.
1328 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
1329 (or (and (> (length host
) 0) host
)
1330 (let ((choices tramp-default-host-alist
)
1333 (setq item
(pop choices
))
1334 (when (and (string-match (or (nth 0 item
) "") (or method
""))
1335 (string-match (or (nth 1 item
) "") (or user
"")))
1336 (setq lhost
(nth 2 item
))
1337 (setq choices nil
)))
1339 tramp-default-host
))
1341 (defun tramp-dissect-file-name (name &optional nodefault
)
1342 "Return a `tramp-file-name' structure.
1343 The structure consists of remote method, remote user, remote host,
1344 localname (file name on remote host) and hop. If NODEFAULT is
1345 non-nil, the file name parts are not expanded to their default
1348 (unless (tramp-tramp-file-p name
)
1349 (tramp-compat-user-error nil
"Not a Tramp file name: \"%s\"" name
))
1350 (if (not (string-match (nth 0 tramp-file-name-structure
) name
))
1351 (error "`tramp-file-name-structure' didn't match!")
1352 (let ((method (match-string (nth 1 tramp-file-name-structure
) name
))
1353 (user (match-string (nth 2 tramp-file-name-structure
) name
))
1354 (host (match-string (nth 3 tramp-file-name-structure
) name
))
1355 (localname (match-string (nth 4 tramp-file-name-structure
) name
))
1356 (hop (match-string (nth 5 tramp-file-name-structure
) name
))
1359 (when (string-match tramp-user-with-domain-regexp user
)
1360 (setq domain
(match-string 2 user
)
1361 user
(match-string 1 user
))))
1364 (when (string-match tramp-host-with-port-regexp host
)
1365 (setq port
(match-string 2 host
)
1366 host
(match-string 1 host
)))
1367 (when (string-match tramp-prefix-ipv6-regexp host
)
1368 (setq host
(replace-match "" nil t host
)))
1369 (when (string-match tramp-postfix-ipv6-regexp host
)
1370 (setq host
(replace-match "" nil t host
))))
1373 (setq method
(tramp-find-method method user host
)
1374 user
(tramp-find-user method user host
)
1375 host
(tramp-find-host method user host
)))
1377 (make-tramp-file-name
1378 :method method
:user user
:domain domain
:host host
:port port
1379 :localname localname
:hop hop
)))))
1381 (defun tramp-buffer-name (vec)
1382 "A name for the connection buffer VEC."
1383 (let ((method (tramp-file-name-method vec
))
1384 (user-domain (tramp-file-name-user-domain vec
))
1385 (host-port (tramp-file-name-host-port vec
)))
1386 (if (not (zerop (length user-domain
)))
1387 (format "*tramp/%s %s@%s*" method user-domain host-port
)
1388 (format "*tramp/%s %s*" method host-port
))))
1390 (defun tramp-make-tramp-file-name (&rest args
)
1391 "Construct a Tramp file name from ARGS.
1393 ARGS could have two different signatures. The first one is of
1394 type (VEC &optional LOCALNAME HOP).
1395 If LOCALNAME is nil, the value in VEC is used. If it is a
1396 symbol, a null localname will be used. Otherwise, LOCALNAME is
1397 expected to be a string, which will be used.
1398 If HOP is nil, the value in VEC is used. If it is a symbol, a
1399 null hop will be used. Otherwise, HOP is expected to be a
1400 string, which will be used.
1402 The other signature exists for backward compatibility. It has
1403 the form (METHOD USER DOMAIN HOST PORT LOCALNAME &optional HOP)."
1404 (let (method user domain host port localname hop
)
1406 ((tramp-file-name-p (car args
))
1407 (setq method
(tramp-file-name-method (car args
))
1408 user
(tramp-file-name-user (car args
))
1409 domain
(tramp-file-name-domain (car args
))
1410 host
(tramp-file-name-host (car args
))
1411 port
(tramp-file-name-port (car args
))
1412 localname
(tramp-file-name-localname (car args
))
1413 hop
(tramp-file-name-hop (car args
)))
1415 (setq localname
(and (stringp (cadr args
)) (cadr args
))))
1416 (when (cl-caddr args
)
1417 (setq hop
(and (stringp (cl-caddr args
)) (cl-caddr args
)))))
1419 (t (setq method
(nth 0 args
)
1424 localname
(nth 5 args
)
1427 (when (zerop (length method
))
1428 (signal 'wrong-type-argument
(list 'stringp method
)))
1429 (concat tramp-prefix-format hop
1430 (unless (zerop (length tramp-postfix-method-format
))
1431 (concat method tramp-postfix-method-format
))
1433 (unless (zerop (length domain
))
1434 (concat tramp-prefix-domain-format domain
))
1435 (unless (zerop (length user
))
1436 tramp-postfix-user-format
)
1438 (if (string-match tramp-ipv6-regexp host
)
1440 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
1442 (unless (zerop (length port
))
1443 (concat tramp-prefix-port-format port
))
1444 tramp-postfix-host-format
1447 (defun tramp-completion-make-tramp-file-name (method user host localname
)
1448 "Construct a Tramp file name from METHOD, USER, HOST and LOCALNAME.
1449 It must not be a complete Tramp file name, but as long as there are
1450 necessary only. This function will be used in file name completion."
1451 (concat tramp-prefix-format
1452 (unless (or (zerop (length method
))
1453 (zerop (length tramp-postfix-method-format
)))
1454 (concat method tramp-postfix-method-format
))
1455 (unless (zerop (length user
))
1456 (concat user tramp-postfix-user-format
))
1457 (unless (zerop (length host
))
1459 (if (string-match tramp-ipv6-regexp host
)
1461 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
1463 tramp-postfix-host-format
))
1464 (when localname localname
)))
1466 (defun tramp-get-buffer (vec)
1467 "Get the connection buffer to be used for VEC."
1468 (or (get-buffer (tramp-buffer-name vec
))
1469 (with-current-buffer (get-buffer-create (tramp-buffer-name vec
))
1470 ;; We use the existence of connection property "process-buffer"
1471 ;; as indication, whether a connection is active.
1472 (tramp-set-connection-property
1473 vec
"process-buffer"
1474 (tramp-get-connection-property vec
"process-buffer" nil
))
1475 (setq buffer-undo-list t
1476 default-directory
(tramp-make-tramp-file-name vec
"/" 'nohop
))
1479 (defun tramp-get-connection-buffer (vec)
1480 "Get the connection buffer to be used for VEC.
1481 In case a second asynchronous communication has been started, it is different
1482 from `tramp-get-buffer'."
1483 (or (tramp-get-connection-property vec
"process-buffer" nil
)
1484 (tramp-get-buffer vec
)))
1486 (defun tramp-get-connection-name (vec)
1487 "Get the connection name to be used for VEC.
1488 In case a second asynchronous communication has been started, it is different
1489 from the default one."
1490 (or (tramp-get-connection-property vec
"process-name" nil
)
1491 (tramp-buffer-name vec
)))
1493 (defun tramp-get-connection-process (vec)
1494 "Get the connection process to be used for VEC.
1495 In case a second asynchronous communication has been started, it is different
1496 from the default one."
1497 (and (tramp-file-name-p vec
) (get-process (tramp-get-connection-name vec
))))
1499 (defun tramp-set-connection-local-variables (vec)
1500 "Set connection-local variables in the connection buffer used for VEC.
1501 If connection-local variables are not supported by this Emacs
1502 version, the function does nothing."
1503 (with-current-buffer (tramp-get-connection-buffer vec
)
1504 ;; `hack-connection-local-variables-apply' exists since Emacs 26.1.
1505 (tramp-compat-funcall
1506 'hack-connection-local-variables-apply
1507 `(:application tramp
1508 :protocol
,(tramp-file-name-method vec
)
1509 :user
,(tramp-file-name-user-domain vec
)
1510 :machine
,(tramp-file-name-host-port vec
)))))
1512 (defun tramp-set-connection-local-variables-for-buffer ()
1513 "Set connection-local variables in the current buffer.
1514 If connection-local variables are not supported by this Emacs
1515 version, the function does nothing."
1516 (when (file-remote-p default-directory
)
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
,(file-remote-p default-directory
'method
)
1522 :user
,(file-remote-p default-directory
'user
)
1523 :machine
,(file-remote-p default-directory
'host
)))))
1525 (defun tramp-debug-buffer-name (vec)
1526 "A name for the debug buffer for VEC."
1527 (let ((method (tramp-file-name-method vec
))
1528 (user-domain (tramp-file-name-user-domain vec
))
1529 (host-port (tramp-file-name-host-port vec
)))
1530 (if (not (zerop (length user-domain
)))
1531 (format "*debug tramp/%s %s@%s*" method user-domain host-port
)
1532 (format "*debug tramp/%s %s*" method host-port
))))
1534 (defconst tramp-debug-outline-regexp
1535 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1536 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1538 (defun tramp-debug-outline-level ()
1539 "Return the depth to which a statement is nested in the outline.
1540 Point must be at the beginning of a header line.
1542 The outline level is equal to the verbosity of the Tramp message."
1543 (1+ (string-to-number (match-string 1))))
1545 (defun tramp-get-debug-buffer (vec)
1546 "Get the debug buffer for VEC."
1547 (with-current-buffer
1548 (get-buffer-create (tramp-debug-buffer-name vec
))
1550 (setq buffer-undo-list t
)
1551 ;; So it does not get loaded while `outline-regexp' is let-bound.
1553 ;; Activate `outline-mode'. This runs `text-mode-hook' and
1554 ;; `outline-mode-hook'. We must prevent that local processes
1555 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
1556 ;; Furthermore, `outline-regexp' must have the correct value
1557 ;; already, because it is used by `font-lock-compile-keywords'.
1558 (let ((default-directory (tramp-compat-temporary-file-directory))
1559 (outline-regexp tramp-debug-outline-regexp
))
1561 (set (make-local-variable 'outline-regexp
) tramp-debug-outline-regexp
)
1562 (set (make-local-variable 'outline-level
) 'tramp-debug-outline-level
)
1563 ;; Do not edit the debug buffer.
1564 (set-keymap-parent (current-local-map) special-mode-map
))
1567 (defsubst tramp-debug-message
(vec fmt-string
&rest arguments
)
1568 "Append message to debug buffer.
1569 Message is formatted with FMT-STRING as control string and the remaining
1570 ARGUMENTS to actually emit the message (if applicable)."
1571 (with-current-buffer (tramp-get-debug-buffer vec
)
1572 (goto-char (point-max))
1577 ";; Emacs: %s Tramp: %s -*- mode: outline; -*-"
1578 emacs-version tramp-version
))
1579 (when (>= tramp-verbose
10)
1582 "\n;; Location: %s Git: %s"
1583 (locate-library "tramp") (tramp-repository-get-version)))))
1587 (let ((now (current-time)))
1588 (insert (format-time-string "%T." now
))
1589 (insert (format "%06d " (nth 2 now
))))
1590 ;; Calling Tramp function. We suppress compat and trace functions
1591 ;; from being displayed.
1592 (let ((btn 1) btf fn
)
1594 (setq btf
(nth 1 (backtrace-frame btn
)))
1598 (setq fn
(symbol-name btf
))
1601 (string-match "^tramp" fn
)
1608 "tramp-compat-funcall"
1609 "tramp-compat-user-error"
1610 "tramp-condition-case-unless-debug"
1611 "tramp-debug-message"
1613 "tramp-error-with-buffer"
1619 (setq btn
(1+ btn
))))
1620 ;; The following code inserts filename and line number. Should
1621 ;; be inactive by default, because it is time consuming.
1622 ; (let ((ffn (find-function-noselect (intern fn))))
1626 ; (file-name-nondirectory (buffer-file-name (car ffn)))
1627 ; (with-current-buffer (car ffn)
1628 ; (1+ (count-lines (point-min) (cdr ffn)))))))
1629 (insert (format "%s " fn
)))
1631 (insert (apply #'format-message fmt-string arguments
))))
1633 (defvar tramp-message-show-message
(null noninteractive
)
1634 "Show Tramp message in the minibuffer.
1635 This variable is used to suppress progress reporter output, and
1636 to disable messages from `tramp-error'. Those messages are
1637 visible anyway, because an error is raised.")
1639 (defsubst tramp-message
(vec-or-proc level fmt-string
&rest arguments
)
1640 "Emit a message depending on verbosity level.
1641 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
1642 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1643 less than LEVEL. The message is emitted only if `tramp-verbose' is
1644 greater than or equal to LEVEL.
1646 The message is also logged into the debug buffer when `tramp-verbose'
1647 is greater than or equal 4.
1649 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1650 control string and the remaining ARGUMENTS to actually emit the message (if
1653 (when (<= level tramp-verbose
)
1654 ;; Match data must be preserved!
1656 ;; Display only when there is a minimum level.
1657 (when (and tramp-message-show-message
(<= level
3))
1663 ((= level
2) "Warning: ")
1667 ;; Log only when there is a minimum level.
1668 (when (>= tramp-verbose
4)
1669 (let ((tramp-verbose 0))
1670 ;; Append connection buffer for error messages.
1672 (with-current-buffer
1673 (if (processp vec-or-proc
)
1674 (process-buffer vec-or-proc
)
1675 (tramp-get-connection-buffer vec-or-proc
))
1676 (setq fmt-string
(concat fmt-string
"\n%s")
1677 arguments
(append arguments
(list (buffer-string))))))
1678 ;; Translate proc to vec.
1679 (when (processp vec-or-proc
)
1680 (setq vec-or-proc
(process-get vec-or-proc
'vector
))))
1682 (when (tramp-file-name-p vec-or-proc
)
1683 (apply 'tramp-debug-message
1685 (concat (format "(%d) # " level
) fmt-string
)
1688 (defsubst tramp-backtrace
(&optional vec-or-proc
)
1689 "Dump a backtrace into the debug buffer.
1690 If VEC-OR-PROC is nil, the buffer *debug tramp* is used. This
1691 function is meant for debugging purposes."
1693 (tramp-message vec-or-proc
10 "\n%s" (with-output-to-string (backtrace)))
1694 (if (>= tramp-verbose
10)
1695 (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
1697 (defsubst tramp-error
(vec-or-proc signal fmt-string
&rest arguments
)
1699 VEC-OR-PROC identifies the connection to use, SIGNAL is the
1700 signal identifier to be raised, remaining arguments passed to
1701 `tramp-message'. Finally, signal SIGNAL is raised."
1702 (let (tramp-message-show-message)
1703 (tramp-backtrace vec-or-proc
)
1705 ;; FMT-STRING could be just a file name, as in
1706 ;; `file-already-exists' errors. It could contain the ?\%
1707 ;; character, as in smb domain spec.
1708 (setq arguments
(list fmt-string
)
1713 (error-message-string
1715 (get signal
'error-message
)
1716 (apply #'format-message fmt-string arguments
)))))
1717 (signal signal
(list (apply #'format-message fmt-string arguments
)))))
1719 (defsubst tramp-error-with-buffer
1720 (buf vec-or-proc signal fmt-string
&rest arguments
)
1721 "Emit an error, and show BUF.
1722 If BUF is nil, show the connection buf. Wait for 30\", or until
1723 an input event arrives. The other arguments are passed to `tramp-error'."
1724 (save-window-excursion
1725 (let* ((buf (or (and (bufferp buf
) buf
)
1726 (and (processp vec-or-proc
) (process-buffer vec-or-proc
))
1727 (and (tramp-file-name-p vec-or-proc
)
1728 (tramp-get-connection-buffer vec-or-proc
))))
1729 (vec (or (and (tramp-file-name-p vec-or-proc
) vec-or-proc
)
1730 (and buf
(with-current-buffer buf
1731 (tramp-dissect-file-name default-directory
))))))
1733 (apply 'tramp-error vec-or-proc signal fmt-string arguments
)
1736 tramp-message-show-message
1737 (not (zerop tramp-verbose
))
1738 ;; Do not show when flagged from outside.
1739 (not (tramp-completion-mode-p))
1740 ;; Show only when Emacs has started already.
1742 (let ((enable-recursive-minibuffers t
))
1743 ;; `tramp-error' does not show messages. So we must do it
1745 (apply 'message fmt-string arguments
)
1750 ;; Reset timestamp. It would be wrong after waiting for a while.
1751 (when (tramp-file-name-equal-p vec
(car tramp-current-connection
))
1752 (setcdr tramp-current-connection
(current-time)))))))
1754 (defmacro tramp-with-demoted-errors
(vec-or-proc format
&rest body
)
1755 "Execute BODY while redirecting the error message to `tramp-message'.
1756 BODY is executed like wrapped by `with-demoted-errors'. FORMAT
1757 is a format-string containing a %-sequence meaning to substitute
1758 the resulting error message."
1759 (declare (debug (symbolp body
))
1761 (let ((err (make-symbol "err")))
1762 `(condition-case-unless-debug ,err
1764 (error (tramp-message ,vec-or-proc
3 ,format
,err
) nil
))))
1766 (defmacro with-parsed-tramp-file-name
(filename var
&rest body
)
1767 "Parse a Tramp filename and make components available in the body.
1769 First arg FILENAME is evaluated and dissected into its components.
1770 Second arg VAR is a symbol. It is used as a variable name to hold
1771 the filename structure. It is also used as a prefix for the variables
1772 holding the components. For example, if VAR is the symbol `foo', then
1773 `foo' will be bound to the whole structure, `foo-method' will be bound to
1774 the method component, and so on for `foo-user', `foo-domain', `foo-host',
1775 `foo-port', `foo-localname', `foo-hop'.
1777 Remaining args are Lisp expressions to be evaluated (inside an implicit
1780 If VAR is nil, then we bind `v' to the structure and `method', `user',
1781 `domain', `host', `port', `localname', `hop' to the components."
1783 (mapcar (lambda (elem)
1784 `(,(if var
(intern (format "%s-%s" var elem
)) elem
)
1785 (,(intern (format "tramp-file-name-%s" elem
))
1787 `,(tramp-compat-tramp-file-name-slots))))
1788 `(let* ((,(or var
'v
) (tramp-dissect-file-name ,filename
))
1790 ;; We don't know which of those vars will be used, so we bind them all,
1791 ;; and then add here a dummy use of all those variables, so we don't get
1792 ;; flooded by warnings about those vars `body' didn't use.
1793 (ignore ,@(mapcar #'car bindings
))
1796 (put 'with-parsed-tramp-file-name
'lisp-indent-function
2)
1797 (put 'with-parsed-tramp-file-name
'edebug-form-spec
'(form symbolp body
))
1798 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-parsed-tramp-file-name\\>"))
1800 (defun tramp-progress-reporter-update (reporter &optional value
)
1801 "Report progress of an operation for Tramp."
1802 (let* ((parameters (cdr reporter
))
1803 (message (aref parameters
3)))
1804 (when (string-match message
(or (current-message) ""))
1805 (progress-reporter-update reporter value
))))
1807 (defmacro with-tramp-progress-reporter
(vec level message
&rest body
)
1808 "Executes BODY, spinning a progress reporter with MESSAGE.
1809 If LEVEL does not fit for visible messages, there are only traces
1810 without a visible progress reporter."
1811 (declare (indent 3) (debug t
))
1813 (tramp-message ,vec
,level
"%s..." ,message
)
1814 (let ((cookie "failed")
1816 ;; We start a pulsing progress reporter after 3 seconds.
1817 (when (and tramp-message-show-message
1818 ;; Display only when there is a minimum level.
1819 (<= ,level
(min tramp-verbose
3)))
1820 (let ((pr (make-progress-reporter ,message nil nil
)))
1823 3 0.1 #'tramp-progress-reporter-update pr
))))))
1825 ;; Execute the body.
1826 (prog1 (progn ,@body
) (setq cookie
"done"))
1827 ;; Stop progress reporter.
1828 (if tm
(cancel-timer tm
))
1829 (tramp-message ,vec
,level
"%s...%s" ,message cookie
)))))
1831 (font-lock-add-keywords
1832 'emacs-lisp-mode
'("\\<with-tramp-progress-reporter\\>"))
1834 (defmacro with-tramp-file-property
(vec file property
&rest body
)
1835 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
1836 FILE must be a local file name on a connection identified via VEC."
1837 `(if (file-name-absolute-p ,file
)
1838 (let ((value (tramp-get-file-property ,vec
,file
,property
'undef
)))
1839 (when (eq value
'undef
)
1840 ;; We cannot pass @body as parameter to
1841 ;; `tramp-set-file-property' because it mangles our
1843 (setq value
(progn ,@body
))
1844 (tramp-set-file-property ,vec
,file
,property value
))
1848 (put 'with-tramp-file-property
'lisp-indent-function
3)
1849 (put 'with-tramp-file-property
'edebug-form-spec t
)
1850 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-tramp-file-property\\>"))
1852 (defmacro with-tramp-connection-property
(key property
&rest body
)
1853 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
1854 `(let ((value (tramp-get-connection-property ,key
,property
'undef
)))
1855 (when (eq value
'undef
)
1856 ;; We cannot pass ,@body as parameter to
1857 ;; `tramp-set-connection-property' because it mangles our debug
1859 (setq value
(progn ,@body
))
1860 (tramp-set-connection-property ,key
,property value
))
1863 (put 'with-tramp-connection-property
'lisp-indent-function
2)
1864 (put 'with-tramp-connection-property
'edebug-form-spec t
)
1865 (font-lock-add-keywords
1866 'emacs-lisp-mode
'("\\<with-tramp-connection-property\\>"))
1868 (defun tramp-drop-volume-letter (name)
1869 "Cut off unnecessary drive letter from file NAME.
1870 The functions `tramp-*-handle-expand-file-name' call `expand-file-name'
1871 locally on a remote file name. When the local system is a W32 system
1872 but the remote system is Unix, this introduces a superfluous drive
1873 letter into the file name. This function removes it."
1876 (if (tramp-compat-file-name-quoted-p name
)
1877 'tramp-compat-file-name-quote
'identity
)
1878 (let ((name (tramp-compat-file-name-unquote name
)))
1879 (if (string-match "\\`[a-zA-Z]:/" name
)
1880 (replace-match "/" nil t name
)
1883 ;;; Config Manipulation Functions:
1885 ;;;###tramp-autoload
1886 (defun tramp-set-completion-function (method function-list
)
1887 "Sets the list of completion functions for METHOD.
1888 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
1889 The FUNCTION is intended to parse FILE according its syntax.
1890 It might be a predefined FUNCTION, or a user defined FUNCTION.
1891 For the list of predefined FUNCTIONs see `tramp-completion-function-alist'.
1895 (tramp-set-completion-function
1897 \\='((tramp-parse-sconfig \"/etc/ssh_config\")
1898 (tramp-parse-sconfig \"~/.ssh/config\")))"
1900 (let ((r function-list
)
1902 (setq tramp-completion-function-alist
1903 (delete (assoc method tramp-completion-function-alist
)
1904 tramp-completion-function-alist
))
1907 ;; Remove double entries.
1908 (when (member (car v
) (cdr v
))
1909 (setcdr v
(delete (car v
) (cdr v
))))
1910 ;; Check for function and file or registry key.
1911 (unless (and (functionp (nth 0 (car v
)))
1913 ;; Windows registry.
1914 ((string-match "^HKEY_CURRENT_USER" (nth 1 (car v
)))
1915 (and (memq system-type
'(cygwin windows-nt
))
1918 v
"reg" nil nil nil
"query" (nth 1 (car v
))))))
1919 ;; Zeroconf service type.
1921 "^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v
))))
1922 ;; Configuration file.
1923 (t (file-exists-p (nth 1 (car v
))))))
1924 (setq r
(delete (car v
) r
)))
1928 (add-to-list 'tramp-completion-function-alist
1931 (defun tramp-get-completion-function (method)
1932 "Returns a list of completion functions for METHOD.
1933 For definition of that list see `tramp-set-completion-function'."
1935 `(;; Default settings are taken into account.
1936 (tramp-parse-default-user-host ,method
)
1937 ;; Hosts visited once shall be remembered.
1938 (tramp-parse-connection-properties ,method
))
1939 ;; The method related defaults.
1940 (cdr (assoc method tramp-completion-function-alist
))))
1943 ;;; Fontification of `read-file-name':
1945 (defvar tramp-rfn-eshadow-overlay
)
1946 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay
)
1948 (defun tramp-rfn-eshadow-setup-minibuffer ()
1949 "Set up a minibuffer for `file-name-shadow-mode'.
1950 Adds another overlay hiding filename parts according to Tramp's
1951 special handling of `substitute-in-file-name'."
1952 (when (symbol-value 'minibuffer-completing-file-name
)
1953 (setq tramp-rfn-eshadow-overlay
1954 (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
1955 ;; Copy rfn-eshadow-overlay properties.
1956 (let ((props (overlay-properties (symbol-value 'rfn-eshadow-overlay
))))
1958 ;; The `field' property prevents correct minibuffer
1959 ;; completion; we exclude it.
1960 (if (not (eq (car props
) 'field
))
1961 (overlay-put tramp-rfn-eshadow-overlay
(pop props
) (pop props
))
1962 (pop props
) (pop props
))))))
1964 (add-hook 'rfn-eshadow-setup-minibuffer-hook
1965 'tramp-rfn-eshadow-setup-minibuffer
)
1966 (add-hook 'tramp-unload-hook
1968 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
1969 'tramp-rfn-eshadow-setup-minibuffer
)))
1971 (defun tramp-rfn-eshadow-update-overlay-regexp ()
1972 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format
))
1974 (defun tramp-rfn-eshadow-update-overlay ()
1975 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
1976 This is intended to be used as a minibuffer `post-command-hook' for
1977 `file-name-shadow-mode'; the minibuffer should have already
1978 been set up by `rfn-eshadow-setup-minibuffer'."
1979 ;; In remote files name, there is a shadowing just for the local part.
1981 (let ((end (or (overlay-end (symbol-value 'rfn-eshadow-overlay
))
1982 (minibuffer-prompt-end)))
1983 ;; We do not want to send any remote command.
1987 (buffer-substring-no-properties end
(point-max)))
1991 (1+ (or (string-match
1992 (tramp-rfn-eshadow-update-overlay-regexp)
1993 (buffer-string) end
)
1996 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay
)
1997 (rfn-eshadow-update-overlay-hook nil
)
1998 file-name-handler-alist
)
1999 (move-overlay rfn-eshadow-overlay
(point-max) (point-max))
2000 (rfn-eshadow-update-overlay))))))))
2002 (add-hook 'rfn-eshadow-update-overlay-hook
2003 'tramp-rfn-eshadow-update-overlay
)
2004 (add-hook 'tramp-unload-hook
2006 (remove-hook 'rfn-eshadow-update-overlay-hook
2007 'tramp-rfn-eshadow-update-overlay
)))
2009 ;; Inodes don't exist for some file systems. Therefore we must
2010 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2011 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2012 ;; performance isn't the major issue given that file transfer will
2014 (defvar tramp-inodes
0
2015 "Keeps virtual inodes numbers.")
2017 ;; Devices must distinguish physical file systems. The device numbers
2018 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2019 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2020 ;; EFS use device number "-1". In order to be different, we use device number
2021 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2022 (defvar tramp-devices
0
2023 "Keeps virtual device numbers.")
2025 (defun tramp-default-file-modes (filename)
2026 "Return file modes of FILENAME as integer.
2027 If the file modes of FILENAME cannot be determined, return the
2028 value of `default-file-modes', without execute permissions."
2029 (or (file-modes filename
)
2030 (logand (default-file-modes) (string-to-number "0666" 8))))
2032 (defun tramp-replace-environment-variables (filename)
2033 "Replace environment variables in FILENAME.
2034 Return the string with the replaced variables."
2036 ;; Optional arg has been introduced with Emacs 24.4.
2037 (tramp-compat-funcall 'substitute-env-vars filename
'only-defined
))
2038 ;; We need an own implementation.
2040 (let ((idx (string-match "$\\(\\w+\\)" filename
)))
2041 ;; `$' is coded as `$$'.
2043 (or (zerop idx
) (not (eq ?$
(aref filename
(1- idx
)))))
2044 (getenv (match-string 1 filename
)))
2047 (substitute-in-file-name (match-string 0 filename
))
2051 (defun tramp-find-file-name-coding-system-alist (filename tmpname
)
2052 "Like `find-operation-coding-system' for Tramp filenames.
2053 Tramp's `insert-file-contents' and `write-region' work over
2054 temporary file names. If `file-coding-system-alist' contains an
2055 expression, which matches more than the file name suffix, the
2056 coding system might not be determined. This function repairs it."
2058 (dolist (elt file-coding-system-alist
(nreverse result
))
2059 (when (and (consp elt
) (string-match (car elt
) filename
))
2060 ;; We found a matching entry in `file-coding-system-alist'.
2061 ;; So we add a similar entry, but with the temporary file name
2063 (push (cons (regexp-quote tmpname
) (cdr elt
)) result
)))))
2065 (defun tramp-run-real-handler (operation args
)
2066 "Invoke normal file name handler for OPERATION.
2067 First arg specifies the OPERATION, second arg is a list of arguments to
2068 pass to the OPERATION."
2069 (let* ((inhibit-file-name-handlers
2070 `(tramp-file-name-handler
2071 tramp-vc-file-name-handler
2072 tramp-completion-file-name-handler
2073 tramp-archive-file-name-handler
2074 cygwin-mount-name-hook-function
2075 cygwin-mount-map-drive-hook-function
2077 ,(and (eq inhibit-file-name-operation operation
)
2078 inhibit-file-name-handlers
)))
2079 (inhibit-file-name-operation operation
))
2080 (apply operation args
)))
2082 ;; We handle here all file primitives. Most of them have the file
2083 ;; name as first parameter; nevertheless we check for them explicitly
2084 ;; in order to be signaled if a new primitive appears. This
2085 ;; scenario is needed because there isn't a way to decide by
2086 ;; syntactical means whether a foreign method must be called. It would
2087 ;; ease the life if `file-name-handler-alist' would support a decision
2088 ;; function as well but regexp only.
2089 (defun tramp-file-name-for-operation (operation &rest args
)
2090 "Return file name related to OPERATION file primitive.
2091 ARGS are the arguments OPERATION has been called with."
2093 ;; FILE resp DIRECTORY.
2095 '(access-file byte-compiler-base-file-name delete-directory
2096 delete-file diff-latest-backup-file directory-file-name
2097 directory-files directory-files-and-attributes
2098 dired-compress-file dired-uncache file-acl
2099 file-accessible-directory-p file-attributes
2100 file-directory-p file-executable-p file-exists-p
2101 file-local-copy file-modes file-name-as-directory
2102 file-name-directory file-name-nondirectory
2103 file-name-sans-versions file-notify-add-watch
2104 file-ownership-preserved-p file-readable-p
2105 file-regular-p file-remote-p file-selinux-context
2106 file-symlink-p file-truename file-writable-p
2107 find-backup-file-name find-file-noselect get-file-buffer
2108 insert-directory insert-file-contents load
2109 make-directory make-directory-internal set-file-acl
2110 set-file-modes set-file-selinux-context set-file-times
2111 substitute-in-file-name unhandled-file-name-directory
2114 file-name-case-insensitive-p
2117 (if (file-name-absolute-p (nth 0 args
))
2120 ;; FILE DIRECTORY resp FILE1 FILE2.
2122 '(add-name-to-file copy-directory copy-file expand-file-name
2123 file-equal-p file-in-directory-p
2124 file-name-all-completions file-name-completion
2125 ;; Starting with Emacs 26.1, just the 2nd argument of
2126 ;; `make-symbolic-link' matters. For backward
2127 ;; compatibility, we still accept the first argument as
2128 ;; file name to be checked. Handled properly in
2129 ;; `tramp-handle-*-make-symbolic-link'.
2130 file-newer-than-file-p make-symbolic-link rename-file
))
2133 ((tramp-tramp-file-p (nth 0 args
)) (nth 0 args
))
2134 ((tramp-tramp-file-p (nth 1 args
)) (nth 1 args
))
2135 (t default-directory
))))
2137 ((eq operation
'write-region
)
2138 (if (file-name-absolute-p (nth 2 args
))
2143 '(make-auto-save-file-name
2144 set-visited-file-modtime verify-visited-file-modtime
))
2146 (if (bufferp (nth 0 args
)) (nth 0 args
) (current-buffer))))
2149 '(process-file shell-command start-file-process
2151 make-nearby-temp-file temporary-file-directory
))
2155 '(file-notify-rm-watch
2157 file-notify-valid-p
))
2158 (when (processp (nth 0 args
))
2159 (with-current-buffer (process-buffer (nth 0 args
))
2160 default-directory
)))
2161 ;; Unknown file primitive.
2162 (t (error "unknown file I/O primitive: %s" operation
))))
2164 (defun tramp-find-foreign-file-name-handler (filename &optional _operation
)
2165 "Return foreign file name handler if exists."
2166 (when (tramp-tramp-file-p filename
)
2167 (let ((handler tramp-foreign-file-name-handler-alist
)
2170 (setq elt
(car handler
)
2171 handler
(cdr handler
))
2172 (when (funcall (car elt
) filename
)
2177 (defvar tramp-debug-on-error nil
2178 "Like `debug-on-error' but used Tramp internal.")
2180 (defmacro tramp-condition-case-unless-debug
2181 (var bodyform
&rest handlers
)
2182 "Like `condition-case-unless-debug' but `tramp-debug-on-error'."
2183 `(let ((debug-on-error tramp-debug-on-error
))
2184 (condition-case-unless-debug ,var
,bodyform
,@handlers
)))
2186 ;; In Emacs, there is some concurrency due to timers. If a timer
2187 ;; interrupts Tramp and wishes to use the same connection buffer as
2188 ;; the "main" Emacs, then garbage might occur in the connection
2189 ;; buffer. Therefore, we need to make sure that a timer does not use
2190 ;; the same connection buffer as the "main" Emacs. We implement a
2191 ;; cheap global lock, instead of locking each connection buffer
2192 ;; separately. The global lock is based on two variables,
2193 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
2194 ;; (with setq) to indicate a lock. But Tramp also calls itself during
2195 ;; processing of a single file operation, so we need to allow
2196 ;; recursive calls. That's where the `tramp-locker' variable comes in
2197 ;; -- it is let-bound to t during the execution of the current
2198 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
2199 ;; then we should just proceed because we have been called
2200 ;; recursively. But if `tramp-locker' is nil, then we are a timer
2201 ;; interrupting the "main" Emacs, and then we signal an error.
2203 (defvar tramp-locked nil
2204 "If non-nil, then Tramp is currently busy.
2205 Together with `tramp-locker', this implements a locking mechanism
2206 preventing reentrant calls of Tramp.")
2208 (defvar tramp-locker nil
2209 "If non-nil, then a caller has locked Tramp.
2210 Together with `tramp-locked', this implements a locking mechanism
2211 preventing reentrant calls of Tramp.")
2214 (defun tramp-file-name-handler (operation &rest args
)
2215 "Invoke Tramp file name handler.
2216 Falls back to normal file name handler if no Tramp file name handler exists."
2217 (let ((filename (apply 'tramp-file-name-for-operation operation args
)))
2218 (if (and tramp-mode
(tramp-tramp-file-p filename
))
2220 (setq filename
(tramp-replace-environment-variables filename
))
2221 (with-parsed-tramp-file-name filename nil
2222 (let ((completion (tramp-completion-mode-p))
2224 (tramp-find-foreign-file-name-handler filename operation
))
2226 ;; Call the backend function.
2228 (tramp-condition-case-unless-debug err
2229 (let ((sf (symbol-function foreign
)))
2230 ;; Some packages set the default directory to a
2231 ;; remote path, before respective Tramp packages
2232 ;; are already loaded. This results in
2233 ;; recursive loading. Therefore, we load the
2234 ;; Tramp packages locally.
2235 (when (autoloadp sf
)
2236 (let ((default-directory
2237 (tramp-compat-temporary-file-directory)))
2238 (load (cadr sf
) 'noerror
'nomessage
)))
2240 ;; v 4 "Running `%s'..." (cons operation args))
2241 ;; If `non-essential' is non-nil, Tramp shall
2242 ;; not open a new connection.
2243 ;; If Tramp detects that it shouldn't continue
2244 ;; to work, it throws the `suppress' event.
2245 ;; This could happen for example, when Tramp
2246 ;; tries to open the same connection twice in a
2247 ;; short time frame.
2248 ;; In both cases, we try the default handler then.
2250 (catch 'non-essential
2252 (when (and tramp-locked
(not tramp-locker
))
2253 (setq tramp-locked nil
)
2255 (car-safe tramp-current-connection
)
2257 "Forbidden reentrant call of Tramp"))
2258 (let ((tl tramp-locked
))
2259 (setq tramp-locked t
)
2261 (let ((tramp-locker t
))
2262 (apply foreign operation args
))
2263 (setq tramp-locked tl
))))))
2265 ;; v 4 "Running `%s'...`%s'" (cons operation args) result)
2267 ((eq result
'non-essential
)
2269 v
5 "Non-essential received in operation %s"
2270 (cons operation args
))
2271 (tramp-run-real-handler operation args
))
2272 ((eq result
'suppress
)
2273 (let (tramp-message-show-message)
2275 v
1 "Suppress received in operation %s"
2276 (cons operation args
))
2277 (tramp-cleanup-connection v t
)
2278 (tramp-run-real-handler operation args
)))
2281 ;; Trace that somebody has interrupted the operation.
2283 (let (tramp-message-show-message)
2285 v
1 "Interrupt received in operation %s"
2286 (cons operation args
)))
2287 ;; Propagate the quit signal.
2288 (signal (car err
) (cdr err
)))
2290 ;; When we are in completion mode, some failed
2291 ;; operations shall return at least a default
2292 ;; value in order to give the user a chance to
2293 ;; correct the file name in the minibuffer.
2294 ;; In order to get a full backtrace, one could apply
2295 ;; (setq tramp-debug-on-error t)
2298 ((and completion
(zerop (length localname
))
2299 (memq operation
'(file-exists-p file-directory-p
)))
2301 ((and completion
(zerop (length localname
))
2303 '(expand-file-name file-name-as-directory
)))
2305 ;; Propagate the error.
2306 (t (signal (car err
) (cdr err
))))))
2308 ;; Nothing to do for us. However, since we are in
2309 ;; `tramp-mode', we must suppress the volume letter on
2311 (setq result
(tramp-run-real-handler operation args
))
2312 (if (stringp result
)
2313 (tramp-drop-volume-letter result
)
2316 ;; When `tramp-mode' is not enabled, or the file name is quoted,
2317 ;; we don't do anything.
2318 (tramp-run-real-handler operation args
))))
2320 (defun tramp-completion-file-name-handler (operation &rest args
)
2321 "Invoke Tramp file name completion handler.
2322 Falls back to normal file name handler if no Tramp file name handler exists."
2323 (let ((fn (assoc operation tramp-completion-file-name-handler-alist
)))
2324 (if (and fn tramp-mode
)
2325 (save-match-data (apply (cdr fn
) args
))
2326 (tramp-run-real-handler operation args
))))
2329 (progn (defun tramp-autoload-file-name-handler (operation &rest args
)
2330 "Load Tramp file name handler, and perform OPERATION."
2332 (let ((default-directory temporary-file-directory
))
2333 (load "tramp" 'noerror
'nomessage
))
2334 (tramp-unload-file-name-handlers))
2335 (apply operation args
)))
2337 ;; `tramp-autoload-file-name-handler' must be registered before
2338 ;; evaluation of site-start and init files, because there might exist
2339 ;; remote files already, f.e. files kept via recentf-mode.
2341 (progn (defun tramp-register-autoload-file-name-handlers ()
2342 "Add Tramp file name handlers to `file-name-handler-alist' during autoload."
2343 (add-to-list 'file-name-handler-alist
2344 (cons tramp-autoload-file-name-regexp
2345 'tramp-autoload-file-name-handler
))
2346 (put 'tramp-autoload-file-name-handler
'safe-magic t
)))
2348 ;;;###autoload (tramp-register-autoload-file-name-handlers)
2350 (defun tramp-use-absolute-autoload-file-names ()
2351 "Change Tramp autoload objects to use absolute file names.
2352 This avoids problems during autoload, when `load-path' contains
2354 ;; We expect all other Tramp files in the same directory as tramp.el.
2355 (let* ((dir (expand-file-name (file-name-directory (locate-library "tramp"))))
2361 'file-name-sans-extension
2362 (directory-files dir nil
"^tramp.+\\.elc?$"))
2366 (when (and (functionp atom
)
2367 (autoloadp (symbol-function atom
))
2368 (string-match files-regexp
(cadr (symbol-function atom
))))
2370 (setf (cadr (symbol-function atom
))
2371 (expand-file-name (cadr (symbol-function atom
)) dir
))))))))
2373 (eval-after-load 'tramp
(tramp-use-absolute-autoload-file-names))
2375 (defun tramp-register-file-name-handlers ()
2376 "Add Tramp file name handlers to `file-name-handler-alist'."
2377 ;; Remove autoloaded handlers from file name handler alist. Useful,
2378 ;; if `tramp-syntax' has been changed. We cannot call
2379 ;; `tramp-unload-file-name-handlers', this would result in recursive
2380 ;; loading of Tramp.
2381 (dolist (fnh '(tramp-file-name-handler
2382 tramp-completion-file-name-handler
2383 tramp-archive-file-name-handler
2384 tramp-autoload-file-name-handler
))
2385 (let ((a1 (rassq fnh file-name-handler-alist
)))
2386 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
))))
2388 ;; Add the handlers. We do not add anything to the `operations'
2389 ;; property of `tramp-file-name-handler' and
2390 ;; `tramp-archive-file-name-handler', this shall be done by the
2391 ;; respective foreign handlers.
2392 (add-to-list 'file-name-handler-alist
2393 (cons tramp-file-name-regexp
'tramp-file-name-handler
))
2394 (put 'tramp-file-name-handler
'safe-magic t
)
2396 (add-to-list 'file-name-handler-alist
2397 (cons tramp-completion-file-name-regexp
2398 'tramp-completion-file-name-handler
))
2399 (put 'tramp-completion-file-name-handler
'safe-magic t
)
2400 ;; Mark `operations' the handler is responsible for.
2401 (put 'tramp-completion-file-name-handler
'operations
2402 (mapcar 'car tramp-completion-file-name-handler-alist
))
2404 (when (bound-and-true-p tramp-archive-enabled
)
2405 (add-to-list 'file-name-handler-alist
2406 (cons tramp-archive-file-name-regexp
2407 'tramp-archive-file-name-handler
))
2408 (put 'tramp-archive-file-name-handler
'safe-magic t
))
2410 ;; If jka-compr or epa-file are already loaded, move them to the
2411 ;; front of `file-name-handler-alist'.
2412 (dolist (fnh '(epa-file-handler jka-compr-handler
))
2413 (let ((entry (rassoc fnh file-name-handler-alist
)))
2415 (setq file-name-handler-alist
2416 (cons entry
(delete entry file-name-handler-alist
)))))))
2418 (eval-after-load 'tramp
(tramp-register-file-name-handlers))
2420 ;;;###tramp-autoload
2421 (progn (defun tramp-register-foreign-file-name-handler
2422 (func handler
&optional append
)
2423 "Register (FUNC . HANDLER) in `tramp-foreign-file-name-handler-alist'.
2424 FUNC is the function, which determines whether HANDLER is to be called.
2425 Add operations defined in `HANDLER-alist' to `tramp-file-name-handler'."
2427 'tramp-foreign-file-name-handler-alist
`(,func .
,handler
) append
)
2428 ;; Mark `operations' the handler is responsible for.
2429 (put 'tramp-file-name-handler
2433 (get 'tramp-file-name-handler
'operations
)
2436 (symbol-value (intern (concat (symbol-name handler
) "-alist")))))))))
2438 (defun tramp-exists-file-name-handler (operation &rest args
)
2439 "Check, whether OPERATION runs a file name handler."
2440 ;; The file name handler is determined on base of either an
2441 ;; argument, `buffer-file-name', or `default-directory'.
2443 (let* ((buffer-file-name "/")
2444 (default-directory "/")
2445 (fnha file-name-handler-alist
)
2446 (check-file-name-operation operation
)
2447 (file-name-handler-alist
2450 (lambda (operation &rest args
)
2451 "Returns OPERATION if it is the one to be checked."
2452 (if (equal check-file-name-operation operation
)
2454 (let ((file-name-handler-alist fnha
))
2455 (apply operation args
))))))))
2456 (equal (apply operation args
) operation
))))
2459 (progn (defun tramp-unload-file-name-handlers ()
2460 "Unload Tramp file name handlers from `file-name-handler-alist'."
2461 (dolist (fnh '(tramp-file-name-handler
2462 tramp-completion-file-name-handler
2463 tramp-archive-file-name-handler
2464 tramp-autoload-file-name-handler
))
2465 (let ((a1 (rassq fnh file-name-handler-alist
)))
2466 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
))))))
2468 (add-hook 'tramp-unload-hook
'tramp-unload-file-name-handlers
)
2470 ;;; File name handler functions for completion mode:
2473 (defvar tramp-completion-mode nil
2474 "If non-nil, external packages signal that they are in file name completion.")
2475 (make-obsolete-variable 'tramp-completion-mode
'non-essential
"26.1")
2477 (defun tramp-completion-mode-p ()
2478 "Check, whether method / user name / host name completion is active."
2480 ;; Signal from outside.
2482 ;; This variable has been obsoleted in Emacs 26.
2483 tramp-completion-mode
))
2485 (defun tramp-connectable-p (filename)
2486 "Check, whether it is possible to connect the remote host w/o side-effects.
2487 This is true, if either the remote host is already connected, or if we are
2488 not in completion mode."
2489 (let (tramp-verbose)
2490 (and (tramp-tramp-file-p filename
)
2491 (or (not (tramp-completion-mode-p))
2493 (tramp-get-connection-process
2494 (tramp-dissect-file-name filename
)))))))
2496 ;; Method, host name and user name completion.
2497 ;; `tramp-completion-dissect-file-name' returns a list of
2498 ;; `tramp-file-name' structures. For all of them we return possible
2500 (defun tramp-completion-handle-file-name-all-completions (filename directory
)
2501 "Like `file-name-all-completions' for partial Tramp files."
2504 (tramp-drop-volume-letter (expand-file-name filename directory
)))
2507 ;; Suppress hop from completion.
2511 "\\(" "\\(" tramp-remote-file-name-spec-regexp
2512 tramp-postfix-hop-regexp
2515 (setq hop
(match-string 1 fullname
)
2516 fullname
(replace-match "" nil nil fullname
1)))
2518 ;; Possible completion structures.
2519 (dolist (elt (tramp-completion-dissect-file-name fullname
))
2520 (let* ((method (tramp-file-name-method elt
))
2521 (user (tramp-file-name-user elt
))
2522 (host (tramp-file-name-host elt
))
2523 (localname (tramp-file-name-localname elt
))
2524 (m (tramp-find-method method user host
))
2527 (unless localname
;; Nothing to complete.
2531 ;; Method dependent user / host combinations.
2535 (setq all-user-hosts
2536 (append all-user-hosts
2537 (funcall (nth 0 x
) (nth 1 x
)))))
2538 (tramp-get-completion-function m
))
2544 (tramp-get-completion-user-host
2545 method user host
(nth 0 x
) (nth 1 x
)))
2546 (delq nil all-user-hosts
)))))
2548 ;; Possible methods.
2550 (append result
(tramp-get-completion-methods m
)))))))
2552 ;; Unify list, add hop, remove nil elements.
2553 (dolist (elt result
)
2555 (string-match tramp-prefix-regexp elt
)
2556 (setq elt
(replace-match (concat tramp-prefix-format hop
) nil nil elt
))
2558 (substring elt
(length (tramp-drop-volume-letter directory
)))
2561 ;; Complete local parts.
2565 (tramp-run-real-handler
2566 'file-name-all-completions
(list filename directory
))))))
2568 ;; Method, host name and user name completion for a file.
2569 (defun tramp-completion-handle-file-name-completion
2570 (filename directory
&optional predicate
)
2571 "Like `file-name-completion' for Tramp files."
2574 (mapcar 'list
(file-name-all-completions filename directory
))
2575 (when (and predicate
2576 (tramp-connectable-p (expand-file-name filename directory
)))
2577 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
2579 ;; I misuse a little bit the `tramp-file-name' structure in order to
2580 ;; handle completion possibilities for partial methods / user names /
2581 ;; host names. Return value is a list of `tramp-file-name' structures
2582 ;; according to possible completions. If "localname" is non-nil it
2583 ;; means there shouldn't be a completion anymore.
2585 ;; Expected results:
2588 ;; ["x" nil nil nil]
2590 ;; "/x:" "/[x/" "/x:y" "/[x/y" "/x:y:" "/[x/y]"
2591 ;; ["x" nil "" nil] ["x" nil "y" nil] ["x" nil "y" ""]
2592 ;; ["x" "" nil nil] ["x" "y" nil nil]
2594 ;; "/x:y@""/[x/y@" "/x:y@z" "/[x/y@z" "/x:y@z:" "/[x/y@z]"
2595 ;;["x" "y" nil nil] ["x" "y" "z" nil] ["x" "y" "z" ""]
2596 (defun tramp-completion-dissect-file-name (name)
2597 "Returns a list of `tramp-file-name' structures.
2598 They are collected by `tramp-completion-dissect-file-name1'."
2600 (let* ((x-nil "\\|\\(\\)")
2601 (tramp-completion-ipv6-regexp
2604 (if (zerop (length tramp-postfix-ipv6-format
))
2605 tramp-postfix-host-format
2606 tramp-postfix-ipv6-format
)))
2607 ;; "/method" "/[method"
2608 (tramp-completion-file-name-structure1
2612 "\\(" tramp-method-regexp x-nil
"\\)$")
2614 ;; "/method:user" "/[method/user"
2615 (tramp-completion-file-name-structure2
2619 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2620 "\\(" tramp-user-regexp x-nil
"\\)$")
2622 ;; "/method:host" "/[method/host"
2623 (tramp-completion-file-name-structure3
2627 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2628 "\\(" tramp-host-regexp x-nil
"\\)$")
2630 ;; "/method:[ipv6" "/[method/ipv6"
2631 (tramp-completion-file-name-structure4
2635 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2636 tramp-prefix-ipv6-regexp
2637 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
2639 ;; "/method:user@host" "/[method/user@host"
2640 (tramp-completion-file-name-structure5
2644 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2645 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
2646 "\\(" tramp-host-regexp x-nil
"\\)$")
2648 ;; "/method:user@[ipv6" "/[method/user@ipv6"
2649 (tramp-completion-file-name-structure6
2653 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
2654 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
2655 tramp-prefix-ipv6-regexp
2656 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
2661 (lambda (structure) (tramp-completion-dissect-file-name1 structure name
))
2663 tramp-completion-file-name-structure1
2664 tramp-completion-file-name-structure2
2665 tramp-completion-file-name-structure3
2666 tramp-completion-file-name-structure4
2667 tramp-completion-file-name-structure5
2668 tramp-completion-file-name-structure6
)))))
2670 (defun tramp-completion-dissect-file-name1 (structure name
)
2671 "Returns a `tramp-file-name' structure matching STRUCTURE.
2672 The structure consists of remote method, remote user,
2673 remote host and localname (filename on remote host)."
2676 (when (string-match (nth 0 structure
) name
)
2677 (make-tramp-file-name
2678 :method
(and (nth 1 structure
)
2679 (match-string (nth 1 structure
) name
))
2680 :user
(and (nth 2 structure
)
2681 (match-string (nth 2 structure
) name
))
2682 :host
(and (nth 3 structure
)
2683 (match-string (nth 3 structure
) name
))))))
2685 ;; This function returns all possible method completions, adding the
2686 ;; trailing method delimiter.
2687 (defun tramp-get-completion-methods (partial-method)
2688 "Returns all method completions for PARTIAL-METHOD."
2692 (string-match (concat "^" (regexp-quote partial-method
)) method
)
2693 (tramp-completion-make-tramp-file-name method nil nil nil
)))
2694 (mapcar 'car tramp-methods
)))
2696 ;; Compares partial user and host names with possible completions.
2697 (defun tramp-get-completion-user-host
2698 (method partial-user partial-host user host
)
2699 "Returns the most expanded string for user and host name completion.
2700 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
2703 ((and partial-user partial-host
)
2705 (string-match (concat "^" (regexp-quote partial-host
)) host
)
2706 (string-equal partial-user
(or user partial-user
)))
2707 (setq user partial-user
)
2714 (and user
(string-match (concat "^" (regexp-quote partial-user
)) user
))
2720 (and host
(string-match (concat "^" (regexp-quote partial-host
)) host
))
2726 (unless (zerop (+ (length user
) (length host
)))
2727 (tramp-completion-make-tramp-file-name method user host nil
)))
2729 (defun tramp-parse-default-user-host (method)
2730 "Return a list of (user host) tuples allowed to access for METHOD.
2731 This function is added always in `tramp-get-completion-function'
2732 for all methods. Resulting data are derived from default settings."
2733 `((,(tramp-find-user method nil nil
) ,(tramp-find-host method nil nil
))))
2735 ;; Generic function.
2736 (defun tramp-parse-group (regexp match-level skip-regexp
)
2737 "Return a (user host) tuple allowed to access.
2738 User is always nil."
2740 (when (re-search-forward regexp
(point-at-eol) t
)
2741 (setq result
(list nil
(match-string match-level
))))
2743 (> (skip-chars-forward skip-regexp
) 0)
2747 ;; Generic function.
2748 (defun tramp-parse-file (filename function
)
2749 "Return a list of (user host) tuples allowed to access.
2750 User is always nil."
2751 ;; On Windows, there are problems in completion when
2752 ;; `default-directory' is remote.
2753 (let ((default-directory (tramp-compat-temporary-file-directory)))
2754 (when (file-readable-p filename
)
2756 (insert-file-contents filename
)
2757 (goto-char (point-min))
2758 (cl-loop while
(not (eobp)) collect
(funcall function
))))))
2760 ;;;###tramp-autoload
2761 (defun tramp-parse-rhosts (filename)
2762 "Return a list of (user host) tuples allowed to access.
2763 Either user or host may be nil."
2764 (tramp-parse-file filename
'tramp-parse-rhosts-group
))
2766 (defun tramp-parse-rhosts-group ()
2767 "Return a (user host) tuple allowed to access.
2768 Either user or host may be nil."
2772 "^\\(" tramp-host-regexp
"\\)"
2773 "\\([ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
2774 (when (re-search-forward regexp
(point-at-eol) t
)
2775 (setq result
(append (list (match-string 3) (match-string 1)))))
2779 ;;;###tramp-autoload
2780 (defun tramp-parse-shosts (filename)
2781 "Return a list of (user host) tuples allowed to access.
2782 User is always nil."
2783 (tramp-parse-file filename
'tramp-parse-shosts-group
))
2785 (defun tramp-parse-shosts-group ()
2786 "Return a (user host) tuple allowed to access.
2787 User is always nil."
2788 (tramp-parse-group (concat "^\\(" tramp-host-regexp
"\\)") 1 ","))
2790 ;;;###tramp-autoload
2791 (defun tramp-parse-sconfig (filename)
2792 "Return a list of (user host) tuples allowed to access.
2793 User is always nil."
2794 (tramp-parse-file filename
'tramp-parse-sconfig-group
))
2796 (defun tramp-parse-sconfig-group ()
2797 "Return a (user host) tuple allowed to access.
2798 User is always nil."
2800 (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp
"\\)") 1 ","))
2802 ;; Generic function.
2803 (defun tramp-parse-shostkeys-sknownhosts (dirname regexp
)
2804 "Return a list of (user host) tuples allowed to access.
2805 User is always nil."
2806 ;; On Windows, there are problems in completion when
2807 ;; `default-directory' is remote.
2808 (let* ((default-directory (tramp-compat-temporary-file-directory))
2809 (files (and (file-directory-p dirname
) (directory-files dirname
))))
2812 when
(and (not (string-match "^\\.\\.?$" f
)) (string-match regexp f
))
2813 collect
(list nil
(match-string 1 f
)))))
2815 ;;;###tramp-autoload
2816 (defun tramp-parse-shostkeys (dirname)
2817 "Return a list of (user host) tuples allowed to access.
2818 User is always nil."
2819 (tramp-parse-shostkeys-sknownhosts
2820 dirname
(concat "^key_[0-9]+_\\(" tramp-host-regexp
"\\)\\.pub$")))
2822 ;;;###tramp-autoload
2823 (defun tramp-parse-sknownhosts (dirname)
2824 "Return a list of (user host) tuples allowed to access.
2825 User is always nil."
2826 (tramp-parse-shostkeys-sknownhosts
2828 (concat "^\\(" tramp-host-regexp
"\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")))
2830 ;;;###tramp-autoload
2831 (defun tramp-parse-hosts (filename)
2832 "Return a list of (user host) tuples allowed to access.
2833 User is always nil."
2834 (tramp-parse-file filename
'tramp-parse-hosts-group
))
2836 (defun tramp-parse-hosts-group ()
2837 "Return a (user host) tuple allowed to access.
2838 User is always nil."
2840 (concat "^\\(" tramp-ipv6-regexp
"\\|" tramp-host-regexp
"\\)") 1 " \t"))
2842 ;;;###tramp-autoload
2843 (defun tramp-parse-passwd (filename)
2844 "Return a list of (user host) tuples allowed to access.
2845 Host is always \"localhost\"."
2846 (with-tramp-connection-property nil
"parse-passwd"
2847 (if (executable-find "getent")
2849 (when (zerop (tramp-call-process nil
"getent" nil t nil
"passwd"))
2850 (goto-char (point-min))
2851 (cl-loop while
(not (eobp)) collect
2852 (tramp-parse-etc-group-group))))
2853 (tramp-parse-file filename
'tramp-parse-passwd-group
))))
2855 (defun tramp-parse-passwd-group ()
2856 "Return a (user host) tuple allowed to access.
2857 Host is always \"localhost\"."
2859 (regexp (concat "^\\(" tramp-user-regexp
"\\):")))
2860 (when (re-search-forward regexp
(point-at-eol) t
)
2861 (setq result
(list (match-string 1) "localhost")))
2865 ;;;###tramp-autoload
2866 (defun tramp-parse-etc-group (filename)
2867 "Return a list of (group host) tuples allowed to access.
2868 Host is always \"localhost\"."
2869 (with-tramp-connection-property nil
"parse-group"
2870 (if (executable-find "getent")
2872 (when (zerop (tramp-call-process nil
"getent" nil t nil
"group"))
2873 (goto-char (point-min))
2874 (cl-loop while
(not (eobp)) collect
2875 (tramp-parse-etc-group-group))))
2876 (tramp-parse-file filename
'tramp-parse-etc-group-group
))))
2878 (defun tramp-parse-etc-group-group ()
2879 "Return a (group host) tuple allowed to access.
2880 Host is always \"localhost\"."
2882 (split (split-string (buffer-substring (point) (point-at-eol)) ":")))
2883 (when (member (user-login-name) (split-string (nth 3 split
) "," 'omit
))
2884 (setq result
(list (nth 0 split
) "localhost")))
2888 ;;;###tramp-autoload
2889 (defun tramp-parse-netrc (filename)
2890 "Return a list of (user host) tuples allowed to access.
2892 (tramp-parse-file filename
'tramp-parse-netrc-group
))
2894 (defun tramp-parse-netrc-group ()
2895 "Return a (user host) tuple allowed to access.
2900 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp
"\\)"
2901 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
2902 (when (re-search-forward regexp
(point-at-eol) t
)
2903 (setq result
(list (match-string 3) (match-string 1))))
2907 ;;;###tramp-autoload
2908 (defun tramp-parse-putty (registry-or-dirname)
2909 "Return a list of (user host) tuples allowed to access.
2910 User is always nil."
2911 (if (memq system-type
'(windows-nt))
2912 (with-tramp-connection-property nil
"parse-putty"
2914 (when (zerop (tramp-call-process
2915 nil
"reg" nil t nil
"query" registry-or-dirname
))
2916 (goto-char (point-min))
2917 (cl-loop while
(not (eobp)) collect
2918 (tramp-parse-putty-group registry-or-dirname
)))))
2920 (tramp-parse-shostkeys-sknownhosts
2921 registry-or-dirname
(concat "^\\(" tramp-host-regexp
"\\)$"))))
2923 (defun tramp-parse-putty-group (registry)
2924 "Return a (user host) tuple allowed to access.
2925 User is always nil."
2927 (regexp (concat (regexp-quote registry
) "\\\\\\(.+\\)")))
2928 (when (re-search-forward regexp
(point-at-eol) t
)
2929 (setq result
(list nil
(match-string 1))))
2933 ;;; Common file name handler functions for different backends:
2935 (defvar tramp-handle-file-local-copy-hook nil
2936 "Normal hook to be run at the end of `tramp-*-handle-file-local-copy'.")
2938 (defvar tramp-handle-write-region-hook nil
2939 "Normal hook to be run at the end of `tramp-*-handle-write-region'.")
2941 (defun tramp-handle-add-name-to-file
2942 (filename newname
&optional ok-if-already-exists
)
2943 "Like `add-name-to-file' for Tramp files."
2944 (with-parsed-tramp-file-name
2945 (if (tramp-tramp-file-p newname
) newname filename
) nil
2946 (unless (tramp-equal-remote filename newname
)
2949 "add-name-to-file: %s"
2950 "only implemented for same method, same user, same host"))
2951 ;; Do the 'confirm if exists' thing.
2952 (when (file-exists-p newname
)
2954 (if (or (null ok-if-already-exists
) ; not allowed to exist
2955 (and (numberp ok-if-already-exists
)
2958 "File %s already exists; make it a link anyway? "
2960 (tramp-error v
'file-already-exists newname
)
2961 (delete-file newname
)))
2962 (tramp-flush-file-properties v
(file-name-directory localname
))
2963 (tramp-flush-file-properties v localname
)
2965 filename newname
'ok-if-already-exists
'keep-time
2966 'preserve-uid-gid
'preserve-permissions
)))
2968 (defun tramp-handle-directory-file-name (directory)
2969 "Like `directory-file-name' for Tramp files."
2970 ;; If localname component of filename is "/", leave it unchanged.
2971 ;; Otherwise, remove any trailing slash from localname component.
2972 ;; Method, host, etc, are unchanged.
2973 (while (with-parsed-tramp-file-name directory nil
2974 (and (not (zerop (length localname
)))
2975 (eq (aref localname
(1- (length localname
))) ?
/)
2976 (not (string= localname
"/"))))
2977 (setq directory
(substring directory
0 -
1)))
2980 (defun tramp-handle-directory-files (directory &optional full match nosort
)
2981 "Like `directory-files' for Tramp files."
2982 (when (file-directory-p directory
)
2983 (setq directory
(file-name-as-directory (expand-file-name directory
)))
2984 (let ((temp (nreverse (file-name-all-completions "" directory
)))
2988 (setq item
(directory-file-name (pop temp
)))
2989 (when (or (null match
) (string-match match item
))
2990 (push (if full
(concat directory item
) item
)
2992 (if nosort result
(sort result
'string
<)))))
2994 (defun tramp-handle-directory-files-and-attributes
2995 (directory &optional full match nosort id-format
)
2996 "Like `directory-files-and-attributes' for Tramp files."
2999 (cons x
(file-attributes
3000 (if full x
(expand-file-name x directory
)) id-format
)))
3001 (directory-files directory full match nosort
)))
3003 (defun tramp-handle-dired-uncache (dir)
3004 "Like `dired-uncache' for Tramp files."
3005 (with-parsed-tramp-file-name
3006 (if (file-directory-p dir
) dir
(file-name-directory dir
)) nil
3007 (tramp-flush-directory-properties v localname
)))
3009 (defun tramp-handle-file-accessible-directory-p (filename)
3010 "Like `file-accessible-directory-p' for Tramp files."
3011 (and (file-directory-p filename
)
3012 (file-readable-p filename
)))
3014 (defun tramp-handle-file-directory-p (filename)
3015 "Like `file-directory-p' for Tramp files."
3016 (eq (tramp-compat-file-attribute-type
3017 (file-attributes (file-truename filename
)))
3020 (defun tramp-handle-file-equal-p (filename1 filename2
)
3021 "Like `file-equalp-p' for Tramp files."
3022 ;; Native `file-equalp-p' calls `file-truename', which requires a
3023 ;; remote connection. This can be avoided, if FILENAME1 and
3024 ;; FILENAME2 are not located on the same remote host.
3026 (file-remote-p (expand-file-name filename1
))
3027 (file-remote-p (expand-file-name filename2
)))
3028 (tramp-run-real-handler 'file-equal-p
(list filename1 filename2
))))
3030 (defun tramp-handle-file-exists-p (filename)
3031 "Like `file-exists-p' for Tramp files."
3032 (not (null (file-attributes filename
))))
3034 (defun tramp-handle-file-in-directory-p (filename directory
)
3035 "Like `file-in-directory-p' for Tramp files."
3036 ;; Native `file-in-directory-p' calls `file-truename', which
3037 ;; requires a remote connection. This can be avoided, if FILENAME
3038 ;; and DIRECTORY are not located on the same remote host.
3040 (file-remote-p (expand-file-name filename
))
3041 (file-remote-p (expand-file-name directory
)))
3042 (tramp-run-real-handler 'file-in-directory-p
(list filename directory
))))
3044 (defun tramp-handle-file-modes (filename)
3045 "Like `file-modes' for Tramp files."
3046 (let ((truename (or (file-truename filename
) filename
)))
3047 (when (file-exists-p truename
)
3048 (tramp-mode-string-to-int
3049 (tramp-compat-file-attribute-modes (file-attributes truename
))))))
3051 ;; Localname manipulation functions that grok Tramp localnames...
3052 (defun tramp-handle-file-name-as-directory (file)
3053 "Like `file-name-as-directory' but aware of Tramp files."
3054 ;; `file-name-as-directory' would be sufficient except localname is
3055 ;; the empty string.
3056 (let ((v (tramp-dissect-file-name file t
)))
3057 ;; Run the command on the localname portion only unless we are in
3059 (tramp-make-tramp-file-name
3060 v
(unless (and (zerop (length (tramp-file-name-localname v
)))
3061 (not (tramp-connectable-p file
)))
3062 (tramp-run-real-handler
3063 'file-name-as-directory
3064 (list (or (tramp-file-name-localname v
) "")))))))
3066 (defun tramp-handle-file-name-case-insensitive-p (filename)
3067 "Like `file-name-case-insensitive-p' for Tramp files."
3068 ;; We make it a connection property, assuming that all file systems
3069 ;; on the remote host behave similar. This might be wrong for
3070 ;; mounted NFS directories or SMB/AFP shares; such more granular
3071 ;; tests will be added in case they are needed.
3072 (setq filename
(expand-file-name filename
))
3073 (with-parsed-tramp-file-name filename nil
3074 (or ;; Maybe there is a default value.
3075 (tramp-get-method-parameter v
'tramp-case-insensitive
)
3077 ;; There isn't. So we must check, in case there's a connection already.
3078 (and (file-remote-p filename nil
'connected
)
3079 (with-tramp-connection-property v
"case-insensitive"
3081 (with-tramp-progress-reporter v
5 "Checking case-insensitive"
3082 ;; The idea is to compare a file with lower case
3083 ;; letters with the same file with upper case letters.
3085 (tramp-compat-file-name-unquote
3086 (directory-file-name filename
)))
3088 ;; Check, whether we find an existing file with
3089 ;; lower case letters. This avoids us to create a
3091 (while (and (string-match
3092 "[a-z]" (file-remote-p candidate
'localname
))
3093 (not (file-exists-p candidate
)))
3095 (directory-file-name
3096 (file-name-directory candidate
))))
3097 ;; Nothing found, so we must use a temporary file
3098 ;; for comparison. `make-nearby-temp-file' is added
3099 ;; to Emacs 26+ like `file-name-case-insensitive-p',
3100 ;; so there is no compatibility problem calling it.
3103 "[a-z]" (file-remote-p candidate
'localname
))
3105 (let ((default-directory
3106 (file-name-directory filename
)))
3107 (tramp-compat-funcall
3108 'make-nearby-temp-file
"tramp."))
3110 ;; Check for the existence of the same file with
3111 ;; upper case letters.
3115 (file-remote-p candidate
)
3116 (upcase (file-remote-p candidate
'localname
))))
3118 (when tmpfile
(delete-file tmpfile
)))))))))))
3120 (defun tramp-handle-file-name-completion
3121 (filename directory
&optional predicate
)
3122 "Like `file-name-completion' for Tramp files."
3123 (let (hits-ignored-extensions)
3126 filename
(file-name-all-completions filename directory
)
3128 (when (funcall (or predicate
'identity
) (expand-file-name x directory
))
3131 completion-ignored-extensions
3133 (concat (regexp-opt completion-ignored-extensions
'paren
) "$") x
)
3134 ;; We remember the hit.
3135 (push x hits-ignored-extensions
))))))
3136 ;; No match. So we try again for ignored files.
3137 (try-completion filename hits-ignored-extensions
))))
3139 (defun tramp-handle-file-name-directory (file)
3140 "Like `file-name-directory' but aware of Tramp files."
3141 ;; Everything except the last filename thing is the directory. We
3142 ;; cannot apply `with-parsed-tramp-file-name', because this expands
3143 ;; the remote file name parts. This is a problem when we are in
3144 ;; file name completion.
3145 (let ((v (tramp-dissect-file-name file t
)))
3146 ;; Run the command on the localname portion only.
3147 (tramp-make-tramp-file-name
3148 v
(tramp-run-real-handler
3149 'file-name-directory
(list (or (tramp-file-name-localname v
) ""))))))
3151 (defun tramp-handle-file-name-nondirectory (file)
3152 "Like `file-name-nondirectory' but aware of Tramp files."
3153 (with-parsed-tramp-file-name file nil
3154 (tramp-run-real-handler 'file-name-nondirectory
(list localname
))))
3156 (defun tramp-handle-file-newer-than-file-p (file1 file2
)
3157 "Like `file-newer-than-file-p' for Tramp files."
3159 ((not (file-exists-p file1
)) nil
)
3160 ((not (file-exists-p file2
)) t
)
3161 (t (time-less-p (tramp-compat-file-attribute-modification-time
3162 (file-attributes file2
))
3163 (tramp-compat-file-attribute-modification-time
3164 (file-attributes file1
))))))
3166 (defun tramp-handle-file-regular-p (filename)
3167 "Like `file-regular-p' for Tramp files."
3168 (and (file-exists-p filename
)
3170 (aref (tramp-compat-file-attribute-modes (file-attributes filename
))
3173 (defun tramp-handle-file-remote-p (filename &optional identification connected
)
3174 "Like `file-remote-p' for Tramp files."
3175 ;; We do not want traces in the debug buffer.
3176 (let ((tramp-verbose (min tramp-verbose
3)))
3177 (when (tramp-tramp-file-p filename
)
3178 (let* ((v (tramp-dissect-file-name filename
))
3179 (p (tramp-get-connection-process v
))
3180 (c (and (process-live-p p
)
3181 (tramp-get-connection-property p
"connected" nil
))))
3182 ;; We expand the file name only, if there is already a connection.
3183 (with-parsed-tramp-file-name
3184 (if c
(expand-file-name filename
) filename
) nil
3185 (and (or (not connected
) c
)
3187 ((eq identification
'method
) method
)
3188 ;; Domain and port are appended to user and host,
3190 ((eq identification
'user
) (tramp-file-name-user-domain v
))
3191 ((eq identification
'host
) (tramp-file-name-host-port v
))
3192 ((eq identification
'localname
) localname
)
3193 ((eq identification
'hop
) hop
)
3194 (t (tramp-make-tramp-file-name
3195 method user domain host port
"" hop
)))))))))
3197 (defun tramp-handle-file-selinux-context (_filename)
3198 "Like `file-selinux-context' for Tramp files."
3199 ;; Return nil context.
3202 (defun tramp-handle-file-symlink-p (filename)
3203 "Like `file-symlink-p' for Tramp files."
3204 (let ((x (tramp-compat-file-attribute-type (file-attributes filename
))))
3205 (and (stringp x
) x
)))
3207 (defun tramp-handle-file-truename (filename)
3208 "Like `file-truename' for Tramp files."
3209 (let ((result (expand-file-name filename
))
3211 ;; Don't make the following value larger than
3212 ;; necessary. People expect an error message in a
3213 ;; timely fashion when something is wrong;
3214 ;; otherwise they might think that Emacs is hung.
3215 ;; Of course, correctness has to come first.
3220 (with-parsed-tramp-file-name result v1
3221 (with-tramp-file-property v1 v1-localname
"file-truename"
3222 (while (and (setq symlink-target
(file-symlink-p result
))
3223 (< numchase numchase-limit
))
3224 (setq numchase
(1+ numchase
)
3226 (with-parsed-tramp-file-name (expand-file-name result
) v2
3227 (tramp-make-tramp-file-name
3228 v2-method v2-user v2-domain v2-host v2-port
3230 (if (tramp-compat-file-name-quoted-p v2-localname
)
3231 'tramp-compat-file-name-quote
'identity
)
3233 (if (stringp symlink-target
)
3234 (if (file-remote-p symlink-target
)
3235 (let (file-name-handler-alist)
3236 (tramp-compat-file-name-quote symlink-target
))
3238 symlink-target
(file-name-directory v2-localname
)))
3240 (when (>= numchase numchase-limit
)
3243 "Maximum number (%d) of symlinks exceeded" numchase-limit
)))
3244 (directory-file-name result
)))
3246 ;; Preserve trailing "/".
3247 (if (string-equal (file-name-nondirectory filename
) "") "/" ""))))
3249 (defun tramp-handle-find-backup-file-name (filename)
3250 "Like `find-backup-file-name' for Tramp files."
3251 (with-parsed-tramp-file-name filename nil
3252 (let ((backup-directory-alist
3253 (if tramp-backup-directory-alist
3258 (if (and (stringp (cdr x
))
3259 (file-name-absolute-p (cdr x
))
3260 (not (tramp-tramp-file-p (cdr x
))))
3261 (tramp-make-tramp-file-name
3262 method user domain host port
(cdr x
) hop
)
3264 tramp-backup-directory-alist
)
3265 backup-directory-alist
)))
3266 (tramp-run-real-handler 'find-backup-file-name
(list filename
)))))
3268 (defun tramp-handle-insert-directory
3269 (filename switches
&optional wildcard full-directory-p
)
3270 "Like `insert-directory' for Tramp files."
3271 (unless switches
(setq switches
""))
3272 ;; Mark trailing "/".
3273 (when (and (zerop (length (file-name-nondirectory filename
)))
3274 (not full-directory-p
))
3275 (setq switches
(concat switches
"F")))
3276 (with-parsed-tramp-file-name (expand-file-name filename
) nil
3277 (with-tramp-progress-reporter v
0 (format "Opening directory %s" filename
)
3279 (let (ls-lisp-use-insert-directory-program start
)
3280 (tramp-run-real-handler
3282 (list filename switches wildcard full-directory-p
))
3283 ;; `ls-lisp' always returns full listings. We must remove
3284 ;; superfluous parts.
3285 (unless (string-match "l" switches
)
3287 (goto-char (point-min))
3289 (text-property-not-all
3290 (point) (point-at-eol) 'dired-filename t
))
3293 (or (text-property-any start
(point-at-eol) 'dired-filename t
)
3295 (if (= (point-at-bol) (point-at-eol))
3297 (delete-region (point) (progn (forward-line) (point)))
3298 (forward-line)))))))))
3300 (defun tramp-handle-insert-file-contents
3301 (filename &optional visit beg end replace
)
3302 "Like `insert-file-contents' for Tramp files."
3303 (barf-if-buffer-read-only)
3304 (setq filename
(expand-file-name filename
))
3305 (let (result local-copy remote-copy
)
3306 (with-parsed-tramp-file-name filename nil
3308 (if (not (file-exists-p filename
))
3310 v tramp-file-missing
3311 "File `%s' not found on remote host" filename
)
3313 (with-tramp-progress-reporter
3314 v
3 (format-message "Inserting `%s'" filename
)
3316 (if (and (tramp-local-host-p v
)
3317 (let (file-name-handler-alist)
3318 (file-readable-p localname
)))
3319 ;; Short track: if we are on the local host, we can
3322 (tramp-run-real-handler
3323 'insert-file-contents
3324 (list localname visit beg end replace
)))
3326 ;; When we shall insert only a part of the file, we
3327 ;; copy this part. This works only for the shell file
3329 (when (and (or beg end
)
3330 (tramp-get-method-parameter
3331 v
'tramp-login-program
))
3332 (setq remote-copy
(tramp-make-tramp-temp-file v
))
3333 ;; This is defined in tramp-sh.el. Let's assume
3334 ;; this is loaded already.
3335 (tramp-compat-funcall
3340 (format "dd bs=1 skip=%d if=%s count=%d of=%s"
3341 beg
(tramp-shell-quote-argument localname
)
3342 (- end beg
) remote-copy
))
3344 (format "dd bs=1 skip=%d if=%s of=%s"
3345 beg
(tramp-shell-quote-argument localname
)
3348 (format "dd bs=1 count=%d if=%s of=%s"
3349 end
(tramp-shell-quote-argument localname
)
3351 (setq tramp-temp-buffer-file-name nil beg nil end nil
))
3353 ;; `insert-file-contents-literally' takes care to
3354 ;; avoid calling jka-compr.el and epa.el. By
3355 ;; let-binding `inhibit-file-name-operation', we
3356 ;; propagate that care to the `file-local-copy'
3359 (let ((inhibit-file-name-operation
3360 (when (eq inhibit-file-name-operation
3361 'insert-file-contents
)
3364 ((stringp remote-copy
)
3366 (tramp-make-tramp-file-name
3367 method user domain host port remote-copy
)))
3368 ((stringp tramp-temp-buffer-file-name
)
3370 filename tramp-temp-buffer-file-name
'ok
)
3371 tramp-temp-buffer-file-name
)
3372 (t (file-local-copy filename
)))))
3374 ;; When the file is not readable for the owner, it
3375 ;; cannot be inserted, even if it is readable for the
3376 ;; group or for everybody.
3377 (set-file-modes local-copy
(string-to-number "0600" 8))
3379 (when (and (null remote-copy
)
3380 (tramp-get-method-parameter
3381 v
'tramp-copy-keep-tmpfile
))
3382 ;; We keep the local file for performance reasons,
3383 ;; useful for "rsync".
3384 (setq tramp-temp-buffer-file-name local-copy
))
3386 ;; We must ensure that `file-coding-system-alist'
3387 ;; matches `local-copy'.
3388 (let ((file-coding-system-alist
3389 (tramp-find-file-name-coding-system-alist
3390 filename local-copy
)))
3392 (insert-file-contents
3393 local-copy visit beg end replace
))))
3395 (add-hook 'find-file-not-found-functions
3396 `(lambda () (signal ',(car err
) ',(cdr err
)))
3398 (signal (car err
) (cdr err
))))))
3403 (setq buffer-file-name filename
)
3404 (setq buffer-read-only
(not (file-writable-p filename
)))
3405 (set-visited-file-modtime)
3406 (set-buffer-modified-p nil
))
3407 (when (and (stringp local-copy
)
3408 (or remote-copy
(null tramp-temp-buffer-file-name
)))
3409 (delete-file local-copy
))
3410 (when (stringp remote-copy
)
3412 (tramp-make-tramp-file-name
3413 method user domain host port remote-copy
)))))
3416 (list (expand-file-name filename
)
3419 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix
)
3420 "Like `load' for Tramp files."
3421 (with-parsed-tramp-file-name (expand-file-name file
) nil
3423 (cond ((file-exists-p (concat file
".elc"))
3424 (setq file
(concat file
".elc")))
3425 ((file-exists-p (concat file
".el"))
3426 (setq file
(concat file
".el")))))
3428 ;; The first condition is always true for absolute file names.
3429 ;; Included for safety's sake.
3430 (unless (or (file-name-directory file
)
3431 (string-match "\\.elc?\\'" file
))
3434 "File `%s' does not include a `.el' or `.elc' suffix" file
)))
3436 (when (not (file-exists-p file
))
3438 v tramp-file-missing
"Cannot load nonexistent file `%s'" file
)))
3439 (if (not (file-exists-p file
))
3441 (let ((tramp-message-show-message (not nomessage
)))
3442 (with-tramp-progress-reporter v
0 (format "Loading %s" file
)
3443 (let ((local-copy (file-local-copy file
)))
3445 (load local-copy noerror t nosuffix must-suffix
)
3446 (delete-file local-copy
)))))
3449 (defun tramp-handle-make-symbolic-link
3450 (target linkname
&optional ok-if-already-exists
)
3451 "Like `make-symbolic-link' for Tramp files.
3452 This is the fallback implementation for backends which do not
3453 support symbolic links."
3454 (if (tramp-tramp-file-p (expand-file-name linkname
))
3456 (tramp-dissect-file-name (expand-file-name linkname
)) 'file-error
3457 "make-symbolic-link not supported")
3458 ;; This is needed prior Emacs 26.1, where TARGET has also be
3459 ;; checked for a file name handler.
3460 (tramp-run-real-handler
3461 'make-symbolic-link
(list target linkname ok-if-already-exists
))))
3463 (defun tramp-handle-shell-command
3464 (command &optional output-buffer error-buffer
)
3465 "Like `shell-command' for Tramp files."
3466 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
3467 ;; We cannot use `shell-file-name' and `shell-command-switch',
3468 ;; they are variables of the local host.
3471 (tramp-get-method-parameter
3472 (tramp-dissect-file-name default-directory
)
3473 'tramp-remote-shell
)
3474 (tramp-get-method-parameter
3475 (tramp-dissect-file-name default-directory
)
3476 'tramp-remote-shell-args
))
3477 (list (substring command
0 asynchronous
))))
3481 ((bufferp output-buffer
) output-buffer
)
3482 ((stringp output-buffer
) (get-buffer-create output-buffer
))
3484 (setq current-buffer-p t
)
3486 (t (get-buffer-create
3488 "*Async Shell Command*"
3489 "*Shell Command Output*")))))
3492 ((bufferp error-buffer
) error-buffer
)
3493 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
3495 (if (and (not asynchronous
) error-buffer
)
3496 (with-parsed-tramp-file-name default-directory nil
3497 (list output-buffer
(tramp-make-tramp-temp-file v
)))
3499 (p (get-buffer-process output-buffer
)))
3501 ;; Check whether there is another process running. Tramp does not
3502 ;; support 2 (asynchronous) processes in parallel.
3504 (if (yes-or-no-p "A command is running. Kill it? ")
3505 (ignore-errors (kill-process p
))
3506 (tramp-compat-user-error p
"Shell command in progress")))
3508 (if current-buffer-p
3510 (barf-if-buffer-read-only)
3512 (with-current-buffer output-buffer
3513 (setq buffer-read-only nil
)
3516 (if (and (not current-buffer-p
) (integerp asynchronous
))
3519 (setq p
(apply 'start-file-process
"*Async Shell*" buffer args
))
3521 (with-current-buffer output-buffer
3522 (display-buffer output-buffer
'(nil (allow-no-window . t
)))
3523 (setq mode-line-process
'(":%s"))
3525 (set-process-sentinel p
'shell-command-sentinel
)
3526 (set-process-filter p
'comint-output-filter
)))
3530 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
3531 ;; Insert error messages if they were separated.
3532 (when (listp buffer
)
3533 (with-current-buffer error-buffer
3534 (insert-file-contents (cadr buffer
)))
3535 (delete-file (cadr buffer
)))
3536 (if current-buffer-p
3537 ;; This is like exchange-point-and-mark, but doesn't
3538 ;; activate the mark. It is cleaner to avoid activation,
3539 ;; even though the command loop would deactivate the mark
3540 ;; because we inserted text.
3541 (goto-char (prog1 (mark t
)
3542 (set-marker (mark-marker) (point)
3544 ;; There's some output, display it.
3545 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
3546 (display-message-or-buffer output-buffer
)))))))
3548 (defun tramp-handle-substitute-in-file-name (filename)
3549 "Like `substitute-in-file-name' for Tramp files.
3550 \"//\" and \"/~\" substitute only in the local filename part."
3551 ;; Check, whether the local part is a quoted file name.
3552 (if (tramp-compat-file-name-quoted-p filename
)
3554 ;; First, we must replace environment variables.
3555 (setq filename
(tramp-replace-environment-variables filename
))
3556 (with-parsed-tramp-file-name filename nil
3557 ;; We do not want to replace environment variables, again.
3558 (let (process-environment)
3559 ;; Ignore in LOCALNAME everything before "//" or "/~".
3560 (when (stringp localname
)
3561 (if (string-match "//\\(/\\|~\\)" localname
)
3562 (setq filename
(substitute-in-file-name localname
))
3564 (concat (file-remote-p filename
)
3565 (substitute-in-file-name localname
))))))
3566 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
3567 (if (and (stringp localname
) (string-equal "~" localname
))
3568 (concat filename
"/")
3571 (defun tramp-handle-set-visited-file-modtime (&optional time-list
)
3572 "Like `set-visited-file-modtime' for Tramp files."
3573 (unless (buffer-file-name)
3574 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
3577 (let ((remote-file-name-inhibit-cache t
))
3578 ;; '(-1 65535) means file doesn't exists yet.
3580 (or (tramp-compat-file-attribute-modification-time
3581 (file-attributes (buffer-file-name)))
3583 ;; We use '(0 0) as a don't-know value.
3584 (unless (equal time-list
'(0 0))
3585 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))))
3587 (defun tramp-handle-verify-visited-file-modtime (&optional buf
)
3588 "Like `verify-visited-file-modtime' for Tramp files.
3589 At the time `verify-visited-file-modtime' calls this function, we
3590 already know that the buffer is visiting a file and that
3591 `visited-file-modtime' does not return 0. Do not call this
3592 function directly, unless those two cases are already taken care
3594 (with-current-buffer (or buf
(current-buffer))
3595 (let ((f (buffer-file-name)))
3596 ;; There is no file visiting the buffer, or the buffer has no
3597 ;; recorded last modification time, or there is no established
3600 (eq (visited-file-modtime) 0)
3601 (not (file-remote-p f nil
'connected
)))
3603 (let* ((remote-file-name-inhibit-cache t
)
3604 (attr (file-attributes f
))
3605 (modtime (tramp-compat-file-attribute-modification-time attr
))
3606 (mt (visited-file-modtime)))
3609 ;; File exists, and has a known modtime.
3610 ((and attr
(not (equal modtime
'(0 0))))
3611 (< (abs (tramp-time-diff
3613 ;; For compatibility, deal with both the old
3614 ;; (HIGH . LOW) and the new (HIGH LOW) return
3615 ;; values of `visited-file-modtime'.
3617 (list (car mt
) (cdr mt
))
3620 ;; Modtime has the don't know value.
3622 ;; If file does not exist, say it is not modified if and
3623 ;; only if that agrees with the buffer's record.
3624 (t (equal mt
'(-1 65535)))))))))
3626 ;; This is used in tramp-gvfs.el and tramp-sh.el.
3627 (defconst tramp-gio-events
3628 '("attribute-changed" "changed" "changes-done-hint"
3629 "created" "deleted" "moved" "pre-unmount" "unmounted")
3630 "List of events \"gio monitor\" could send.")
3632 ;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
3634 (defun tramp-handle-file-notify-add-watch (filename _flags _callback
)
3635 "Like `file-notify-add-watch' for Tramp files."
3636 (setq filename
(expand-file-name filename
))
3637 (with-parsed-tramp-file-name filename nil
3639 v
'file-notify-error
"File notification not supported for `%s'" filename
)))
3641 (defun tramp-handle-file-notify-rm-watch (proc)
3642 "Like `file-notify-rm-watch' for Tramp files."
3643 ;; The descriptor must be a process object.
3644 (unless (processp proc
)
3645 (tramp-error proc
'file-notify-error
"Not a valid descriptor %S" proc
))
3646 (tramp-message proc
6 "Kill %S" proc
)
3647 (delete-process proc
))
3649 (defun tramp-handle-file-notify-valid-p (proc)
3650 "Like `file-notify-valid-p' for Tramp files."
3651 (and (process-live-p proc
)
3652 ;; Sometimes, the process is still in status `run' when the
3653 ;; file or directory to be watched is deleted already.
3654 (with-current-buffer (process-buffer proc
)
3656 (concat (file-remote-p default-directory
)
3657 (process-get proc
'watch-name
))))))
3659 ;;; Functions for establishing connection:
3661 ;; The following functions are actions to be taken when seeing certain
3662 ;; prompts from the remote host. See the variable
3663 ;; `tramp-actions-before-shell' for usage of these functions.
3665 (defun tramp-action-login (_proc vec
)
3666 "Send the login name."
3667 (let ((user (or (tramp-file-name-user vec
)
3668 (with-tramp-connection-property vec
"login-as"
3669 (save-window-excursion
3670 (let ((enable-recursive-minibuffers t
))
3671 (pop-to-buffer (tramp-get-connection-buffer vec
))
3672 (read-string (match-string 0))))))))
3673 (with-current-buffer (tramp-get-connection-buffer vec
)
3674 (tramp-message vec
6 "\n%s" (buffer-string)))
3675 (tramp-message vec
3 "Sending login name `%s'" user
)
3676 (tramp-send-string vec
(concat user tramp-local-end-of-line
))))
3678 (defun tramp-action-password (proc vec
)
3679 "Query the user for a password."
3680 (with-current-buffer (process-buffer proc
)
3681 (let ((enable-recursive-minibuffers t
)
3682 (case-fold-search t
))
3683 ;; Let's check whether a wrong password has been sent already.
3684 ;; Sometimes, the process returns a new password request
3685 ;; immediately after rejecting the previous (wrong) one.
3686 (unless (tramp-get-connection-property vec
"first-password-request" nil
)
3687 (tramp-clear-passwd vec
))
3688 (goto-char (point-min))
3689 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
3690 (tramp-message vec
3 "Sending %s" (match-string 1))
3691 ;; We don't call `tramp-send-string' in order to hide the
3692 ;; password from the debug buffer.
3693 (process-send-string
3694 proc
(concat (tramp-read-passwd proc
) tramp-local-end-of-line
))
3695 ;; Hide password prompt.
3696 (narrow-to-region (point-max) (point-max)))))
3698 (defun tramp-action-succeed (_proc _vec
)
3699 "Signal success in finding shell prompt."
3700 (throw 'tramp-action
'ok
))
3702 (defun tramp-action-permission-denied (proc _vec
)
3703 "Signal permission denied."
3705 (throw 'tramp-action
'permission-denied
))
3707 (defun tramp-action-yesno (proc vec
)
3708 "Ask the user for confirmation using `yes-or-no-p'.
3709 Send \"yes\" to remote process on confirmation, abort otherwise.
3710 See also `tramp-action-yn'."
3711 (save-window-excursion
3712 (let ((enable-recursive-minibuffers t
))
3713 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
3714 (unless (yes-or-no-p (match-string 0))
3716 (throw 'tramp-action
'permission-denied
))
3717 (with-current-buffer (tramp-get-connection-buffer vec
)
3718 (tramp-message vec
6 "\n%s" (buffer-string)))
3719 (tramp-send-string vec
(concat "yes" tramp-local-end-of-line
)))))
3721 (defun tramp-action-yn (proc vec
)
3722 "Ask the user for confirmation using `y-or-n-p'.
3723 Send \"y\" to remote process on confirmation, abort otherwise.
3724 See also `tramp-action-yesno'."
3725 (save-window-excursion
3726 (let ((enable-recursive-minibuffers t
))
3727 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
3728 (unless (y-or-n-p (match-string 0))
3730 (throw 'tramp-action
'permission-denied
))
3731 (with-current-buffer (tramp-get-connection-buffer vec
)
3732 (tramp-message vec
6 "\n%s" (buffer-string)))
3733 (tramp-send-string vec
(concat "y" tramp-local-end-of-line
)))))
3735 (defun tramp-action-terminal (_proc vec
)
3736 "Tell the remote host which terminal type to use.
3737 The terminal type can be configured with `tramp-terminal-type'."
3738 (tramp-message vec
5 "Setting `%s' as terminal type." tramp-terminal-type
)
3739 (with-current-buffer (tramp-get-connection-buffer vec
)
3740 (tramp-message vec
6 "\n%s" (buffer-string)))
3741 (tramp-send-string vec
(concat tramp-terminal-type tramp-local-end-of-line
)))
3743 (defun tramp-action-process-alive (proc _vec
)
3744 "Check, whether a process has finished."
3745 (unless (process-live-p proc
)
3746 (throw 'tramp-action
'process-died
)))
3748 (defun tramp-action-out-of-band (proc vec
)
3749 "Check, whether an out-of-band copy has finished."
3750 ;; There might be pending output for the exit status.
3751 (tramp-accept-process-output proc
0.1)
3752 (cond ((and (not (process-live-p proc
))
3753 (zerop (process-exit-status proc
)))
3754 (tramp-message vec
3 "Process has finished.")
3755 (throw 'tramp-action
'ok
))
3756 ((or (and (memq (process-status proc
) '(stop exit
))
3757 (not (zerop (process-exit-status proc
))))
3758 (memq (process-status proc
) '(signal)))
3759 ;; `scp' could have copied correctly, but set modes could have failed.
3760 ;; This can be ignored.
3761 (with-current-buffer (process-buffer proc
)
3762 (goto-char (point-min))
3763 (if (re-search-forward tramp-operation-not-permitted-regexp nil t
)
3765 (tramp-message vec
5 "'set mode' error ignored.")
3766 (tramp-message vec
3 "Process has finished.")
3767 (throw 'tramp-action
'ok
))
3768 (tramp-message vec
3 "Process has died.")
3769 (throw 'tramp-action
'out-of-band-failed
))))
3772 ;;; Functions for processing the actions:
3774 (defun tramp-process-one-action (proc vec actions
)
3775 "Wait for output from the shell and perform one action."
3776 (let ((case-fold-search t
)
3777 found todo item pattern action
)
3779 ;; Reread output once all actions have been performed.
3780 ;; Obviously, the output was not complete.
3781 (tramp-accept-process-output proc
1)
3784 (setq item
(pop todo
))
3785 (setq pattern
(format "\\(%s\\)\\'" (symbol-value (nth 0 item
))))
3786 (setq action
(nth 1 item
))
3788 vec
5 "Looking for regexp \"%s\" from remote shell" pattern
)
3789 (when (tramp-check-for-regexp proc pattern
)
3790 (tramp-message vec
5 "Call `%s'" (symbol-name action
))
3791 (setq found
(funcall action proc vec
)))))
3794 (defun tramp-process-actions (proc vec pos actions
&optional timeout
)
3795 "Perform ACTIONS until success or TIMEOUT.
3796 PROC and VEC indicate the remote connection to be used. POS, if
3797 set, is the starting point of the region to be deleted in the
3799 ;; Enable `auth-source', unless "emacs -Q" has been called. We must
3800 ;; use the "password-vector" property in case we have several hops.
3801 (tramp-set-connection-property
3802 (tramp-get-connection-property
3803 proc
"password-vector" (process-get proc
'vector
))
3804 "first-password-request" tramp-cache-read-persistent-data
)
3806 (with-tramp-progress-reporter
3807 proc
3 "Waiting for prompts from remote shell"
3810 (with-timeout (timeout (setq exit
'timeout
))
3813 (catch 'tramp-action
3814 (tramp-process-one-action proc vec actions
)))))
3817 (catch 'tramp-action
3818 (tramp-process-one-action proc vec actions
)))))
3819 (with-current-buffer (tramp-get-connection-buffer vec
)
3821 (tramp-message vec
6 "\n%s" (buffer-string)))
3822 (unless (eq exit
'ok
)
3823 (tramp-clear-passwd vec
)
3824 (delete-process proc
)
3825 (tramp-error-with-buffer
3826 (tramp-get-connection-buffer vec
) vec
'file-error
3828 ((eq exit
'permission-denied
) "Permission denied")
3829 ((eq exit
'out-of-band-failed
)
3831 "Copy failed, see buffer `%s' for details"
3832 (tramp-get-connection-buffer vec
)))
3833 ((eq exit
'process-died
)
3834 (substitute-command-keys
3836 "Tramp failed to connect. If this happens repeatedly, try\n"
3837 " `\\[tramp-cleanup-this-connection]'")))
3840 "Timeout reached, see buffer `%s' for details"
3841 (tramp-get-connection-buffer vec
)))
3842 (t "Login failed")))))
3844 (with-current-buffer (tramp-get-connection-buffer vec
)
3845 (let (buffer-read-only) (delete-region pos
(point))))))))
3847 ;;; Utility functions:
3849 (defun tramp-accept-process-output (proc timeout
)
3850 "Like `accept-process-output' for Tramp processes.
3851 This is needed in order to hide `last-coding-system-used', which is set
3852 for process communication also."
3853 (with-current-buffer (process-buffer proc
)
3854 (let (buffer-read-only last-coding-system-used
3855 ;; We do not want to run timers.
3856 timer-list timer-idle-list
)
3857 ;; Under Windows XP, `accept-process-output' doesn't return
3858 ;; sometimes. So we add an additional timeout. JUST-THIS-ONE
3859 ;; is set due to Bug#12145. It is an integer, in order to avoid
3860 ;; running timers as well.
3862 proc
10 "%s %s %s\n%s"
3863 proc
(process-status proc
)
3864 (with-timeout (timeout)
3865 (accept-process-output proc timeout nil
0))
3868 (defun tramp-check-for-regexp (proc regexp
)
3869 "Check, whether REGEXP is contained in process buffer of PROC.
3870 Erase echoed commands if exists."
3871 (with-current-buffer (process-buffer proc
)
3872 (goto-char (point-min))
3874 ;; Check whether we need to remove echo output.
3875 (when (and (tramp-get-connection-property proc
"check-remote-echo" nil
)
3876 (re-search-forward tramp-echoed-echo-mark-regexp nil t
))
3877 (let ((begin (match-beginning 0)))
3878 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t
)
3879 ;; Discard echo from remote output.
3880 (tramp-set-connection-property proc
"check-remote-echo" nil
)
3881 (tramp-message proc
5 "echo-mark found")
3883 (delete-region begin
(point))
3884 (goto-char (point-min)))))
3886 (when (or (not (tramp-get-connection-property proc
"check-remote-echo" nil
))
3887 ;; Sometimes, the echo string is suppressed on the remote side.
3889 (substring-no-properties
3890 tramp-echo-mark-marker
3891 0 (min tramp-echo-mark-marker-length
(1- (point-max))))
3892 (buffer-substring-no-properties
3894 (min (+ (point-min) tramp-echo-mark-marker-length
)
3896 ;; No echo to be handled, now we can look for the regexp.
3897 ;; Sometimes, lines are much too long, and we run into a "Stack
3898 ;; overflow in regexp matcher". For example, //DIRED// lines of
3899 ;; directory listings with some thousand files. Therefore, we
3900 ;; look from the end.
3901 (goto-char (point-max))
3902 (ignore-errors (re-search-backward regexp nil t
)))))
3904 (defun tramp-wait-for-regexp (proc timeout regexp
)
3905 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
3906 Expects the output of PROC to be sent to the current buffer. Returns
3907 the string that matched, or nil. Waits indefinitely if TIMEOUT is
3909 (with-current-buffer (process-buffer proc
)
3910 (let ((found (tramp-check-for-regexp proc regexp
)))
3912 (with-timeout (timeout)
3914 (tramp-accept-process-output proc
1)
3915 (unless (process-live-p proc
)
3916 (tramp-error-with-buffer
3917 nil proc
'file-error
"Process has died"))
3918 (setq found
(tramp-check-for-regexp proc regexp
)))))
3921 (tramp-accept-process-output proc
1)
3922 (unless (process-live-p proc
)
3923 (tramp-error-with-buffer
3924 nil proc
'file-error
"Process has died"))
3925 (setq found
(tramp-check-for-regexp proc regexp
)))))
3926 (tramp-message proc
6 "\n%s" (buffer-string))
3930 proc
'file-error
"[[Regexp `%s' not found in %d secs]]"
3932 (tramp-error proc
'file-error
"[[Regexp `%s' not found]]" regexp
)))
3935 ;; It seems that Tru64 Unix does not like it if long strings are sent
3936 ;; to it in one go. (This happens when sending the Perl
3937 ;; `file-attributes' implementation, for instance.) Therefore, we
3938 ;; have this function which sends the string in chunks.
3939 (defun tramp-send-string (vec string
)
3940 "Send the STRING via connection VEC.
3942 The STRING is expected to use Unix line-endings, but the lines sent to
3943 the remote host use line-endings as defined in the variable
3944 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
3945 (let* ((p (tramp-get-connection-process vec
))
3946 (chunksize (tramp-get-connection-property p
"chunksize" nil
)))
3949 vec
'file-error
"Can't send string to remote host -- not logged in"))
3950 (tramp-set-connection-property p
"last-cmd-time" (current-time))
3951 (tramp-message vec
10 "%s" string
)
3952 (with-current-buffer (tramp-get-connection-buffer vec
)
3953 ;; Clean up the buffer. We cannot call `erase-buffer' because
3954 ;; narrowing might be in effect.
3955 (let (buffer-read-only) (delete-region (point-min) (point-max)))
3956 ;; Replace "\n" by `tramp-rsh-end-of-line'.
3959 'identity
(split-string string
"\n") tramp-rsh-end-of-line
))
3960 (unless (or (string= string
"")
3961 (string-equal (substring string -
1) tramp-rsh-end-of-line
))
3962 (setq string
(concat string tramp-rsh-end-of-line
)))
3964 (if (and chunksize
(not (zerop chunksize
)))
3966 (end (length string
)))
3969 vec
10 "Sending chunk from %s to %s"
3970 pos
(min (+ pos chunksize
) end
))
3971 (process-send-string
3972 p
(substring string pos
(min (+ pos chunksize
) end
)))
3973 (setq pos
(+ pos chunksize
))))
3974 (process-send-string p string
)))))
3976 (defun tramp-get-inode (vec)
3977 "Returns the virtual inode number.
3978 If it doesn't exist, generate a new one."
3979 (with-tramp-file-property vec
(tramp-file-name-localname vec
) "inode"
3980 (setq tramp-inodes
(1+ tramp-inodes
))))
3982 (defun tramp-get-device (vec)
3983 "Returns the virtual device number.
3984 If it doesn't exist, generate a new one."
3985 (with-tramp-connection-property (tramp-get-connection-process vec
) "device"
3986 (cons -
1 (setq tramp-devices
(1+ tramp-devices
)))))
3988 (defun tramp-equal-remote (file1 file2
)
3989 "Check, whether the remote parts of FILE1 and FILE2 are identical.
3990 The check depends on method, user and host name of the files. If
3991 one of the components is missing, the default values are used.
3992 The local file name parts of FILE1 and FILE2 are not taken into
3997 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
3999 would yield t. On the other hand, the following check results in nil:
4001 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
4002 (and (tramp-tramp-file-p file1
)
4003 (tramp-tramp-file-p file2
)
4004 (string-equal (file-remote-p file1
) (file-remote-p file2
))))
4006 ;;;###tramp-autoload
4007 (defun tramp-mode-string-to-int (mode-string)
4008 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
4009 (let* (case-fold-search
4010 (mode-chars (string-to-vector mode-string
))
4011 (owner-read (aref mode-chars
1))
4012 (owner-write (aref mode-chars
2))
4013 (owner-execute-or-setid (aref mode-chars
3))
4014 (group-read (aref mode-chars
4))
4015 (group-write (aref mode-chars
5))
4016 (group-execute-or-setid (aref mode-chars
6))
4017 (other-read (aref mode-chars
7))
4018 (other-write (aref mode-chars
8))
4019 (other-execute-or-sticky (aref mode-chars
9)))
4023 ((char-equal owner-read ?r
) (string-to-number "00400" 8))
4024 ((char-equal owner-read ?-
) 0)
4025 (t (error "Second char `%c' must be one of `r-'" owner-read
)))
4027 ((char-equal owner-write ?w
) (string-to-number "00200" 8))
4028 ((char-equal owner-write ?-
) 0)
4029 (t (error "Third char `%c' must be one of `w-'" owner-write
)))
4031 ((char-equal owner-execute-or-setid ?x
) (string-to-number "00100" 8))
4032 ((char-equal owner-execute-or-setid ?S
) (string-to-number "04000" 8))
4033 ((char-equal owner-execute-or-setid ?s
) (string-to-number "04100" 8))
4034 ((char-equal owner-execute-or-setid ?-
) 0)
4035 (t (error "Fourth char `%c' must be one of `xsS-'"
4036 owner-execute-or-setid
)))
4038 ((char-equal group-read ?r
) (string-to-number "00040" 8))
4039 ((char-equal group-read ?-
) 0)
4040 (t (error "Fifth char `%c' must be one of `r-'" group-read
)))
4042 ((char-equal group-write ?w
) (string-to-number "00020" 8))
4043 ((char-equal group-write ?-
) 0)
4044 (t (error "Sixth char `%c' must be one of `w-'" group-write
)))
4046 ((char-equal group-execute-or-setid ?x
) (string-to-number "00010" 8))
4047 ((char-equal group-execute-or-setid ?S
) (string-to-number "02000" 8))
4048 ((char-equal group-execute-or-setid ?s
) (string-to-number "02010" 8))
4049 ((char-equal group-execute-or-setid ?-
) 0)
4050 (t (error "Seventh char `%c' must be one of `xsS-'"
4051 group-execute-or-setid
)))
4053 ((char-equal other-read ?r
) (string-to-number "00004" 8))
4054 ((char-equal other-read ?-
) 0)
4055 (t (error "Eighth char `%c' must be one of `r-'" other-read
)))
4057 ((char-equal other-write ?w
) (string-to-number "00002" 8))
4058 ((char-equal other-write ?-
) 0)
4059 (t (error "Ninth char `%c' must be one of `w-'" other-write
)))
4061 ((char-equal other-execute-or-sticky ?x
) (string-to-number "00001" 8))
4062 ((char-equal other-execute-or-sticky ?T
) (string-to-number "01000" 8))
4063 ((char-equal other-execute-or-sticky ?t
) (string-to-number "01001" 8))
4064 ((char-equal other-execute-or-sticky ?-
) 0)
4065 (t (error "Tenth char `%c' must be one of `xtT-'"
4066 other-execute-or-sticky
)))))))
4068 (defconst tramp-file-mode-type-map
4069 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
4071 (2 .
"c") ; character device
4072 (3 .
"m") ; multiplexed character device (v7)
4073 (4 .
"d") ; directory
4074 (5 .
"?") ; Named special file (XENIX)
4075 (6 .
"b") ; block device
4076 (7 .
"?") ; multiplexed block device (v7)
4077 (8 .
"-") ; regular file
4078 (9 .
"n") ; network special file (HP-UX)
4079 (10 .
"l") ; symlink
4080 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
4082 (13 .
"D") ; door special (Solaris)
4083 (14 .
"w")) ; whiteout (BSD)
4084 "A list of file types returned from the `stat' system call.
4085 This is used to map a mode number to a permission string.")
4087 ;;;###tramp-autoload
4088 (defun tramp-file-mode-from-int (mode)
4089 "Turn an integer representing a file mode into an ls(1)-like string."
4091 (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
4092 (user (logand (lsh mode -
6) 7))
4093 (group (logand (lsh mode -
3) 7))
4094 (other (logand (lsh mode -
0) 7))
4095 (suid (> (logand (lsh mode -
9) 4) 0))
4096 (sgid (> (logand (lsh mode -
9) 2) 0))
4097 (sticky (> (logand (lsh mode -
9) 1) 0)))
4098 (setq user
(tramp-file-mode-permissions user suid
"s"))
4099 (setq group
(tramp-file-mode-permissions group sgid
"s"))
4100 (setq other
(tramp-file-mode-permissions other sticky
"t"))
4101 (concat type user group other
)))
4103 (defun tramp-file-mode-permissions (perm suid suid-text
)
4104 "Convert a permission bitset into a string.
4105 This is used internally by `tramp-file-mode-from-int'."
4106 (let ((r (> (logand perm
4) 0))
4107 (w (> (logand perm
2) 0))
4108 (x (> (logand perm
1) 0)))
4109 (concat (or (and r
"r") "-")
4110 (or (and w
"w") "-")
4111 (or (and suid x suid-text
) ; suid, execute
4112 (and suid
(upcase suid-text
)) ; suid, !execute
4113 (and x
"x") "-")))) ; !suid
4115 ;;;###tramp-autoload
4116 (defun tramp-get-local-uid (id-format)
4117 "The uid of the local user, in ID-FORMAT.
4118 ID-FORMAT valid values are `string' and `integer'."
4119 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
4121 ;;;###tramp-autoload
4122 (defun tramp-get-local-gid (id-format)
4123 "The gid of the local user, in ID-FORMAT.
4124 ID-FORMAT valid values are `string' and `integer'."
4125 ;; `group-gid' has been introduced with Emacs 24.4.
4126 (if (and (fboundp 'group-gid
) (equal id-format
'integer
))
4127 (tramp-compat-funcall 'group-gid
)
4128 (tramp-compat-file-attribute-group-id (file-attributes "~/" id-format
))))
4130 (defun tramp-get-local-locale (&optional vec
)
4131 "Determine locale, supporting UTF8 if possible.
4132 VEC is used for tracing."
4133 ;; We use key nil for local connection properties.
4134 (with-tramp-connection-property nil
"locale"
4135 (let ((candidates '("en_US.utf8" "C.utf8" "en_US.UTF-8"))
4138 (unless (or (memq system-type
'(windows-nt))
4139 (not (zerop (tramp-call-process
4140 nil
"locale" nil t nil
"-a"))))
4142 (goto-char (point-min))
4143 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates
)))
4145 (setq locale
(car candidates
)
4147 (setq candidates
(cdr candidates
))))))
4149 (when vec
(tramp-message vec
7 "locale %s" (or locale
"C")))
4152 ;;;###tramp-autoload
4153 (defun tramp-check-cached-permissions (vec access
)
4154 "Check `file-attributes' caches for VEC.
4155 Return t if according to the cache access type ACCESS is known to
4161 ((eq ?x access
) 3))))
4162 (dolist (suffix '("string" "integer") result
)
4169 (tramp-get-file-property
4170 vec
(tramp-file-name-localname vec
)
4171 (concat "file-attributes-" suffix
) nil
)
4173 (tramp-make-tramp-file-name vec
) (intern suffix
))))
4175 (tramp-get-connection-property
4176 vec
(concat "uid-" suffix
) nil
))
4178 (tramp-get-connection-property
4179 vec
(concat "gid-" suffix
) nil
))
4181 (if (string-equal suffix
"string")
4182 tramp-unknown-id-string tramp-unknown-id-integer
)))
4187 (eq t
(tramp-compat-file-attribute-type file-attr
))
4188 (null (tramp-compat-file-attribute-type file-attr
)))
4190 ;; World accessible.
4192 (aref (tramp-compat-file-attribute-modes file-attr
)
4194 ;; User accessible and owned by user.
4197 (aref (tramp-compat-file-attribute-modes file-attr
) offset
))
4198 (or (equal remote-uid
4199 (tramp-compat-file-attribute-user-id file-attr
))
4201 (tramp-compat-file-attribute-user-id file-attr
))))
4202 ;; Group accessible and owned by user's principal group.
4205 (aref (tramp-compat-file-attribute-modes file-attr
)
4207 (or (equal remote-gid
4208 (tramp-compat-file-attribute-group-id file-attr
))
4210 (tramp-compat-file-attribute-group-id
4211 file-attr
))))))))))))
4213 ;;;###tramp-autoload
4214 (defun tramp-local-host-p (vec)
4215 "Return t if this points to the local host, nil otherwise."
4216 (let ((host (tramp-file-name-host vec
))
4217 (port (tramp-file-name-port vec
)))
4220 (string-match tramp-local-host-regexp host
)
4221 ;; A port is an indication for an ssh tunnel or alike.
4223 ;; The method shall be applied to one of the shell file name
4224 ;; handlers. `tramp-local-host-p' is also called for "smb" and
4225 ;; alike, where it must fail.
4226 (tramp-get-method-parameter vec
'tramp-login-program
)
4227 ;; The local temp directory must be writable for the other user.
4229 (tramp-make-tramp-file-name
4230 vec
(tramp-compat-temporary-file-directory) 'nohop
))
4231 ;; On some systems, chown runs only for root.
4232 (or (zerop (user-uid))
4233 ;; This is defined in tramp-sh.el. Let's assume this is
4235 (zerop (tramp-compat-funcall 'tramp-get-remote-uid vec
'integer
))))))
4237 (defun tramp-get-remote-tmpdir (vec)
4238 "Return directory for temporary files on the remote host identified by VEC."
4239 (with-tramp-connection-property vec
"tmpdir"
4241 (tramp-make-tramp-file-name
4242 vec
(or (tramp-get-method-parameter vec
'tramp-tmpdir
) "/tmp"))))
4243 (or (and (file-directory-p dir
) (file-writable-p dir
)
4244 (file-remote-p dir
'localname
))
4245 (tramp-error vec
'file-error
"Directory %s not accessible" dir
))
4248 ;;;###tramp-autoload
4249 (defun tramp-make-tramp-temp-file (vec)
4250 "Create a temporary file on the remote host identified by VEC.
4251 Return the local name of the temporary file."
4252 (let ((prefix (expand-file-name
4253 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))
4256 ;; `make-temp-file' would be the natural choice for
4257 ;; implementation. But it calls `write-region' internally,
4258 ;; which also needs a temporary file - we would end in an
4260 (setq result
(make-temp-name prefix
))
4261 (if (file-exists-p result
)
4263 ;; This creates the file by side effect.
4264 (set-file-times result
)
4265 (set-file-modes result
(string-to-number "0700" 8))))
4267 ;; Return the local part.
4268 (with-parsed-tramp-file-name result nil localname
)))
4270 (defun tramp-delete-temp-file-function ()
4271 "Remove temporary files related to current buffer."
4272 (when (stringp tramp-temp-buffer-file-name
)
4273 (ignore-errors (delete-file tramp-temp-buffer-file-name
))))
4275 (add-hook 'kill-buffer-hook
'tramp-delete-temp-file-function
)
4276 (add-hook 'tramp-unload-hook
4278 (remove-hook 'kill-buffer-hook
4279 'tramp-delete-temp-file-function
)))
4281 (defun tramp-handle-make-auto-save-file-name ()
4282 "Like `make-auto-save-file-name' for Tramp files.
4283 Returns a file name in `tramp-auto-save-directory' for autosaving
4284 this file, if that variable is non-nil."
4285 (when (stringp tramp-auto-save-directory
)
4286 (setq tramp-auto-save-directory
4287 (expand-file-name tramp-auto-save-directory
)))
4288 ;; Create directory.
4289 (unless (or (null tramp-auto-save-directory
)
4290 (file-exists-p tramp-auto-save-directory
))
4291 (make-directory tramp-auto-save-directory t
))
4294 (if (and (stringp tramp-auto-save-directory
)
4295 (file-remote-p tramp-auto-save-directory
))
4298 (auto-save-file-name-transforms
4299 (if (null tramp-auto-save-directory
)
4300 auto-save-file-name-transforms
))
4302 (if (null tramp-auto-save-directory
)
4305 (tramp-subst-strs-in-string
4312 (tramp-compat-file-name-unquote (buffer-file-name)))
4313 tramp-auto-save-directory
))))
4314 ;; Run plain `make-auto-save-file-name'.
4315 (tramp-run-real-handler 'make-auto-save-file-name nil
)))
4317 (defun tramp-subst-strs-in-string (alist string
)
4318 "Replace all occurrences of the string FROM with TO in STRING.
4319 ALIST is of the form ((FROM . TO) ...)."
4322 (let* ((pr (car alist
))
4325 (while (string-match (regexp-quote from
) string
)
4326 (setq string
(replace-match to t t string
)))
4327 (setq alist
(cdr alist
))))
4330 (defun tramp-handle-temporary-file-directory ()
4331 "Like `temporary-file-directory' for Tramp files."
4333 (dolist (dir `(,(ignore-errors
4334 (tramp-get-remote-tmpdir
4335 (tramp-dissect-file-name default-directory
)))
4336 ,default-directory
))
4337 (when (and (stringp dir
) (file-directory-p dir
) (file-writable-p dir
))
4338 (throw 'result
(expand-file-name dir
))))))
4340 (defun tramp-handle-make-nearby-temp-file (prefix &optional dir-flag suffix
)
4341 "Like `make-nearby-temp-file' for Tramp files."
4342 (let ((temporary-file-directory
4343 (tramp-compat-temporary-file-directory-function)))
4344 (make-temp-file prefix dir-flag suffix
)))
4346 ;;; Compatibility functions section:
4348 (defun tramp-call-process
4349 (vec program
&optional infile destination display
&rest args
)
4350 "Calls `call-process' on the local host.
4351 It always returns a return code. The Lisp error raised when
4352 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4353 are written with verbosity of 6."
4354 (let ((default-directory (tramp-compat-temporary-file-directory))
4355 (destination (if (eq destination t
) (current-buffer) destination
))
4356 output error result
)
4358 vec
6 "`%s %s' %s %s"
4359 program
(mapconcat 'identity args
" ") infile destination
)
4364 'call-process program infile
(or destination t
) display args
))
4365 ;; `result' could also be an error string.
4366 (when (stringp result
)
4369 (with-current-buffer
4370 (if (bufferp destination
) destination
(current-buffer))
4371 (setq output
(buffer-string))))
4373 (setq error
(error-message-string err
)
4375 (if (zerop (length error
))
4376 (tramp-message vec
6 "%d\n%s" result output
)
4377 (tramp-message vec
6 "%d\n%s\n%s" result output error
))
4380 (defun tramp-call-process-region
4381 (vec start end program
&optional delete buffer display
&rest args
)
4382 "Calls `call-process-region' on the local host.
4383 It always returns a return code. The Lisp error raised when
4384 PROGRAM is nil is trapped also, returning 1. Furthermore, traces
4385 are written with verbosity of 6."
4386 (let ((default-directory (tramp-compat-temporary-file-directory))
4387 (buffer (if (eq buffer t
) (current-buffer) buffer
))
4390 vec
6 "`%s %s' %s %s %s %s"
4391 program
(mapconcat 'identity args
" ") start end delete buffer
)
4396 'call-process-region
4397 start end program delete buffer display args
))
4398 ;; `result' could also be an error string.
4399 (when (stringp result
)
4400 (signal 'file-error
(list result
)))
4401 (with-current-buffer (if (bufferp buffer
) buffer
(current-buffer))
4403 (tramp-message vec
6 "%d" result
)
4404 (tramp-message vec
6 "%d\n%s" result
(buffer-string)))))
4407 (tramp-message vec
6 "%d\n%s" result
(error-message-string err
))))
4410 ;;;###tramp-autoload
4411 (defun tramp-read-passwd (proc &optional prompt
)
4412 "Read a password from user (compat function).
4413 Consults the auth-source package.
4414 Invokes `password-read' if available, `read-passwd' else."
4415 (let* ((case-fold-search t
)
4416 (key (tramp-make-tramp-file-name
4417 ;; In tramp-sh.el, we must use "password-vector" due to
4419 (tramp-get-connection-property
4420 proc
"password-vector" (process-get proc
'vector
))
4424 (with-current-buffer (process-buffer proc
)
4425 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
4426 (format "%s for %s " (capitalize (match-string 1)) key
))))
4427 ;; We suspend the timers while reading the password.
4428 (stimers (with-timeout-suspend))
4429 auth-info auth-passwd
)
4432 (with-parsed-tramp-file-name key nil
4434 (or user
(tramp-get-connection-property key
"login-as" nil
)))
4437 ;; See if auth-sources contains something useful.
4439 (and (tramp-get-connection-property
4440 v
"first-password-request" nil
)
4441 ;; Try with Tramp's current method.
4447 (concat user tramp-prefix-domain-format domain
)
4451 (concat host tramp-prefix-port-format port
)
4454 :require
(cons :secret
(and user
'(:user
))))
4455 auth-passwd
(plist-get
4456 (nth 0 auth-info
) :secret
)
4457 auth-passwd
(if (functionp auth-passwd
)
4458 (funcall auth-passwd
)
4460 ;; Try the password cache.
4461 (let ((password (password-read pw-prompt key
)))
4462 ;; FIXME test password works before caching it.
4463 (password-cache-add key password
)
4465 ;; Else, get the password interactively.
4466 (read-passwd pw-prompt
))
4467 (tramp-set-connection-property v
"first-password-request" nil
)))
4468 ;; Reenable the timers.
4469 (with-timeout-unsuspend stimers
))))
4471 ;;;###tramp-autoload
4472 (defun tramp-clear-passwd (vec)
4473 "Clear password cache for connection related to VEC."
4474 (let ((method (tramp-file-name-method vec
))
4475 (user-domain (tramp-file-name-user-domain vec
))
4476 (host-port (tramp-file-name-host-port vec
))
4477 (hop (tramp-file-name-hop vec
)))
4479 ;; Clear also the passwords of the hops.
4481 (tramp-dissect-file-name
4484 (replace-regexp-in-string
4485 (concat tramp-postfix-hop-regexp
"$")
4486 tramp-postfix-host-format hop
)))))
4488 `(:max
1 ,(and user-domain
:user
) ,user-domain
4489 :host
,host-port
:port
,method
))
4490 (password-cache-remove (tramp-make-tramp-file-name vec
'noloc
'nohop
))))
4492 ;; Snarfed code from time-date.el.
4494 (defconst tramp-half-a-year
'(241 17024)
4495 "Evaluated by \"(days-to-time 183)\".")
4497 ;;;###tramp-autoload
4498 (defun tramp-time-diff (t1 t2
)
4499 "Return the difference between the two times, in seconds.
4500 T1 and T2 are time values (as returned by `current-time' for example)."
4501 ;; Starting with Emacs 25.1, we could change this to use `time-subtract'.
4502 (float-time (tramp-compat-funcall 'subtract-time t1 t2
)))
4504 (defun tramp-unquote-shell-quote-argument (s)
4505 "Remove quotation prefix \"/:\" from string S, and quote it then for shell."
4506 (shell-quote-argument (tramp-compat-file-name-unquote s
)))
4508 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
4509 ;; does not deal well with newline characters. Newline is replaced by
4510 ;; backslash newline. But if, say, the string `a backslash newline b'
4511 ;; is passed to a shell, the shell will expand this into "ab",
4512 ;; completely omitting the newline. This is not what was intended.
4513 ;; It does not appear to be possible to make the function
4514 ;; `shell-quote-argument' work with newlines without making it
4515 ;; dependent on the shell used. But within this package, we know that
4516 ;; we will always use a Bourne-like shell, so we use an approach which
4519 ;; The approach is simple: we call `shell-quote-argument', then
4520 ;; massage the newline part of the result.
4522 ;; This function should produce a string which is grokked by a Unix
4523 ;; shell, even if the Emacs is running on Windows. Since this is the
4524 ;; kludges section, we bind `system-type' in such a way that
4525 ;; `shell-quote-argument' behaves as if on Unix.
4527 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
4528 ;; function to work with Bourne-like shells.
4529 ;;;###tramp-autoload
4530 (defun tramp-shell-quote-argument (s)
4531 "Similar to `shell-quote-argument', but groks newlines.
4532 Only works for Bourne-like shells."
4533 (let ((system-type 'not-windows
))
4535 (let ((result (tramp-unquote-shell-quote-argument s
))
4536 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line
))))
4537 (when (and (>= (length result
) 2)
4538 (string= (substring result
0 2) "\\~"))
4539 (setq result
(substring result
1)))
4540 (while (string-match nl result
)
4541 (setq result
(replace-match (format "'%s'" tramp-rsh-end-of-line
)
4545 ;;; Signal handling. This works for remote processes, which have set
4546 ;;; the process property `remote-pid'.
4548 (defun tramp-interrupt-process (&optional process _current-group
)
4549 "Interrupt remote process PROC."
4550 ;; CURRENT-GROUP is not implemented yet.
4552 ((processp process
) process
)
4553 ((bufferp process
) (get-buffer-process process
))
4554 ((stringp process
) (or (get-process process
)
4555 (get-buffer-process process
)))
4556 ((null process
) (get-buffer-process (current-buffer)))
4559 ;; If it's a Tramp process, send the INT signal remotely.
4560 (when (and (processp proc
) (setq pid
(process-get proc
'remote-pid
)))
4561 (if (not (process-live-p proc
))
4562 (tramp-error proc
'error
"Process %s is not active" proc
)
4563 (tramp-message proc
5 "Interrupt process %s with pid %s" proc pid
)
4564 ;; This is for tramp-sh.el. Other backends do not support this (yet).
4565 (tramp-compat-funcall
4567 (process-get proc
'vector
)
4568 (format "kill -2 %d" pid
))
4569 ;; Wait, until the process has disappeared. If it doesn't,
4570 ;; fall back to the default implementation.
4571 (with-timeout (1 (ignore))
4572 (while (process-live-p proc
)
4573 ;; We cannot run `tramp-accept-process-output', it blocks timers.
4574 (accept-process-output proc
0.1))
4578 ;; `interrupt-process-functions' exists since Emacs 26.1.
4579 (when (boundp 'interrupt-process-functions
)
4580 (add-hook 'interrupt-process-functions
'tramp-interrupt-process
)
4584 (remove-hook 'interrupt-process-functions
'tramp-interrupt-process
))))
4586 ;;; Integration of eshell.el:
4588 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
4589 ;; when `default-directory' points to another host.
4590 (defun tramp-eshell-directory-change ()
4591 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
4592 (setq eshell-path-env
4593 (if (tramp-tramp-file-p default-directory
)
4594 (with-parsed-tramp-file-name default-directory nil
4598 ;; When `tramp-own-remote-path' is in `tramp-remote-path',
4599 ;; the remote path is only set in the session cache.
4600 (tramp-get-connection-property
4601 (tramp-get-connection-process v
) "remote-path" nil
)
4602 (tramp-get-connection-property v
"remote-path" nil
))
4606 (eval-after-load "esh-util"
4608 (add-hook 'eshell-mode-hook
4609 'tramp-eshell-directory-change
)
4610 (add-hook 'eshell-directory-change-hook
4611 'tramp-eshell-directory-change
)
4612 (add-hook 'tramp-unload-hook
4614 (remove-hook 'eshell-mode-hook
4615 'tramp-eshell-directory-change
)
4616 (remove-hook 'eshell-directory-change-hook
4617 'tramp-eshell-directory-change
)))))
4619 ;; Checklist for `tramp-unload-hook'
4620 ;; - Unload all `tramp-*' packages
4621 ;; - Reset `file-name-handler-alist'
4622 ;; - Cleanup hooks where Tramp functions are in
4623 ;; - Cleanup advised functions
4624 ;; - Cleanup autoloads
4626 (defun tramp-unload-tramp ()
4627 "Discard Tramp from loading remote files."
4629 ;; ange-ftp settings must be enabled.
4630 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp
)
4631 ;; Maybe it's not loaded yet.
4632 (ignore-errors (unload-feature 'tramp
'force
)))
4638 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
4640 ;; * Better error checking. At least whenever we see something
4641 ;; strange when doing zerop, we should kill the process and start
4642 ;; again. (Greg Stark)
4644 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
4646 ;; * I was wondering if it would be possible to use tramp even if I'm
4647 ;; actually using sshfs. But when I launch a command I would like
4648 ;; to get it executed on the remote machine where the files really
4649 ;; are. (Andrea Crotti)
4651 ;; * Run emerge on two remote files. Bug is described here:
4652 ;; <https://www.mail-archive.com/tramp-devel@nongnu.org/msg01041.html>.
4655 ;; * Refactor code from different handlers. Start with
4656 ;; *-process-file. One idea is to generalize `tramp-send-command'
4657 ;; and friends, for most of the handlers this is the major
4658 ;; difference between the different backends. Other handlers but
4659 ;; *-process-file would profit from this as well.
4661 ;;; tramp.el ends here