summaryrefslogtreecommitdiff
path: root/lisp/net/tramp.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/net/tramp.el')
-rw-r--r--lisp/net/tramp.el2167
1 files changed, 1223 insertions, 944 deletions
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 315e7099479..b3a8b93e2a8 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -7,6 +7,9 @@
;; Maintainer: Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, processes
;; Package: tramp
+;; Version: 2.4.2
+;; Package-Requires: ((emacs "24.4"))
+;; URL: https://savannah.gnu.org/projects/tramp
;; This file is part of GNU Emacs.
@@ -35,8 +38,6 @@
;; Notes:
;; -----
;;
-;; This package only works for Emacs 24.1 and higher.
-;;
;; Also see the todo list at the bottom of this file.
;;
;; The current version of Tramp can be retrieved from the following URL:
@@ -56,12 +57,13 @@
;;; Code:
(require 'tramp-compat)
+(require 'tramp-integration)
+(require 'trampver)
;; Pacify byte-compiler.
(require 'cl-lib)
+(declare-function netrc-parse "netrc")
(defvar auto-save-file-name-transforms)
-(defvar eshell-path-env)
-(defvar ls-lisp-use-insert-directory-program)
(defvar outline-regexp)
;;; User Customizable Internal Variables:
@@ -73,6 +75,16 @@
:link '(custom-manual "(tramp)Top")
:version "22.1")
+(eval-and-compile ;; So it's also available in tramp-loaddefs.el!
+ (defvar tramp--startup-hook nil
+ "Forms to be executed at the end of tramp.el.")
+
+ (defmacro tramp--with-startup (&rest body)
+ "Schedule BODY to be executed at the end of tramp.el."
+ `(add-hook 'tramp--startup-hook (lambda () ,@body))))
+
+(require 'tramp-loaddefs)
+
;; Maybe we need once a real Tramp mode, with key bindings etc.
;;;###autoload
(defcustom tramp-mode t
@@ -122,8 +134,10 @@ This setting has precedence over `auto-save-file-name-transforms'."
:type '(choice (const :tag "Use default" nil)
(directory :tag "Auto save directory name")))
+;; Suppress `shell-file-name' for w32 systems.
(defcustom tramp-encoding-shell
- (or (tramp-compat-funcall 'w32-shell-name) "/bin/sh")
+ (let (shell-file-name)
+ (or (tramp-compat-funcall 'w32-shell-name) "/bin/sh"))
"Use this program for encoding and decoding commands on the local host.
This shell is used to execute the encoding and decoding command on the
local host, so if you want to use `~' in those commands, you should
@@ -146,27 +160,31 @@ use for the remote host."
:group 'tramp
:type '(file :must-match t))
+;; Suppress `shell-file-name' for w32 systems.
(defcustom tramp-encoding-command-switch
- (if (tramp-compat-funcall 'w32-shell-dos-semantics) "/c" "-c")
+ (let (shell-file-name)
+ (if (tramp-compat-funcall 'w32-shell-dos-semantics) "/c" "-c"))
"Use this switch together with `tramp-encoding-shell' for local commands.
See the variable `tramp-encoding-shell' for more information."
:group 'tramp
:type 'string)
+;; Suppress `shell-file-name' for w32 systems.
(defcustom tramp-encoding-command-interactive
- (unless (tramp-compat-funcall 'w32-shell-dos-semantics) "-i")
+ (let (shell-file-name)
+ (unless (tramp-compat-funcall 'w32-shell-dos-semantics) "-i"))
"Use this switch together with `tramp-encoding-shell' for interactive shells.
See the variable `tramp-encoding-shell' for more information."
:version "24.1"
:group 'tramp
:type '(choice (const nil) string))
-;;;###tramp-autoload
(defvar tramp-methods nil
"Alist of methods for remote files.
This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
Each NAME stands for a remote access method. Each PARAM is a
pair of the form (KEY VALUE). The following KEYs are defined:
+
* `tramp-remote-shell'
This specifies the shell to use on the remote host. This
MUST be a Bourne-like shell. It is normally not necessary to
@@ -175,19 +193,23 @@ pair of the form (KEY VALUE). The following KEYs are defined:
for it. Also note that \"/bin/sh\" exists on all Unixen,
this might not be true for the value that you decide to use.
You Have Been Warned.
+
* `tramp-remote-shell-login'
This specifies the arguments to let `tramp-remote-shell' run
as a login shell. It defaults to (\"-l\"), but some shells,
like ksh, require another argument. See
`tramp-connection-properties' for a way to overwrite the
default value.
+
* `tramp-remote-shell-args'
For implementation of `shell-command', this specifies the
arguments to let `tramp-remote-shell' run a single command.
+
* `tramp-login-program'
This specifies the name of the program to use for logging in to the
remote host. This may be the name of rsh or a workalike program,
or the name of telnet or a workalike, or the name of su or a workalike.
+
* `tramp-login-args'
This specifies the list of arguments to pass to the above
mentioned program. Please note that this is a list of list of arguments,
@@ -203,55 +225,88 @@ pair of the form (KEY VALUE). The following KEYs are defined:
`tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
parameter of a program, if exists. \"%c\" adds additional
`tramp-ssh-controlmaster-options' options for the first hop.
+ The existence of `tramp-login-args', combined with the absence of
+ `tramp-copy-args', is an indication that the method is capable of
+ multi-hops.
+
* `tramp-login-env'
A list of environment variables and their values, which will
be set when calling `tramp-login-program'.
+
* `tramp-async-args'
When an asynchronous process is started, we know already that
the connection works. Therefore, we can pass additional
parameters to suppress diagnostic messages, in order not to
tamper the process output.
+
* `tramp-copy-program'
This specifies the name of the program to use for remotely copying
the file; this might be the absolute filename of scp or the name of
a workalike program. It is always applied on the local host.
+
* `tramp-copy-args'
This specifies the list of parameters to pass to the above mentioned
program, the hints for `tramp-login-args' also apply here.
+
* `tramp-copy-env'
A list of environment variables and their values, which will
be set when calling `tramp-copy-program'.
+
* `tramp-remote-copy-program'
The listener program to be applied on remote side, if needed.
+
* `tramp-remote-copy-args'
The list of parameters to pass to the listener program, the hints
for `tramp-login-args' also apply here. Additionally, \"%r\" could
be used here and in `tramp-copy-args'. It denotes a randomly
chosen port for the remote listener.
+
* `tramp-copy-keep-date'
This specifies whether the copying program when the preserves the
timestamp of the original file.
+
* `tramp-copy-keep-tmpfile'
This specifies whether a temporary local file shall be kept
for optimization reasons (useful for \"rsync\" methods).
+
* `tramp-copy-recursive'
Whether the operation copies directories recursively.
+
* `tramp-default-port'
The default port of a method.
+
* `tramp-tmpdir'
A directory on the remote host for temporary files. If not
specified, \"/tmp\" is taken as default.
+
* `tramp-connection-timeout'
This is the maximum time to be spent for establishing a connection.
In general, the global default value shall be used, but for
some methods, like \"su\" or \"sudo\", a shorter timeout
might be desirable.
+
+ * `tramp-session-timeout'
+ How long a Tramp connection keeps open before being disconnected.
+ This is useful for methods like \"su\" or \"sudo\", which
+ shouldn't run an open connection in the background forever.
+
* `tramp-case-insensitive'
Whether the remote file system handles file names case insensitive.
Only a non-nil value counts, the default value nil means to
perform further checks on the remote host. See
`tramp-connection-properties' for a way to overwrite this.
+ * `tramp-mount-args'
+ * `tramp-copyto-args'
+ * `tramp-moveto-args'
+ * `tramp-about-args'
+ These parameters, a list of list like `tramp-login-args', are used
+ for the \"rclone\" method, and are appended to the respective
+ \"rclone\" commands. In general, they shouldn't be changed inside
+ `tramp-methods'; it is recommended to change their values via
+ `tramp-connection-properties'. Unlike `tramp-login-args' there is
+ no pattern replacement.
+
What does all this mean? Well, you should specify `tramp-login-program'
for all methods; this program is used to log in to the remote site. Then,
there are two ways to actually transfer the files between the local and the
@@ -304,7 +359,6 @@ Also see `tramp-default-method-alist'."
:group 'tramp
:type 'string)
-;;;###tramp-autoload
(defcustom tramp-default-method-alist nil
"Default method to use for specific host/user pairs.
This is an alist of items (HOST USER METHOD). The first matching item
@@ -334,7 +388,6 @@ This variable is regarded as obsolete, and will be removed soon."
:group 'tramp
:type '(choice (const nil) string))
-;;;###tramp-autoload
(defcustom tramp-default-user-alist nil
"Default user to use for specific method/host pairs.
This is an alist of items (METHOD HOST USER). The first matching item
@@ -356,7 +409,6 @@ Useful for su and sudo methods mostly."
:group 'tramp
:type 'string)
-;;;###tramp-autoload
(defcustom tramp-default-host-alist nil
"Default host to use for specific method/user pairs.
This is an alist of items (METHOD USER HOST). The first matching item
@@ -378,11 +430,17 @@ empty string for the method name."
This is an alist of items (HOST USER PROXY). The first matching
item specifies the proxy to be passed for a file name located on
a remote target matching USER@HOST. HOST and USER are regular
-expressions. PROXY must be a Tramp filename without a localname
-part. Method and user name on PROXY are optional, which is
-interpreted with the default values. PROXY can contain the
-patterns %h and %u, which are replaced by the strings matching
-HOST or USER, respectively.
+expressions, which could also cover a domain (USER%DOMAIN) or
+port (HOST#PORT). PROXY must be a Tramp filename without a
+localname part. Method and user name on PROXY are optional,
+which is interpreted with the default values.
+
+PROXY can contain the patterns %h and %u, which are replaced by
+the strings matching HOST or USER (without DOMAIN and PORT parts),
+respectively.
+
+If an entry is added while parsing ad-hoc hop definitions, PROXY
+carries the non-nil text property `tramp-ad-hoc'.
HOST, USER or PROXY could also be Lisp forms, which will be
evaluated. The result must be a string or nil, which is
@@ -410,14 +468,18 @@ host runs a registered shell, it shall be added to this list, too."
:group 'tramp
:type '(repeat (regexp :tag "Host regexp")))
-;;;###tramp-autoload
-(defconst tramp-local-host-regexp
+(defcustom tramp-local-host-regexp
(concat
"\\`"
(regexp-opt
(list "localhost" "localhost6" (system-name) "127.0.0.1" "::1") t)
"\\'")
- "Host names which are regarded as local host.")
+ "Host names which are regarded as local host.
+If the local host runs a chrooted environment, set this to nil."
+ :version "27.1"
+ :group 'tramp
+ :type '(choice (const :tag "Chrooted environment" nil)
+ (regexp :tag "Host regexp")))
(defvar tramp-completion-function-alist nil
"Alist of methods for remote files.
@@ -510,10 +572,7 @@ This regexp must match both `tramp-initial-end-of-output' and
:type 'regexp)
(defcustom tramp-password-prompt-regexp
- (format "^.*\\(%s\\).*:\^@? *"
- ;; `password-word-equivalents' has been introduced with Emacs 24.4.
- (regexp-opt (or (bound-and-true-p password-word-equivalents)
- '("password" "passphrase"))))
+ (format "^.*\\(%s\\).*:\^@? *" (regexp-opt password-word-equivalents))
"Regexp matching password-like prompts.
The regexp should match at end of buffer.
@@ -549,7 +608,10 @@ The regexp should match at end of buffer."
(defcustom tramp-yesno-prompt-regexp
(concat
- (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
+ (regexp-opt
+ '("Are you sure you want to continue connecting (yes/no)?"
+ "Are you sure you want to continue connecting (yes/no/[fingerprint])?")
+ t)
"\\s-*")
"Regular expression matching all yes/no queries which need to be confirmed.
The confirmation should be done with yes or no.
@@ -632,7 +694,6 @@ Useful for \"rsync\" like methods.")
(make-variable-buffer-local 'tramp-temp-buffer-file-name)
(put 'tramp-temp-buffer-file-name 'permanent-local t)
-;;;###tramp-autoload
(defcustom tramp-syntax 'default
"Tramp filename syntax to be used.
@@ -651,8 +712,8 @@ Customize. See also `tramp-change-syntax'."
(const :tag "Ange-FTP" simplified)
(const :tag "XEmacs" separate))
:require 'tramp
- :initialize 'custom-initialize-set
- :set 'tramp-set-syntax)
+ :initialize #'custom-initialize-default
+ :set #'tramp-set-syntax)
(defun tramp-set-syntax (symbol value)
"Set SYMBOL to value VALUE.
@@ -660,7 +721,7 @@ Used in user option `tramp-syntax'. There are further variables
to be set, depending on VALUE."
;; Check allowed values.
(unless (memq value (tramp-syntax-values))
- (tramp-compat-user-error "Wrong `tramp-syntax' %s" tramp-syntax))
+ (tramp-user-error "Wrong `tramp-syntax' %s" value))
;; Cleanup existing buffers.
(unless (eq (symbol-value symbol) value)
(tramp-cleanup-all-buffers))
@@ -692,14 +753,15 @@ to be set, depending on VALUE."
;; value of `tramp-file-name-regexp'. Other Tramp syntax variables
;; must be initialized as well to proper values. We do not call
;; `custom-set-variable', this would load Tramp via custom.el.
-(eval-after-load 'tramp
- '(tramp-set-syntax 'tramp-syntax (tramp-compat-tramp-syntax)))
+(tramp--with-startup
+ (tramp-set-syntax 'tramp-syntax (tramp-compat-tramp-syntax)))
(defun tramp-syntax-values ()
"Return possible values of `tramp-syntax', a list"
(let ((values (cdr (get 'tramp-syntax 'custom-type))))
- (setq values (mapcar 'last values)
- values (mapcar 'car values))))
+ (setq values (mapcar #'last values)
+ values (mapcar #'car values))
+ values))
(defun tramp-lookup-syntax (alist)
"Look up a syntax string in ALIST according to `tramp-compat-tramp-syntax.'
@@ -716,14 +778,14 @@ Raise an error if `tramp-syntax' is invalid."
(defun tramp-build-prefix-format ()
(tramp-lookup-syntax tramp-prefix-format-alist))
-(defvar tramp-prefix-format (tramp-build-prefix-format)
+(defvar tramp-prefix-format nil ;Initialized when defining `tramp-syntax'!
"String matching the very beginning of Tramp file names.
Used in `tramp-make-tramp-file-name'.")
(defun tramp-build-prefix-regexp ()
(concat "^" (regexp-quote tramp-prefix-format)))
-(defvar tramp-prefix-regexp (tramp-build-prefix-regexp)
+(defvar tramp-prefix-regexp nil ;Initialized when defining `tramp-syntax'!
"Regexp matching the very beginning of Tramp file names.
Should always start with \"^\". Derived from `tramp-prefix-format'.")
@@ -736,7 +798,7 @@ Should always start with \"^\". Derived from `tramp-prefix-format'.")
(defun tramp-build-method-regexp ()
(tramp-lookup-syntax tramp-method-regexp-alist))
-(defvar tramp-method-regexp (tramp-build-method-regexp)
+(defvar tramp-method-regexp nil ;Initialized when defining `tramp-syntax'!
"Regexp matching methods identifiers.
The `ftp' syntax does not support methods.")
@@ -749,7 +811,7 @@ The `ftp' syntax does not support methods.")
(defun tramp-build-postfix-method-format ()
(tramp-lookup-syntax tramp-postfix-method-format-alist))
-(defvar tramp-postfix-method-format (tramp-build-postfix-method-format)
+(defvar tramp-postfix-method-format nil ;Init'd when defining `tramp-syntax'!
"String matching delimiter between method and user or host names.
The `ftp' syntax does not support methods.
Used in `tramp-make-tramp-file-name'.")
@@ -757,18 +819,16 @@ Used in `tramp-make-tramp-file-name'.")
(defun tramp-build-postfix-method-regexp ()
(regexp-quote tramp-postfix-method-format))
-(defvar tramp-postfix-method-regexp (tramp-build-postfix-method-regexp)
+(defvar tramp-postfix-method-regexp nil ;Init'd when defining `tramp-syntax'!
"Regexp matching delimiter between method and user or host names.
Derived from `tramp-postfix-method-format'.")
(defconst tramp-user-regexp "[^/|: \t]+"
"Regexp matching user names.")
-;;;###tramp-autoload
(defconst tramp-prefix-domain-format "%"
"String matching delimiter between user and domain names.")
-;;;###tramp-autoload
(defconst tramp-prefix-domain-regexp (regexp-quote tramp-prefix-domain-format)
"Regexp matching delimiter between user and domain names.
Derived from `tramp-prefix-domain-format'.")
@@ -802,21 +862,21 @@ Derived from `tramp-postfix-user-format'.")
(defun tramp-build-prefix-ipv6-format ()
(tramp-lookup-syntax tramp-prefix-ipv6-format-alist))
-(defvar tramp-prefix-ipv6-format (tramp-build-prefix-ipv6-format)
+(defvar tramp-prefix-ipv6-format nil ;Initialized when defining `tramp-syntax'!
"String matching left hand side of IPv6 addresses.
Used in `tramp-make-tramp-file-name'.")
(defun tramp-build-prefix-ipv6-regexp ()
(regexp-quote tramp-prefix-ipv6-format))
-(defvar tramp-prefix-ipv6-regexp (tramp-build-prefix-ipv6-regexp)
+(defvar tramp-prefix-ipv6-regexp nil ;Initialized when defining `tramp-syntax'!
"Regexp matching left hand side of IPv6 addresses.
Derived from `tramp-prefix-ipv6-format'.")
;; The following regexp is a bit sloppy. But it shall serve our
;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
;; "::ffff:192.168.0.1".
-(defconst tramp-ipv6-regexp "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
+(defconst tramp-ipv6-regexp "\\(?:[a-zA-Z0-9]*:\\)+[a-zA-Z0-9.]+"
"Regexp matching IPv6 addresses.")
(defconst tramp-postfix-ipv6-format-alist
@@ -828,14 +888,14 @@ Derived from `tramp-prefix-ipv6-format'.")
(defun tramp-build-postfix-ipv6-format ()
(tramp-lookup-syntax tramp-postfix-ipv6-format-alist))
-(defvar tramp-postfix-ipv6-format (tramp-build-postfix-ipv6-format)
+(defvar tramp-postfix-ipv6-format nil ;Initialized when defining `tramp-syntax'!
"String matching right hand side of IPv6 addresses.
Used in `tramp-make-tramp-file-name'.")
(defun tramp-build-postfix-ipv6-regexp ()
(regexp-quote tramp-postfix-ipv6-format))
-(defvar tramp-postfix-ipv6-regexp (tramp-build-postfix-ipv6-regexp)
+(defvar tramp-postfix-ipv6-regexp nil ;Initialized when defining `tramp-syntax'!
"Regexp matching right hand side of IPv6 addresses.
Derived from `tramp-postfix-ipv6-format'.")
@@ -871,18 +931,18 @@ Derived from `tramp-postfix-hop-format'.")
(defun tramp-build-postfix-host-format ()
(tramp-lookup-syntax tramp-postfix-host-format-alist))
-(defvar tramp-postfix-host-format (tramp-build-postfix-host-format)
+(defvar tramp-postfix-host-format nil ;Initialized when defining `tramp-syntax'!
"String matching delimiter between host names and localnames.
Used in `tramp-make-tramp-file-name'.")
(defun tramp-build-postfix-host-regexp ()
(regexp-quote tramp-postfix-host-format))
-(defvar tramp-postfix-host-regexp (tramp-build-postfix-host-regexp)
+(defvar tramp-postfix-host-regexp nil ;Initialized when defining `tramp-syntax'!
"Regexp matching delimiter between host names and localnames.
Derived from `tramp-postfix-host-format'.")
-(defconst tramp-localname-regexp ".*$"
+(defconst tramp-localname-regexp "[^\n\r]*\\'"
"Regexp matching localnames.")
(defconst tramp-unknown-id-string "UNKNOWN"
@@ -905,7 +965,7 @@ It is expected, that `tramp-syntax' has the proper value."
"\\(?:" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"))
(defvar tramp-remote-file-name-spec-regexp
- (tramp-build-remote-file-name-spec-regexp)
+ nil ;Initialized when defining `tramp-syntax'!
"Regular expression matching a Tramp file name between prefix and postfix.")
(defun tramp-build-file-name-structure ()
@@ -921,7 +981,7 @@ See `tramp-file-name-structure'."
"\\(" tramp-localname-regexp "\\)")
5 6 7 8 1))
-(defvar tramp-file-name-structure (tramp-build-file-name-structure)
+(defvar tramp-file-name-structure nil ;Initialized when defining `tramp-syntax'!
"List of six elements (REGEXP METHOD USER HOST FILE HOP), detailing \
the Tramp file name structure.
@@ -956,6 +1016,13 @@ This regexp should match Tramp file names but no other file
names. When calling `tramp-register-file-name-handlers', the
initial value is overwritten by the car of `tramp-file-name-structure'.")
+;;;###autoload
+(defcustom tramp-ignored-file-name-regexp nil
+ "Regular expression matching file names that are not under Tramp’s control."
+ :version "27.1"
+ :group 'tramp
+ :type '(choice (const nil) regexp))
+
(defconst tramp-completion-file-name-regexp-default
(concat
"\\`/\\("
@@ -1007,7 +1074,7 @@ See `tramp-file-name-structure' for more explanations.")
(tramp-lookup-syntax tramp-completion-file-name-regexp-alist))
(defvar tramp-completion-file-name-regexp
- (tramp-build-completion-file-name-regexp)
+ nil ;Initialized when defining `tramp-syntax'!
"Regular expression matching file names handled by Tramp completion.
This regexp should match partial Tramp file names only.
@@ -1149,23 +1216,15 @@ means to use always cached values for the directory contents."
;;; Internal Variables:
-(defvar tramp-current-method nil
- "Connection method for this *tramp* buffer.")
-
-(defvar tramp-current-user nil
- "Remote login name for this *tramp* buffer.")
-
-(defvar tramp-current-domain nil
- "Remote domain name for this *tramp* buffer.")
-
-(defvar tramp-current-host nil
- "Remote host for this *tramp* buffer.")
-
-(defvar tramp-current-port nil
- "Remote port for this *tramp* buffer.")
-
(defvar tramp-current-connection nil
- "Last connection timestamp.")
+ "Last connection timestamp.
+It is a cons cell of the actual `tramp-file-name-structure', and
+the (optional) timestamp of last activity on this connection.")
+
+(defvar tramp-password-save-function nil
+ "Password save function.
+Will be called once the password has been verified by successful
+authentication.")
(defconst tramp-completion-file-name-handler-alist
'((file-name-all-completions
@@ -1177,7 +1236,6 @@ Operations not mentioned here will be handled by Tramp's file
name handler functions, or the normal Emacs functions.")
;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
-;;;###tramp-autoload
(defvar tramp-foreign-file-name-handler-alist nil
"Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
@@ -1216,6 +1274,7 @@ If nil, return `tramp-default-port'."
(or (tramp-file-name-port vec)
(tramp-get-method-parameter vec 'tramp-default-port)))
+;; Comparision of file names is performed by `tramp-equal-remote'.
(defun tramp-file-name-equal-p (vec1 vec2)
"Check, whether VEC1 and VEC2 denote the same `tramp-file-name'."
(and (tramp-file-name-p vec1) (tramp-file-name-p vec2)
@@ -1246,22 +1305,24 @@ entry does not exist, return nil."
"Return unquoted localname component of VEC."
(tramp-compat-file-name-unquote (tramp-file-name-localname vec)))
-;;;###tramp-autoload
(defun tramp-tramp-file-p (name)
"Return t if NAME is a string with Tramp file name syntax."
- (and (stringp name)
+ (and tramp-mode (stringp name)
;; No "/:" and "/c:". This is not covered by `tramp-file-name-regexp'.
(not (string-match-p
(if (memq system-type '(cygwin windows-nt))
"^/[[:alpha:]]?:" "^/:")
name))
+ ;; Excluded file names.
+ (or (null tramp-ignored-file-name-regexp)
+ (not (string-match-p tramp-ignored-file-name-regexp name)))
(string-match-p tramp-file-name-regexp name)
t))
(defun tramp-find-method (method user host)
"Return the right method string to use.
This is METHOD, if non-nil. Otherwise, do a lookup in
-`tramp-default-method-alist'."
+`tramp-default-method-alist' and `tramp-default-method'."
(when (and method
(or (string-equal method "")
(string-equal method tramp-default-method-marker)))
@@ -1272,8 +1333,8 @@ This is METHOD, if non-nil. Otherwise, do a lookup in
lmethod item)
(while choices
(setq item (pop choices))
- (when (and (string-match (or (nth 0 item) "") (or host ""))
- (string-match (or (nth 1 item) "") (or user "")))
+ (when (and (string-match-p (or (nth 0 item) "") (or host ""))
+ (string-match-p (or (nth 1 item) "") (or user "")))
(setq lmethod (nth 2 item))
(setq choices nil)))
lmethod)
@@ -1286,15 +1347,15 @@ This is METHOD, if non-nil. Otherwise, do a lookup in
(defun tramp-find-user (method user host)
"Return the right user string to use.
This is USER, if non-nil. Otherwise, do a lookup in
-`tramp-default-user-alist'."
+`tramp-default-user-alist' and `tramp-default-user'."
(let ((result
(or user
(let ((choices tramp-default-user-alist)
luser item)
(while choices
(setq item (pop choices))
- (when (and (string-match (or (nth 0 item) "") (or method ""))
- (string-match (or (nth 1 item) "") (or host "")))
+ (when (and (string-match-p (or (nth 0 item) "") (or method ""))
+ (string-match-p (or (nth 1 item) "") (or host "")))
(setq luser (nth 2 item))
(setq choices nil)))
luser)
@@ -1306,18 +1367,24 @@ This is USER, if non-nil. Otherwise, do a lookup in
(defun tramp-find-host (method user host)
"Return the right host string to use.
-This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
- (or (and (> (length host) 0) host)
- (let ((choices tramp-default-host-alist)
- lhost item)
- (while choices
- (setq item (pop choices))
- (when (and (string-match (or (nth 0 item) "") (or method ""))
- (string-match (or (nth 1 item) "") (or user "")))
- (setq lhost (nth 2 item))
- (setq choices nil)))
- lhost)
- tramp-default-host))
+This is HOST, if non-nil. Otherwise, do a lookup in
+`tramp-default-host-alist' and `tramp-default-host'."
+ (let ((result
+ (or (and (> (length host) 0) host)
+ (let ((choices tramp-default-host-alist)
+ lhost item)
+ (while choices
+ (setq item (pop choices))
+ (when (and (string-match-p (or (nth 0 item) "") (or method ""))
+ (string-match-p (or (nth 1 item) "") (or user "")))
+ (setq lhost (nth 2 item))
+ (setq choices nil)))
+ lhost)
+ tramp-default-host)))
+ ;; We must mark, whether a default value has been used.
+ (if (or (> (length host) 0) (null result))
+ result
+ (propertize result 'tramp-default t))))
(defun tramp-dissect-file-name (name &optional nodefault)
"Return a `tramp-file-name' structure of NAME, a remote file name.
@@ -1329,7 +1396,7 @@ to their default values. For the other file name parts, no
default values are used."
(save-match-data
(unless (tramp-tramp-file-p name)
- (tramp-compat-user-error nil "Not a Tramp file name: \"%s\"" name))
+ (tramp-user-error nil "Not a Tramp file name: \"%s\"" name))
(if (not (string-match (nth 0 tramp-file-name-structure) name))
(error "`tramp-file-name-structure' didn't match!")
(let ((method (match-string (nth 1 tramp-file-name-structure) name))
@@ -1337,7 +1404,7 @@ default values are used."
(host (match-string (nth 3 tramp-file-name-structure) name))
(localname (match-string (nth 4 tramp-file-name-structure) name))
(hop (match-string (nth 5 tramp-file-name-structure) name))
- domain port)
+ domain port v)
(when user
(when (string-match tramp-user-with-domain-regexp user)
(setq domain (match-string 2 user)
@@ -1353,13 +1420,56 @@ default values are used."
(setq host (replace-match "" nil t host))))
(unless nodefault
- (setq method (tramp-find-method method user host)
- user (tramp-find-user method user host)
- host (tramp-find-host method user host)))
-
- (make-tramp-file-name
- :method method :user user :domain domain :host host :port port
- :localname (or localname "") :hop hop)))))
+ (when hop
+ (setq v (tramp-dissect-hop-name hop)
+ hop (and hop (tramp-make-tramp-hop-name v))))
+ (let ((tramp-default-host
+ (or (and v (not (string-match-p "%h" (tramp-file-name-host v)))
+ (tramp-file-name-host v))
+ tramp-default-host)))
+ (setq method (tramp-find-method method user host)
+ user (tramp-find-user method user host)
+ host (tramp-find-host method user host)
+ hop
+ (and hop
+ (format-spec hop (format-spec-make ?h host ?u user))))))
+
+ ;; Return result.
+ (prog1
+ (setq v (make-tramp-file-name
+ :method method :user user :domain domain :host host
+ :port port :localname localname :hop hop))
+ ;; The method must be known.
+ (unless (or (tramp-completion-mode-p)
+ (string-equal method tramp-default-method-marker)
+ (assoc method tramp-methods))
+ (tramp-user-error
+ v "Method `%s' is not known." method))
+ ;; Only some methods from tramp-sh.el do support multi-hops.
+ (when (and
+ hop
+ (or (not (tramp-get-method-parameter v 'tramp-login-program))
+ (tramp-get-method-parameter v 'tramp-copy-program)))
+ (tramp-user-error
+ v "Method `%s' is not supported for multi-hops." method)))))))
+
+(defun tramp-dissect-hop-name (name &optional nodefault)
+ "Return a `tramp-file-name' structure of `hop' part of NAME.
+See `tramp-dissect-file-name' for details."
+ (let ((v (tramp-dissect-file-name
+ (concat tramp-prefix-format
+ (replace-regexp-in-string
+ (concat tramp-postfix-hop-regexp "$")
+ tramp-postfix-host-format name))
+ nodefault)))
+ ;; Only some methods from tramp-sh.el do support multi-hops.
+ (when (or (not (tramp-get-method-parameter v 'tramp-login-program))
+ (tramp-get-method-parameter v 'tramp-copy-program))
+ (tramp-user-error
+ v "Method `%s' is not supported for multi-hops."
+ (tramp-file-name-method v)))
+ ;; Return result.
+ v))
(defun tramp-buffer-name (vec)
"A name for the connection buffer VEC."
@@ -1370,33 +1480,75 @@ default values are used."
(format "*tramp/%s %s@%s*" method user-domain host-port)
(format "*tramp/%s %s*" method host-port))))
-(defun tramp-make-tramp-file-name
- (method user domain host port localname &optional hop)
- "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
-When not nil, optional DOMAIN, PORT and HOP are used."
- ;; Unless `tramp-syntax' is `simplified', we need a method.
- (when (and (not (zerop (length tramp-postfix-method-format)))
- (zerop (length method)))
- (signal 'wrong-type-argument (list 'stringp method)))
- (concat tramp-prefix-format hop
- (unless (zerop (length tramp-postfix-method-format))
- (concat method tramp-postfix-method-format))
- user
- (unless (zerop (length domain))
- (concat tramp-prefix-domain-format domain))
- (unless (zerop (length user))
- tramp-postfix-user-format)
- (when host
- (if (string-match tramp-ipv6-regexp host)
- (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
- host))
- (unless (zerop (length port))
- (concat tramp-prefix-port-format port))
- tramp-postfix-host-format
- (when localname localname)))
+(defun tramp-make-tramp-file-name (&rest args)
+ "Construct a Tramp file name from ARGS.
+
+ARGS could have two different signatures. The first one is of
+type (VEC &optional LOCALNAME HOP).
+If LOCALNAME is nil, the value in VEC is used. If it is a
+symbol, a null localname will be used. Otherwise, LOCALNAME is
+expected to be a string, which will be used.
+If HOP is nil, the value in VEC is used. If it is a symbol, a
+null hop will be used. Otherwise, HOP is expected to be a
+string, which will be used.
+
+The other signature exists for backward compatibility. It has
+the form (METHOD USER DOMAIN HOST PORT LOCALNAME &optional HOP)."
+ (let (method user domain host port localname hop)
+ (cond
+ ((tramp-file-name-p (car args))
+ (setq method (tramp-file-name-method (car args))
+ user (tramp-file-name-user (car args))
+ domain (tramp-file-name-domain (car args))
+ host (tramp-file-name-host (car args))
+ port (tramp-file-name-port (car args))
+ localname (tramp-file-name-localname (car args))
+ hop (tramp-file-name-hop (car args)))
+ (when (cadr args)
+ (setq localname (and (stringp (cadr args)) (cadr args))))
+ (when (cl-caddr args)
+ (setq hop (and (stringp (cl-caddr args)) (cl-caddr args)))))
+
+ (t (setq method (nth 0 args)
+ user (nth 1 args)
+ domain (nth 2 args)
+ host (nth 3 args)
+ port (nth 4 args)
+ localname (nth 5 args)
+ hop (nth 6 args))))
+
+ ;; Unless `tramp-syntax' is `simplified', we need a method.
+ (when (and (not (zerop (length tramp-postfix-method-format)))
+ (zerop (length method)))
+ (signal 'wrong-type-argument (list #'stringp method)))
+ (concat tramp-prefix-format hop
+ (unless (zerop (length tramp-postfix-method-format))
+ (concat method tramp-postfix-method-format))
+ user
+ (unless (zerop (length domain))
+ (concat tramp-prefix-domain-format domain))
+ (unless (zerop (length user))
+ tramp-postfix-user-format)
+ (when host
+ (if (string-match-p tramp-ipv6-regexp host)
+ (concat
+ tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
+ host))
+ (unless (zerop (length port))
+ (concat tramp-prefix-port-format port))
+ tramp-postfix-host-format
+ localname)))
+
+(defun tramp-make-tramp-hop-name (vec)
+ "Construct a Tramp hop name from VEC."
+ (replace-regexp-in-string
+ tramp-prefix-regexp ""
+ (replace-regexp-in-string
+ (concat tramp-postfix-host-regexp "$") tramp-postfix-hop-format
+ (tramp-make-tramp-file-name vec 'noloc))))
(defun tramp-completion-make-tramp-file-name (method user host localname)
- "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
+ "Construct a Tramp file name from METHOD, USER, HOST and LOCALNAME.
It must not be a complete Tramp file name, but as long as there are
necessary only. This function will be used in file name completion."
(concat tramp-prefix-format
@@ -1407,7 +1559,7 @@ necessary only. This function will be used in file name completion."
(concat user tramp-postfix-user-format))
(unless (zerop (length host))
(concat
- (if (string-match tramp-ipv6-regexp host)
+ (if (string-match-p tramp-ipv6-regexp host)
(concat
tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
host)
@@ -1423,15 +1575,8 @@ necessary only. This function will be used in file name completion."
(tramp-set-connection-property
vec "process-buffer"
(tramp-get-connection-property vec "process-buffer" nil))
- (setq buffer-undo-list t)
- (setq default-directory
- (tramp-make-tramp-file-name
- (tramp-file-name-method vec)
- (tramp-file-name-user vec)
- (tramp-file-name-domain vec)
- (tramp-file-name-host vec)
- (tramp-file-name-port vec)
- "/"))
+ (setq buffer-undo-list t
+ default-directory (tramp-make-tramp-file-name vec 'noloc 'nohop))
(current-buffer))))
(defun tramp-get-connection-buffer (vec)
@@ -1506,8 +1651,6 @@ The outline level is equal to the verbosity of the Tramp message."
(get-buffer-create (tramp-debug-buffer-name vec))
(when (bobp)
(setq buffer-undo-list t)
- ;; So it does not get loaded while `outline-regexp' is let-bound.
- (require 'outline)
;; Activate `outline-mode'. This runs `text-mode-hook' and
;; `outline-mode-hook'. We must prevent that local processes
;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
@@ -1517,7 +1660,9 @@ The outline level is equal to the verbosity of the Tramp message."
(outline-regexp tramp-debug-outline-regexp))
(outline-mode))
(set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
- (set (make-local-variable 'outline-level) 'tramp-debug-outline-level))
+ (set (make-local-variable 'outline-level) 'tramp-debug-outline-level)
+ ;; Do not edit the debug buffer.
+ (use-local-map special-mode-map))
(current-buffer)))
(defsubst tramp-debug-message (vec fmt-string &rest arguments)
@@ -1533,10 +1678,13 @@ ARGUMENTS to actually emit the message (if applicable)."
";; Emacs: %s Tramp: %s -*- mode: outline; -*-"
emacs-version tramp-version))
(when (>= tramp-verbose 10)
- (insert
- (format
- "\n;; Location: %s Git: %s"
- (locate-library "tramp") (tramp-repository-get-version)))))
+ (let ((tramp-verbose 0))
+ (insert
+ (format
+ "\n;; Location: %s Git: %s/%s"
+ (locate-library "tramp")
+ (or tramp-repository-branch "")
+ (or tramp-repository-version ""))))))
(unless (bolp)
(insert "\n"))
;; Timestamp.
@@ -1554,22 +1702,23 @@ ARGUMENTS to actually emit the message (if applicable)."
(setq fn (symbol-name btf))
(unless
(and
- (string-match "^tramp" fn)
+ (string-match-p "^tramp" fn)
(not
- (string-match
- (concat
- "^"
- (regexp-opt
- '("tramp-backtrace"
- "tramp-compat-funcall"
- "tramp-compat-user-error"
- "tramp-condition-case-unless-debug"
- "tramp-debug-message"
- "tramp-error"
- "tramp-error-with-buffer"
- "tramp-message")
- t)
- "$")
+ (string-match-p
+ (eval-when-compile
+ (concat
+ "^"
+ (regexp-opt
+ '("tramp-backtrace"
+ "tramp-compat-funcall"
+ "tramp-debug-message"
+ "tramp-error"
+ "tramp-error-with-buffer"
+ "tramp-message"
+ "tramp-signal-hook-function"
+ "tramp-user-error")
+ t)
+ "$"))
fn)))
(setq fn nil)))
(setq btn (1+ btn))))
@@ -1607,54 +1756,54 @@ control string and the remaining ARGUMENTS to actually emit the message (if
applicable)."
(ignore-errors
(when (<= level tramp-verbose)
- ;; Match data must be preserved!
- (save-match-data
- ;; Display only when there is a minimum level.
- (when (and tramp-message-show-message (<= level 3))
- (apply 'message
- (concat
- (cond
- ((= level 0) "")
- ((= level 1) "")
- ((= level 2) "Warning: ")
- (t "Tramp: "))
- fmt-string)
- arguments))
- ;; Log only when there is a minimum level.
- (when (>= tramp-verbose 4)
- ;; Translate proc to vec.
- (when (processp vec-or-proc)
- (let ((tramp-verbose 0))
- (setq vec-or-proc
- (tramp-get-connection-property vec-or-proc "vector" nil))))
+ ;; Display only when there is a minimum level.
+ (when (and tramp-message-show-message (<= level 3))
+ (apply #'message
+ (concat
+ (cond
+ ((= level 0) "")
+ ((= level 1) "")
+ ((= level 2) "Warning: ")
+ (t "Tramp: "))
+ fmt-string)
+ arguments))
+ ;; Log only when there is a minimum level.
+ (when (>= tramp-verbose 4)
+ (let ((tramp-verbose 0))
;; Append connection buffer for error messages.
(when (= level 1)
- (let ((tramp-verbose 0))
- (with-current-buffer (tramp-get-connection-buffer vec-or-proc)
- (setq fmt-string (concat fmt-string "\n%s")
- arguments (append arguments (list (buffer-string)))))))
- ;; Do it.
- (when (tramp-file-name-p vec-or-proc)
- (apply 'tramp-debug-message
- vec-or-proc
- (concat (format "(%d) # " level) fmt-string)
- arguments)))))))
+ (with-current-buffer
+ (if (processp vec-or-proc)
+ (process-buffer vec-or-proc)
+ (tramp-get-connection-buffer vec-or-proc))
+ (setq fmt-string (concat fmt-string "\n%s")
+ arguments (append arguments (list (buffer-string))))))
+ ;; Translate proc to vec.
+ (when (processp vec-or-proc)
+ (setq vec-or-proc (process-get vec-or-proc 'vector))))
+ ;; Do it.
+ (when (tramp-file-name-p vec-or-proc)
+ (apply #'tramp-debug-message
+ vec-or-proc
+ (concat (format "(%d) # " level) fmt-string)
+ arguments))))))
(defsubst tramp-backtrace (&optional vec-or-proc)
"Dump a backtrace into the debug buffer.
If VEC-OR-PROC is nil, the buffer *debug tramp* is used. This
function is meant for debugging purposes."
- (if vec-or-proc
- (tramp-message vec-or-proc 10 "\n%s" (with-output-to-string (backtrace)))
- (if (>= tramp-verbose 10)
- (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
+ (when (>= tramp-verbose 10)
+ (if vec-or-proc
+ (tramp-message
+ vec-or-proc 10 "\n%s" (with-output-to-string (backtrace)))
+ (with-output-to-temp-buffer "*debug tramp*" (backtrace)))))
(defsubst tramp-error (vec-or-proc signal fmt-string &rest arguments)
"Emit an error.
VEC-OR-PROC identifies the connection to use, SIGNAL is the
signal identifier to be raised, remaining arguments passed to
`tramp-message'. Finally, signal SIGNAL is raised."
- (let (tramp-message-show-message)
+ (let (tramp-message-show-message signal-hook-function)
(tramp-backtrace vec-or-proc)
(unless arguments
;; FMT-STRING could be just a file name, as in
@@ -1685,7 +1834,7 @@ an input event arrives. The other arguments are passed to `tramp-error'."
(and buf (with-current-buffer buf
(tramp-dissect-file-name default-directory))))))
(unwind-protect
- (apply 'tramp-error vec-or-proc signal fmt-string arguments)
+ (apply #'tramp-error vec-or-proc signal fmt-string arguments)
;; Save exit.
(when (and buf
tramp-message-show-message
@@ -1697,7 +1846,7 @@ an input event arrives. The other arguments are passed to `tramp-error'."
(let ((enable-recursive-minibuffers t))
;; `tramp-error' does not show messages. So we must do it
;; ourselves.
- (apply 'message fmt-string arguments)
+ (apply #'message fmt-string arguments)
;; Show buffer.
(pop-to-buffer buf)
(discard-input)
@@ -1706,6 +1855,28 @@ an input event arrives. The other arguments are passed to `tramp-error'."
(when (tramp-file-name-equal-p vec (car tramp-current-connection))
(setcdr tramp-current-connection (current-time)))))))
+;; We must make it a defun, because it is used earlier already.
+(defun tramp-user-error (vec-or-proc fmt-string &rest arguments)
+ "Signal a user error (or \"pilot error\")."
+ (unwind-protect
+ (apply #'tramp-error vec-or-proc 'user-error fmt-string arguments)
+ ;; Save exit.
+ (when (and tramp-message-show-message
+ (not (zerop tramp-verbose))
+ ;; Do not show when flagged from outside.
+ (not (tramp-completion-mode-p))
+ ;; Show only when Emacs has started already.
+ (current-message))
+ (let ((enable-recursive-minibuffers t))
+ ;; `tramp-error' does not show messages. So we must do it ourselves.
+ (apply #'message fmt-string arguments)
+ (discard-input)
+ (sit-for 30)
+ ;; Reset timestamp. It would be wrong after waiting for a while.
+ (when
+ (tramp-file-name-equal-p vec-or-proc (car tramp-current-connection))
+ (setcdr tramp-current-connection (current-time)))))))
+
(defmacro tramp-with-demoted-errors (vec-or-proc format &rest body)
"Execute BODY while redirecting the error message to `tramp-message'.
BODY is executed like wrapped by `with-demoted-errors'. FORMAT
@@ -1718,6 +1889,12 @@ the resulting error message."
(progn ,@body)
(error (tramp-message ,vec-or-proc 3 ,format ,err) nil))))
+;; This function provides traces in case of errors not triggered by
+;; Tramp functions.
+(defun tramp-signal-hook-function (error-symbol data)
+ "Funtion to be called via `signal-hook-function'."
+ (tramp-error (car tramp-current-connection) error-symbol "%s" data))
+
(defmacro with-parsed-tramp-file-name (filename var &rest body)
"Parse a Tramp filename and make components available in the body.
@@ -1752,12 +1929,12 @@ If VAR is nil, then we bind `v' to the structure and `method', `user',
(put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
-(defun tramp-progress-reporter-update (reporter &optional value)
+(defun tramp-progress-reporter-update (reporter &optional value suffix)
"Report progress of an operation for Tramp."
(let* ((parameters (cdr reporter))
(message (aref parameters 3)))
- (when (string-match message (or (current-message) ""))
- (progress-reporter-update reporter value))))
+ (when (string-match-p message (or (current-message) ""))
+ (tramp-compat-progress-reporter-update reporter value suffix))))
(defmacro with-tramp-progress-reporter (vec level message &rest body)
"Executes BODY, spinning a progress reporter with MESSAGE.
@@ -1829,7 +2006,7 @@ letter into the file name. This function removes it."
(save-match-data
(funcall
(if (tramp-compat-file-name-quoted-p name)
- 'tramp-compat-file-name-quote 'identity)
+ #'tramp-compat-file-name-quote #'identity)
(let ((name (tramp-compat-file-name-unquote name)))
(if (string-match "\\`[a-zA-Z]:/" name)
(replace-match "/" nil t name)
@@ -1837,7 +2014,6 @@ letter into the file name. This function removes it."
;;; Config Manipulation Functions:
-;;;###tramp-autoload
(defun tramp-set-completion-function (method function-list)
"Sets the list of completion functions for METHOD.
FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
@@ -1851,7 +2027,6 @@ Example:
\"ssh\"
\\='((tramp-parse-sconfig \"/etc/ssh_config\")
(tramp-parse-sconfig \"~/.ssh/config\")))"
-
(let ((r function-list)
(v function-list))
(setq tramp-completion-function-alist
@@ -1866,13 +2041,13 @@ Example:
(unless (and (functionp (nth 0 (car v)))
(cond
;; Windows registry.
- ((string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
+ ((string-match-p "^HKEY_CURRENT_USER" (nth 1 (car v)))
(and (memq system-type '(cygwin windows-nt))
(zerop
(tramp-call-process
v "reg" nil nil nil "query" (nth 1 (car v))))))
;; Zeroconf service type.
- ((string-match
+ ((string-match-p
"^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v))))
;; Configuration file.
(t (file-exists-p (nth 1 (car v))))))
@@ -1889,82 +2064,13 @@ For definition of that list see `tramp-set-completion-function'."
(append
`(;; Default settings are taken into account.
(tramp-parse-default-user-host ,method)
+ ;; Hits from auth-sources.
+ (tramp-parse-auth-sources ,method)
;; Hosts visited once shall be remembered.
(tramp-parse-connection-properties ,method))
;; The method related defaults.
(cdr (assoc method tramp-completion-function-alist))))
-;;; Fontification of `read-file-name':
-
-(defvar tramp-rfn-eshadow-overlay)
-(make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
-
-(defun tramp-rfn-eshadow-setup-minibuffer ()
- "Set up a minibuffer for `file-name-shadow-mode'.
-Adds another overlay hiding filename parts according to Tramp's
-special handling of `substitute-in-file-name'."
- (when minibuffer-completing-file-name
- (setq tramp-rfn-eshadow-overlay
- (make-overlay (minibuffer-prompt-end) (minibuffer-prompt-end)))
- ;; Copy rfn-eshadow-overlay properties.
- (let ((props (overlay-properties rfn-eshadow-overlay)))
- (while props
- ;; The `field' property prevents correct minibuffer
- ;; completion; we exclude it.
- (if (not (eq (car props) 'field))
- (overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))
- (pop props) (pop props))))))
-
-(add-hook 'rfn-eshadow-setup-minibuffer-hook
- 'tramp-rfn-eshadow-setup-minibuffer)
-(add-hook 'tramp-unload-hook
- (lambda ()
- (remove-hook 'rfn-eshadow-setup-minibuffer-hook
- 'tramp-rfn-eshadow-setup-minibuffer)))
-
-(defun tramp-rfn-eshadow-update-overlay-regexp ()
- (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
-
-;; Package rfn-eshadow is preloaded in Emacs, but for some reason,
-;; it only did (defvar rfn-eshadow-overlay) without giving it a global
-;; value, so it was only declared as dynamically-scoped within the
-;; rfn-eshadow.el file. This is now fixed in Emacs>26.1 but we still need
-;; this defvar here for older releases.
-(defvar rfn-eshadow-overlay)
-
-(defun tramp-rfn-eshadow-update-overlay ()
- "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
-This is intended to be used as a minibuffer `post-command-hook' for
-`file-name-shadow-mode'; the minibuffer should have already
-been set up by `rfn-eshadow-setup-minibuffer'."
- ;; In remote files name, there is a shadowing just for the local part.
- (ignore-errors
- (let ((end (or (overlay-end rfn-eshadow-overlay)
- (minibuffer-prompt-end)))
- ;; We do not want to send any remote command.
- (non-essential t))
- (when (tramp-tramp-file-p (buffer-substring end (point-max)))
- (save-excursion
- (save-restriction
- (narrow-to-region
- (1+ (or (string-match
- (tramp-rfn-eshadow-update-overlay-regexp)
- (buffer-string) end)
- end))
- (point-max))
- (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
- (rfn-eshadow-update-overlay-hook nil)
- file-name-handler-alist)
- (move-overlay rfn-eshadow-overlay (point-max) (point-max))
- (rfn-eshadow-update-overlay))))))))
-
-(add-hook 'rfn-eshadow-update-overlay-hook
- 'tramp-rfn-eshadow-update-overlay)
-(add-hook 'tramp-unload-hook
- (lambda ()
- (remove-hook 'rfn-eshadow-update-overlay-hook
- 'tramp-rfn-eshadow-update-overlay)))
-
;; Inodes don't exist for some file systems. Therefore we must
;; generate virtual ones. Used in `find-buffer-visiting'. The method
;; applied might be not so efficient (Ange-FTP uses hashes). But
@@ -1986,26 +2092,12 @@ been set up by `rfn-eshadow-setup-minibuffer'."
If the file modes of FILENAME cannot be determined, return the
value of `default-file-modes', without execute permissions."
(or (file-modes filename)
- (logand (default-file-modes) (string-to-number "0666" 8))))
+ (logand (default-file-modes) #o0666)))
(defun tramp-replace-environment-variables (filename)
"Replace environment variables in FILENAME.
Return the string with the replaced variables."
- (or (ignore-errors
- ;; Optional arg has been introduced with Emacs 24.4.
- (tramp-compat-funcall 'substitute-env-vars filename 'only-defined))
- ;; We need an own implementation.
- (save-match-data
- (let ((idx (string-match "$\\(\\w+\\)" filename)))
- ;; `$' is coded as `$$'.
- (when (and idx
- (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
- (getenv (match-string 1 filename)))
- (setq filename
- (replace-match
- (substitute-in-file-name (match-string 0 filename))
- t nil filename)))
- filename))))
+ (substitute-env-vars filename 'only-defined))
(defun tramp-find-file-name-coding-system-alist (filename tmpname)
"Like `find-operation-coding-system' for Tramp filenames.
@@ -2015,7 +2107,7 @@ expression, which matches more than the file name suffix, the
coding system might not be determined. This function repairs it."
(let (result)
(dolist (elt file-coding-system-alist (nreverse result))
- (when (and (consp elt) (string-match (car elt) filename))
+ (when (and (consp elt) (string-match-p (car elt) filename))
;; We found a matching entry in `file-coding-system-alist'.
;; So we add a similar entry, but with the temporary file name
;; as regexp.
@@ -2029,12 +2121,14 @@ pass to the OPERATION."
`(tramp-file-name-handler
tramp-vc-file-name-handler
tramp-completion-file-name-handler
+ tramp-archive-file-name-handler
cygwin-mount-name-hook-function
cygwin-mount-map-drive-hook-function
.
,(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
- (inhibit-file-name-operation operation))
+ (inhibit-file-name-operation operation)
+ signal-hook-function)
(apply operation args)))
;; We handle here all file primitives. Most of them have the file
@@ -2046,7 +2140,11 @@ pass to the OPERATION."
;; function as well but regexp only.
(defun tramp-file-name-for-operation (operation &rest args)
"Return file name related to OPERATION file primitive.
-ARGS are the arguments OPERATION has been called with."
+ARGS are the arguments OPERATION has been called with.
+
+It does not always return a Tramp file name, for example if the
+first argument of `expand-file-name' is absolute and not remote.
+Must be handled by the callers."
(cond
;; FILE resp DIRECTORY.
((member operation
@@ -2062,7 +2160,7 @@ ARGS are the arguments OPERATION has been called with."
file-ownership-preserved-p file-readable-p
file-regular-p file-remote-p file-selinux-context
file-symlink-p file-truename file-writable-p
- find-backup-file-name find-file-noselect get-file-buffer
+ find-backup-file-name get-file-buffer
insert-directory insert-file-contents load
make-directory make-directory-internal set-file-acl
set-file-modes set-file-selinux-context set-file-times
@@ -2071,26 +2169,32 @@ ARGS are the arguments OPERATION has been called with."
;; Emacs 26+ only.
file-name-case-insensitive-p
;; Emacs 27+ only.
- file-system-info))
+ file-system-info
+ ;; Tramp internal magic file name function.
+ tramp-set-file-uid-gid))
(if (file-name-absolute-p (nth 0 args))
(nth 0 args)
default-directory))
+ ;; STRING FILE.
+ ;; Starting with Emacs 26.1, just the 2nd argument of
+ ;; `make-symbolic-link' matters.
+ ((eq operation 'make-symbolic-link) (nth 1 args))
;; FILE DIRECTORY resp FILE1 FILE2.
((member operation
- '(add-name-to-file copy-directory copy-file expand-file-name
+ '(add-name-to-file copy-directory copy-file
file-equal-p file-in-directory-p
file-name-all-completions file-name-completion
- ;; Starting with Emacs 26.1, just the 2nd argument of
- ;; `make-symbolic-link' matters. For backward
- ;; compatibility, we still accept the first argument as
- ;; file name to be checked. Handled properly in
- ;; `tramp-handle-*-make-symbolic-link'.
- file-newer-than-file-p make-symbolic-link rename-file))
- (save-match-data
- (cond
- ((tramp-tramp-file-p (nth 0 args)) (nth 0 args))
- ((tramp-tramp-file-p (nth 1 args)) (nth 1 args))
- (t default-directory))))
+ file-newer-than-file-p rename-file))
+ (cond
+ ((tramp-tramp-file-p (nth 0 args)) (nth 0 args))
+ ((tramp-tramp-file-p (nth 1 args)) (nth 1 args))
+ (t default-directory)))
+ ;; FILE DIRECTORY resp FILE1 FILE2.
+ ((eq operation 'expand-file-name)
+ (cond
+ ((file-name-absolute-p (nth 0 args)) (nth 0 args))
+ ((tramp-tramp-file-p (nth 1 args)) (nth 1 args))
+ (t default-directory)))
;; START END FILE.
((eq operation 'write-region)
(if (file-name-absolute-p (nth 2 args))
@@ -2106,7 +2210,9 @@ ARGS are the arguments OPERATION has been called with."
((member operation
'(process-file shell-command start-file-process
;; Emacs 26+ only.
- make-nearby-temp-file temporary-file-directory))
+ make-nearby-temp-file temporary-file-directory
+ ;; Emacs 27+ only.
+ exec-path make-process))
default-directory)
;; PROC.
((member operation
@@ -2132,15 +2238,6 @@ ARGS are the arguments OPERATION has been called with."
res (cdr elt))))
res)))
-(defvar tramp-debug-on-error nil
- "Like `debug-on-error' but used Tramp internal.")
-
-(defmacro tramp-condition-case-unless-debug
- (var bodyform &rest handlers)
- "Like `condition-case-unless-debug' but `tramp-debug-on-error'."
- `(let ((debug-on-error tramp-debug-on-error))
- (condition-case-unless-debug ,var ,bodyform ,@handlers)))
-
;; In Emacs, there is some concurrency due to timers. If a timer
;; interrupts Tramp and wishes to use the same connection buffer as
;; the "main" Emacs, then garbage might occur in the connection
@@ -2172,100 +2269,96 @@ preventing reentrant calls of Tramp.")
(defun tramp-file-name-handler (operation &rest args)
"Invoke Tramp file name handler.
Falls back to normal file name handler if no Tramp file name handler exists."
- (let ((filename (apply 'tramp-file-name-for-operation operation args)))
- (if (and tramp-mode (tramp-tramp-file-p filename))
+ (let ((filename (apply #'tramp-file-name-for-operation operation args))
+ ;; `file-remote-p' is called for everything, even for symbolic
+ ;; links which look remote. We don't want to get an error.
+ (non-essential (or non-essential (eq operation 'file-remote-p))))
+ (if (tramp-tramp-file-p filename)
(save-match-data
(setq filename (tramp-replace-environment-variables filename))
(with-parsed-tramp-file-name filename nil
- (let ((completion (tramp-completion-mode-p))
+ (let ((current-connection tramp-current-connection)
(foreign
(tramp-find-foreign-file-name-handler filename operation))
+ (signal-hook-function #'tramp-signal-hook-function)
result)
+ ;; Set `tramp-current-connection'.
+ (unless
+ (tramp-file-name-equal-p v (car tramp-current-connection))
+ (setq tramp-current-connection (list v)))
+
;; Call the backend function.
- (if foreign
- (tramp-condition-case-unless-debug err
- (let ((sf (symbol-function foreign)))
- ;; Some packages set the default directory to a
- ;; remote path, before respective Tramp packages
- ;; are already loaded. This results in
- ;; recursive loading. Therefore, we load the
- ;; Tramp packages locally.
- (when (autoloadp sf)
- (let ((default-directory
- (tramp-compat-temporary-file-directory)))
- (load (cadr sf) 'noerror 'nomessage)))
- ;; If `non-essential' is non-nil, Tramp shall
- ;; not open a new connection.
- ;; If Tramp detects that it shouldn't continue
- ;; to work, it throws the `suppress' event.
- ;; This could happen for example, when Tramp
- ;; tries to open the same connection twice in a
- ;; short time frame.
- ;; In both cases, we try the default handler then.
- (setq result
- (catch 'non-essential
- (catch 'suppress
- (when (and tramp-locked (not tramp-locker))
- (setq tramp-locked nil)
- (tramp-error
- (car-safe tramp-current-connection)
- 'file-error
- "Forbidden reentrant call of Tramp"))
- (let ((tl tramp-locked))
- (setq tramp-locked t)
- (unwind-protect
- (let ((tramp-locker t))
- (apply foreign operation args))
- (setq tramp-locked tl))))))
- (cond
- ((eq result 'non-essential)
- (tramp-message
- v 5 "Non-essential received in operation %s"
- (cons operation args))
- (tramp-run-real-handler operation args))
- ((eq result 'suppress)
- (let (tramp-message-show-message)
+ (unwind-protect
+ (if foreign
+ (let ((sf (symbol-function foreign)))
+ ;; Some packages set the default directory to
+ ;; a remote path, before respective Tramp
+ ;; packages are already loaded. This results
+ ;; in recursive loading. Therefore, we load
+ ;; the Tramp packages locally.
+ (when (autoloadp sf)
+ ;; FIXME: Not clear why we need these bindings here.
+ ;; The explanation above is not convincing and
+ ;; the bug#9114 for which it was added doesn't
+ ;; clarify the core of the problem.
+ (let ((default-directory
+ (tramp-compat-temporary-file-directory))
+ file-name-handler-alist)
+ (autoload-do-load sf foreign)))
+ ;; (tramp-message
+ ;; v 4 "Running `%s'..." (cons operation args))
+ ;; If `non-essential' is non-nil, Tramp shall
+ ;; not open a new connection.
+ ;; If Tramp detects that it shouldn't continue
+ ;; to work, it throws the `suppress' event.
+ ;; This could happen for example, when Tramp
+ ;; tries to open the same connection twice in
+ ;; a short time frame.
+ ;; In both cases, we try the default handler then.
+ (setq result
+ (catch 'non-essential
+ (catch 'suppress
+ (when (and tramp-locked (not tramp-locker))
+ (setq tramp-locked nil)
+ (tramp-error
+ v 'file-error
+ "Forbidden reentrant call of Tramp"))
+ (let ((tl tramp-locked))
+ (setq tramp-locked t)
+ (unwind-protect
+ (let ((tramp-locker t))
+ (apply foreign operation args))
+ (setq tramp-locked tl))))))
+ ;; (tramp-message
+ ;; v 4 "Running `%s'...`%s'" (cons operation args) result)
+ (cond
+ ((eq result 'non-essential)
(tramp-message
- v 1 "Suppress received in operation %s"
+ v 5 "Non-essential received in operation %s"
(cons operation args))
- (tramp-cleanup-connection v t)
- (tramp-run-real-handler operation args)))
- (t result)))
-
- ;; Trace that somebody has interrupted the operation.
- ((debug quit)
- (let (tramp-message-show-message)
- (tramp-message
- v 1 "Interrupt received in operation %s"
- (cons operation args)))
- ;; Propagate the quit signal.
- (signal (car err) (cdr err)))
-
- ;; When we are in completion mode, some failed
- ;; operations shall return at least a default
- ;; value in order to give the user a chance to
- ;; correct the file name in the minibuffer.
- ;; In order to get a full backtrace, one could apply
- ;; (setq tramp-debug-on-error t)
- (error
- (cond
- ((and completion (zerop (length localname))
- (memq operation '(file-exists-p file-directory-p)))
- t)
- ((and completion (zerop (length localname))
- (memq operation
- '(expand-file-name file-name-as-directory)))
- filename)
- ;; Propagate the error.
- (t (signal (car err) (cdr err))))))
-
- ;; Nothing to do for us. However, since we are in
- ;; `tramp-mode', we must suppress the volume letter on
- ;; MS Windows.
- (setq result (tramp-run-real-handler operation args))
- (if (stringp result)
- (tramp-drop-volume-letter result)
- result)))))
+ (tramp-run-real-handler operation args))
+ ((eq result 'suppress)
+ (let (tramp-message-show-message)
+ (tramp-message
+ v 1 "Suppress received in operation %s"
+ (cons operation args))
+ (tramp-cleanup-connection v t)
+ (tramp-run-real-handler operation args)))
+ (t result)))
+
+ ;; Nothing to do for us. However, since we are in
+ ;; `tramp-mode', we must suppress the volume
+ ;; letter on MS Windows.
+ (setq result (tramp-run-real-handler operation args))
+ (if (stringp result)
+ (tramp-drop-volume-letter result)
+ result))
+
+ ;; Reset `tramp-current-connection'.
+ (unless
+ (tramp-file-name-equal-p
+ (car current-connection) (car tramp-current-connection))
+ (setq tramp-current-connection current-connection))))))
;; When `tramp-mode' is not enabled, or the file name is quoted,
;; we don't do anything.
@@ -2282,10 +2375,10 @@ Falls back to normal file name handler if no Tramp file name handler exists."
;;;###autoload
(progn (defun tramp-autoload-file-name-handler (operation &rest args)
"Load Tramp file name handler, and perform OPERATION."
+ (tramp-unload-file-name-handlers)
(if tramp-mode
(let ((default-directory temporary-file-directory))
- (load "tramp" 'noerror 'nomessage))
- (tramp-unload-file-name-handlers))
+ (load "tramp" 'noerror 'nomessage)))
(apply operation args)))
;; `tramp-autoload-file-name-handler' must be registered before
@@ -2312,44 +2405,47 @@ remote file names."
"^%s$"
(regexp-opt
(mapcar
- 'file-name-sans-extension
+ #'file-name-sans-extension
(directory-files dir nil "^tramp.+\\.elc?$"))
'paren))))
(mapatoms
(lambda (atom)
(when (and (functionp atom)
(autoloadp (symbol-function atom))
- (string-match files-regexp (cadr (symbol-function atom))))
+ (string-match-p files-regexp (cadr (symbol-function atom))))
(ignore-errors
(setf (cadr (symbol-function atom))
(expand-file-name (cadr (symbol-function atom)) dir))))))))
-(eval-after-load 'tramp (tramp-use-absolute-autoload-file-names))
+(tramp--with-startup (tramp-use-absolute-autoload-file-names))
(defun tramp-register-file-name-handlers ()
"Add Tramp file name handlers to `file-name-handler-alist'."
;; Remove autoloaded handlers from file name handler alist. Useful,
;; if `tramp-syntax' has been changed.
- (dolist (fnh '(tramp-file-name-handler
- tramp-completion-file-name-handler
- tramp-autoload-file-name-handler))
- (let ((a1 (rassq fnh file-name-handler-alist)))
- (setq file-name-handler-alist (delq a1 file-name-handler-alist))))
+ (tramp-unload-file-name-handlers)
;; Add the handlers. We do not add anything to the `operations'
- ;; property of `tramp-file-name-handler', this shall be done by the
+ ;; property of `tramp-file-name-handler' and
+ ;; `tramp-archive-file-name-handler', this shall be done by the
;; respective foreign handlers.
(add-to-list 'file-name-handler-alist
- (cons tramp-file-name-regexp 'tramp-file-name-handler))
+ (cons tramp-file-name-regexp #'tramp-file-name-handler))
(put 'tramp-file-name-handler 'safe-magic t)
(add-to-list 'file-name-handler-alist
(cons tramp-completion-file-name-regexp
- 'tramp-completion-file-name-handler))
+ #'tramp-completion-file-name-handler))
(put 'tramp-completion-file-name-handler 'safe-magic t)
;; Mark `operations' the handler is responsible for.
(put 'tramp-completion-file-name-handler 'operations
- (mapcar 'car tramp-completion-file-name-handler-alist))
+ (mapcar #'car tramp-completion-file-name-handler-alist))
+
+ (when (bound-and-true-p tramp-archive-enabled)
+ (add-to-list 'file-name-handler-alist
+ (cons tramp-archive-file-name-regexp
+ #'tramp-archive-file-name-handler))
+ (put 'tramp-archive-file-name-handler 'safe-magic t))
;; If jka-compr or epa-file are already loaded, move them to the
;; front of `file-name-handler-alist'.
@@ -2359,10 +2455,9 @@ remote file names."
(setq file-name-handler-alist
(cons entry (delete entry file-name-handler-alist)))))))
-(eval-after-load 'tramp (tramp-register-file-name-handlers))
+(tramp--with-startup (tramp-register-file-name-handlers))
-;;;###tramp-autoload
-(progn (defun tramp-register-foreign-file-name-handler
+(defun tramp-register-foreign-file-name-handler
(func handler &optional append)
"Register (FUNC . HANDLER) in `tramp-foreign-file-name-handler-alist'.
FUNC is the function, which determines whether HANDLER is to be called.
@@ -2376,8 +2471,8 @@ Add operations defined in `HANDLER-alist' to `tramp-file-name-handler'."
(append
(get 'tramp-file-name-handler 'operations)
(mapcar
- 'car
- (symbol-value (intern (concat (symbol-name handler) "-alist")))))))))
+ #'car
+ (symbol-value (intern (concat (symbol-name handler) "-alist"))))))))
(defun tramp-exists-file-name-handler (operation &rest args)
"Check, whether OPERATION runs a file name handler."
@@ -2402,13 +2497,12 @@ Add operations defined in `HANDLER-alist' to `tramp-file-name-handler'."
;;;###autoload
(progn (defun tramp-unload-file-name-handlers ()
"Unload Tramp file name handlers from `file-name-handler-alist'."
- (dolist (fnh '(tramp-file-name-handler
- tramp-completion-file-name-handler
- tramp-autoload-file-name-handler))
- (let ((a1 (rassq fnh file-name-handler-alist)))
- (setq file-name-handler-alist (delq a1 file-name-handler-alist))))))
+ (dolist (fnh file-name-handler-alist)
+ (when (and (symbolp (cdr fnh))
+ (string-prefix-p "tramp-" (symbol-name (cdr fnh))))
+ (setq file-name-handler-alist (delq fnh file-name-handler-alist))))))
-(add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
+(add-hook 'tramp-unload-hook #'tramp-unload-file-name-handlers)
;;; File name handler functions for completion mode:
@@ -2442,7 +2536,6 @@ not in completion mode."
;; completions.
(defun tramp-completion-handle-file-name-all-completions (filename directory)
"Like `file-name-all-completions' for partial Tramp files."
-
(let ((fullname
(tramp-drop-volume-letter (expand-file-name filename directory)))
hop result result1)
@@ -2465,7 +2558,6 @@ not in completion mode."
(host (tramp-file-name-host elt))
(localname (tramp-file-name-localname elt))
(m (tramp-find-method method user host))
- (tramp-current-user user) ; see `tramp-parse-passwd'
all-user-hosts)
(unless localname ;; Nothing to complete.
@@ -2515,7 +2607,7 @@ not in completion mode."
"Like `file-name-completion' for Tramp files."
(try-completion
filename
- (mapcar 'list (file-name-all-completions filename directory))
+ (mapcar #'list (file-name-all-completions filename directory))
(when (and predicate
(tramp-connectable-p (expand-file-name filename directory)))
(lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
@@ -2540,7 +2632,6 @@ not in completion mode."
(defun tramp-completion-dissect-file-name (name)
"Returns a list of `tramp-file-name' structures.
They are collected by `tramp-completion-dissect-file-name1'."
-
(let* ((x-nil "\\|\\(\\)")
(tramp-completion-ipv6-regexp
(format
@@ -2615,7 +2706,6 @@ They are collected by `tramp-completion-dissect-file-name1'."
"Returns a `tramp-file-name' structure matching STRUCTURE.
The structure consists of remote method, remote user,
remote host and localname (filename on remote host)."
-
(save-match-data
(when (string-match (nth 0 structure) name)
(make-tramp-file-name
@@ -2633,9 +2723,9 @@ remote host and localname (filename on remote host)."
(mapcar
(lambda (method)
(and method
- (string-match (concat "^" (regexp-quote partial-method)) method)
+ (string-match-p (concat "^" (regexp-quote partial-method)) method)
(tramp-completion-make-tramp-file-name method nil nil nil)))
- (mapcar 'car tramp-methods)))
+ (mapcar #'car tramp-methods)))
;; Compares partial user and host names with possible completions.
(defun tramp-get-completion-user-host
@@ -2646,7 +2736,7 @@ PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
((and partial-user partial-host)
(if (and host
- (string-match (concat "^" (regexp-quote partial-host)) host)
+ (string-match-p (concat "^" (regexp-quote partial-host)) host)
(string-equal partial-user (or user partial-user)))
(setq user partial-user)
(setq user nil
@@ -2655,13 +2745,15 @@ PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
(partial-user
(setq host nil)
(unless
- (and user (string-match (concat "^" (regexp-quote partial-user)) user))
+ (and user
+ (string-match-p (concat "^" (regexp-quote partial-user)) user))
(setq user nil)))
(partial-host
(setq user nil)
(unless
- (and host (string-match (concat "^" (regexp-quote partial-host)) host))
+ (and host
+ (string-match-p (concat "^" (regexp-quote partial-host)) host))
(setq host nil)))
(t (setq user nil
@@ -2676,15 +2768,33 @@ This function is added always in `tramp-get-completion-function'
for all methods. Resulting data are derived from default settings."
`((,(tramp-find-user method nil nil) ,(tramp-find-host method nil nil))))
+(defcustom tramp-completion-use-auth-sources auth-source-do-cache
+ "Whether to use `auth-source-search' for completion of user and host names.
+This could be disturbing, if it requires a password / passphrase,
+as for \"~/.authinfo.gpg\"."
+ :group 'tramp
+ :version "27.1"
+ :type 'boolean)
+
+(defun tramp-parse-auth-sources (method)
+ "Return a list of (user host) tuples allowed to access for METHOD.
+This function is added always in `tramp-get-completion-function'
+for all methods. Resulting data are derived from default settings."
+ (and tramp-completion-use-auth-sources
+ (mapcar
+ (lambda (x) `(,(plist-get x :user) ,(plist-get x :host)))
+ (auth-source-search
+ :port method :require '(:port) :max most-positive-fixnum))))
+
;; Generic function.
-(defun tramp-parse-group (regexp match-level skip-regexp)
+(defun tramp-parse-group (regexp match-level skip-chars)
"Return a (user host) tuple allowed to access.
User is always nil."
(let (result)
(when (re-search-forward regexp (point-at-eol) t)
(setq result (list nil (match-string match-level))))
(or
- (> (skip-chars-forward skip-regexp) 0)
+ (> (skip-chars-forward skip-chars) 0)
(forward-line 1))
result))
@@ -2701,11 +2811,10 @@ User is always nil."
(goto-char (point-min))
(cl-loop while (not (eobp)) collect (funcall function))))))
-;;;###tramp-autoload
(defun tramp-parse-rhosts (filename)
"Return a list of (user host) tuples allowed to access.
Either user or host may be nil."
- (tramp-parse-file filename 'tramp-parse-rhosts-group))
+ (tramp-parse-file filename #'tramp-parse-rhosts-group))
(defun tramp-parse-rhosts-group ()
"Return a (user host) tuple allowed to access.
@@ -2720,22 +2829,20 @@ Either user or host may be nil."
(forward-line 1)
result))
-;;;###tramp-autoload
(defun tramp-parse-shosts (filename)
"Return a list of (user host) tuples allowed to access.
User is always nil."
- (tramp-parse-file filename 'tramp-parse-shosts-group))
+ (tramp-parse-file filename #'tramp-parse-shosts-group))
(defun tramp-parse-shosts-group ()
"Return a (user host) tuple allowed to access.
User is always nil."
(tramp-parse-group (concat "^\\(" tramp-host-regexp "\\)") 1 ","))
-;;;###tramp-autoload
(defun tramp-parse-sconfig (filename)
"Return a list of (user host) tuples allowed to access.
User is always nil."
- (tramp-parse-file filename 'tramp-parse-sconfig-group))
+ (tramp-parse-file filename #'tramp-parse-sconfig-group))
(defun tramp-parse-sconfig-group ()
"Return a (user host) tuple allowed to access.
@@ -2743,7 +2850,7 @@ User is always nil."
(tramp-parse-group
(concat "\\(?:^[ \t]*Host\\)" "\\|" "\\(?:^.+\\)"
"\\|" "\\(" tramp-host-regexp "\\)")
- 1 "[ \t]+"))
+ 1 " \t"))
;; Generic function.
(defun tramp-parse-shostkeys-sknownhosts (dirname regexp)
@@ -2758,14 +2865,12 @@ User is always nil."
when (and (not (string-match "^\\.\\.?$" f)) (string-match regexp f))
collect (list nil (match-string 1 f)))))
-;;;###tramp-autoload
(defun tramp-parse-shostkeys (dirname)
"Return a list of (user host) tuples allowed to access.
User is always nil."
(tramp-parse-shostkeys-sknownhosts
dirname (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$")))
-;;;###tramp-autoload
(defun tramp-parse-sknownhosts (dirname)
"Return a list of (user host) tuples allowed to access.
User is always nil."
@@ -2773,11 +2878,10 @@ User is always nil."
dirname
(concat "^\\(" tramp-host-regexp "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$")))
-;;;###tramp-autoload
(defun tramp-parse-hosts (filename)
"Return a list of (user host) tuples allowed to access.
User is always nil."
- (tramp-parse-file filename 'tramp-parse-hosts-group))
+ (tramp-parse-file filename #'tramp-parse-hosts-group))
(defun tramp-parse-hosts-group ()
"Return a (user host) tuple allowed to access.
@@ -2785,7 +2889,6 @@ User is always nil."
(tramp-parse-group
(concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)") 1 " \t"))
-;;;###tramp-autoload
(defun tramp-parse-passwd (filename)
"Return a list of (user host) tuples allowed to access.
Host is always \"localhost\"."
@@ -2796,7 +2899,7 @@ Host is always \"localhost\"."
(goto-char (point-min))
(cl-loop while (not (eobp)) collect
(tramp-parse-etc-group-group))))
- (tramp-parse-file filename 'tramp-parse-passwd-group))))
+ (tramp-parse-file filename #'tramp-parse-passwd-group))))
(defun tramp-parse-passwd-group ()
"Return a (user host) tuple allowed to access.
@@ -2808,7 +2911,6 @@ Host is always \"localhost\"."
(forward-line 1)
result))
-;;;###tramp-autoload
(defun tramp-parse-etc-group (filename)
"Return a list of (group host) tuples allowed to access.
Host is always \"localhost\"."
@@ -2819,7 +2921,7 @@ Host is always \"localhost\"."
(goto-char (point-min))
(cl-loop while (not (eobp)) collect
(tramp-parse-etc-group-group))))
- (tramp-parse-file filename 'tramp-parse-etc-group-group))))
+ (tramp-parse-file filename #'tramp-parse-etc-group-group))))
(defun tramp-parse-etc-group-group ()
"Return a (group host) tuple allowed to access.
@@ -2831,26 +2933,18 @@ Host is always \"localhost\"."
(forward-line 1)
result))
-;;;###tramp-autoload
(defun tramp-parse-netrc (filename)
"Return a list of (user host) tuples allowed to access.
User may be nil."
- (tramp-parse-file filename 'tramp-parse-netrc-group))
-
-(defun tramp-parse-netrc-group ()
- "Return a (user host) tuple allowed to access.
-User may be nil."
- (let ((result)
- (regexp
- (concat
- "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
- "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
- (when (re-search-forward regexp (point-at-eol) t)
- (setq result (list (match-string 3) (match-string 1))))
- (forward-line 1)
- result))
+ ;; The declaration is not sufficient at runtime, because netrc.el is
+ ;; not autoloaded.
+ (autoload 'netrc-parse "netrc")
+ (mapcar
+ (lambda (item)
+ (and (assoc "machine" item)
+ `(,(cdr (assoc "login" item)) ,(cdr (assoc "machine" item)))))
+ (netrc-parse filename)))
-;;;###tramp-autoload
(defun tramp-parse-putty (registry-or-dirname)
"Return a list of (user host) tuples allowed to access.
User is always nil."
@@ -2884,6 +2978,13 @@ User is always nil."
(defvar tramp-handle-write-region-hook nil
"Normal hook to be run at the end of `tramp-*-handle-write-region'.")
+(defun tramp-handle-access-file (filename string)
+ "Like `access-file' for Tramp files."
+ (unless (file-readable-p filename)
+ (tramp-error
+ (tramp-dissect-file-name filename) tramp-file-missing
+ "%s: No such file or directory %s" string filename)))
+
(defun tramp-handle-add-name-to-file
(filename newname &optional ok-if-already-exists)
"Like `add-name-to-file' for Tramp files."
@@ -2905,8 +3006,8 @@ User is always nil."
localname)))))
(tramp-error v 'file-already-exists newname)
(delete-file newname)))
- (tramp-flush-file-property v (file-name-directory localname))
- (tramp-flush-file-property v localname)
+ (tramp-flush-file-properties v (file-name-directory localname))
+ (tramp-flush-file-properties v localname)
(copy-file
filename newname 'ok-if-already-exists 'keep-time
'preserve-uid-gid 'preserve-permissions)))
@@ -2932,10 +3033,10 @@ User is always nil."
(while temp
(setq item (directory-file-name (pop temp)))
- (when (or (null match) (string-match match item))
+ (when (or (null match) (string-match-p match item))
(push (if full (concat directory item) item)
result)))
- (if nosort result (sort result 'string<)))))
+ (if nosort result (sort result #'string<)))))
(defun tramp-handle-directory-files-and-attributes
(directory &optional full match nosort id-format)
@@ -2950,22 +3051,51 @@ User is always nil."
"Like `dired-uncache' for Tramp files."
(with-parsed-tramp-file-name
(if (file-directory-p dir) dir (file-name-directory dir)) nil
- (tramp-flush-directory-property v localname)))
+ (tramp-flush-directory-properties v localname)))
+
+(defun tramp-handle-expand-file-name (name &optional dir)
+ "Like `expand-file-name' for Tramp files."
+ ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
+ (setq dir (or dir default-directory "/"))
+ ;; Handle empty NAME.
+ (when (zerop (length name)) (setq name "."))
+ ;; Unless NAME is absolute, concat DIR and NAME.
+ (unless (file-name-absolute-p name)
+ (setq name (concat (file-name-as-directory dir) name)))
+ ;; If NAME is not a Tramp file, run the real handler.
+ (if (not (tramp-tramp-file-p name))
+ (tramp-run-real-handler #'expand-file-name (list name nil))
+ ;; Dissect NAME.
+ (with-parsed-tramp-file-name name nil
+ (unless (tramp-run-real-handler #'file-name-absolute-p (list localname))
+ (setq localname (concat "/" localname)))
+ ;; Do normal `expand-file-name' (this does "/./" and "/../").
+ ;; `default-directory' is bound, because on Windows there would
+ ;; be problems with UNC shares or Cygwin mounts.
+ (let ((default-directory (tramp-compat-temporary-file-directory)))
+ (tramp-make-tramp-file-name
+ v (tramp-drop-volume-letter
+ (tramp-run-real-handler #'expand-file-name (list localname))))))))
(defun tramp-handle-file-accessible-directory-p (filename)
"Like `file-accessible-directory-p' for Tramp files."
(and (file-directory-p filename)
(file-readable-p filename)))
+(defun tramp-handle-file-directory-p (filename)
+ "Like `file-directory-p' for Tramp files."
+ (eq (tramp-compat-file-attribute-type
+ (file-attributes (file-truename filename)))
+ t))
+
(defun tramp-handle-file-equal-p (filename1 filename2)
"Like `file-equalp-p' for Tramp files."
;; Native `file-equalp-p' calls `file-truename', which requires a
;; remote connection. This can be avoided, if FILENAME1 and
;; FILENAME2 are not located on the same remote host.
- (when (string-equal
- (file-remote-p (expand-file-name filename1))
- (file-remote-p (expand-file-name filename2)))
- (tramp-run-real-handler 'file-equal-p (list filename1 filename2))))
+ (when (tramp-equal-remote
+ (expand-file-name filename1) (expand-file-name filename2))
+ (tramp-run-real-handler #'file-equal-p (list filename1 filename2))))
(defun tramp-handle-file-exists-p (filename)
"Like `file-exists-p' for Tramp files."
@@ -2976,10 +3106,20 @@ User is always nil."
;; Native `file-in-directory-p' calls `file-truename', which
;; requires a remote connection. This can be avoided, if FILENAME
;; and DIRECTORY are not located on the same remote host.
- (when (string-equal
- (file-remote-p (expand-file-name filename))
- (file-remote-p (expand-file-name directory)))
- (tramp-run-real-handler 'file-in-directory-p (list filename directory))))
+ (when (tramp-equal-remote
+ (expand-file-name filename) (expand-file-name directory))
+ (tramp-run-real-handler #'file-in-directory-p (list filename directory))))
+
+(defun tramp-handle-file-local-copy (filename)
+ "Like `file-local-copy' for Tramp files."
+ (with-parsed-tramp-file-name filename nil
+ (unless (file-exists-p filename)
+ (tramp-error
+ v tramp-file-missing
+ "Cannot make local copy of non-existing file `%s'" filename))
+ (let ((tmpfile (tramp-compat-make-temp-file filename)))
+ (copy-file filename tmpfile 'ok-if-already-exists 'keep-time)
+ tmpfile)))
(defun tramp-handle-file-modes (filename)
"Like `file-modes' for Tramp files."
@@ -2997,17 +3137,11 @@ User is always nil."
;; Run the command on the localname portion only unless we are in
;; completion mode.
(tramp-make-tramp-file-name
- (tramp-file-name-method v)
- (tramp-file-name-user v)
- (tramp-file-name-domain v)
- (tramp-file-name-host v)
- (tramp-file-name-port v)
- (if (and (zerop (length (tramp-file-name-localname v)))
- (not (tramp-connectable-p file)))
- ""
- (tramp-run-real-handler
- 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))
- (tramp-file-name-hop v))))
+ v (or (and (zerop (length (tramp-file-name-localname v)))
+ (not (tramp-connectable-p file)))
+ (tramp-run-real-handler
+ #'file-name-as-directory
+ (list (tramp-file-name-localname v)))))))
(defun tramp-handle-file-name-case-insensitive-p (filename)
"Like `file-name-case-insensitive-p' for Tramp files."
@@ -3034,8 +3168,8 @@ User is always nil."
;; Check, whether we find an existing file with
;; lower case letters. This avoids us to create a
;; temporary file.
- (while (and (string-match
- "[a-z]" (file-remote-p candidate 'localname))
+ (while (and (string-match-p
+ "[a-z]" (tramp-compat-file-local-name candidate))
(not (file-exists-p candidate)))
(setq candidate
(directory-file-name
@@ -3045,8 +3179,8 @@ User is always nil."
;; to Emacs 26+ like `file-name-case-insensitive-p',
;; so there is no compatibility problem calling it.
(unless
- (string-match
- "[a-z]" (file-remote-p candidate 'localname))
+ (string-match-p
+ "[a-z]" (tramp-compat-file-local-name candidate))
(setq tmpfile
(let ((default-directory
(file-name-directory filename)))
@@ -3059,27 +3193,23 @@ User is always nil."
(file-exists-p
(concat
(file-remote-p candidate)
- (upcase (file-remote-p candidate 'localname))))
+ (upcase (tramp-compat-file-local-name candidate))))
;; Cleanup.
(when tmpfile (delete-file tmpfile)))))))))))
(defun tramp-handle-file-name-completion
(filename directory &optional predicate)
"Like `file-name-completion' for Tramp files."
- (unless (tramp-tramp-file-p directory)
- (error
- "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
- directory))
(let (hits-ignored-extensions)
(or
(try-completion
filename (file-name-all-completions filename directory)
(lambda (x)
- (when (funcall (or predicate 'identity) (expand-file-name x directory))
+ (when (funcall (or predicate #'identity) (expand-file-name x directory))
(not
(and
completion-ignored-extensions
- (string-match
+ (string-match-p
(concat (regexp-opt completion-ignored-extensions 'paren) "$") x)
;; We remember the hit.
(push x hits-ignored-extensions))))))
@@ -3090,24 +3220,19 @@ User is always nil."
"Like `file-name-directory' but aware of Tramp files."
;; Everything except the last filename thing is the directory. We
;; cannot apply `with-parsed-tramp-file-name', because this expands
- ;; the remote file name parts. This is a problem when we are in
- ;; file name completion.
+ ;; the remote file name parts.
(let ((v (tramp-dissect-file-name file t)))
- ;; Run the command on the localname portion only.
+ ;; Run the command on the localname portion only. If this returns
+ ;; nil, mark also the localname part of `v' as nil.
(tramp-make-tramp-file-name
- (tramp-file-name-method v)
- (tramp-file-name-user v)
- (tramp-file-name-domain v)
- (tramp-file-name-host v)
- (tramp-file-name-port v)
- (tramp-run-real-handler
- 'file-name-directory (list (or (tramp-file-name-localname v) "")))
- (tramp-file-name-hop v))))
+ v (or (tramp-run-real-handler
+ #'file-name-directory (list (tramp-file-name-localname v)))
+ 'noloc))))
(defun tramp-handle-file-name-nondirectory (file)
"Like `file-name-nondirectory' but aware of Tramp files."
(with-parsed-tramp-file-name file nil
- (tramp-run-real-handler 'file-name-nondirectory (list localname))))
+ (tramp-run-real-handler #'file-name-nondirectory (list localname))))
(defun tramp-handle-file-newer-than-file-p (file1 file2)
"Like `file-newer-than-file-p' for Tramp files."
@@ -3141,13 +3266,13 @@ User is always nil."
(and (or (not connected) c)
(cond
((eq identification 'method) method)
- ;; Domain and port are appended.
+ ;; Domain and port are appended to user and host,
+ ;; respectively.
((eq identification 'user) (tramp-file-name-user-domain v))
((eq identification 'host) (tramp-file-name-host-port v))
((eq identification 'localname) localname)
((eq identification 'hop) hop)
- (t (tramp-make-tramp-file-name
- method user domain host port "" hop)))))))))
+ (t (tramp-make-tramp-file-name v 'noloc)))))))))
(defun tramp-handle-file-selinux-context (_filename)
"Like `file-selinux-context' for Tramp files."
@@ -3164,7 +3289,7 @@ User is always nil."
;; Preserve trailing "/".
(funcall
(if (string-equal (file-name-nondirectory filename) "")
- 'file-name-as-directory 'identity)
+ #'file-name-as-directory #'identity)
(let ((result (expand-file-name filename))
(numchase 0)
;; Don't make the following value larger than necessary.
@@ -3174,30 +3299,44 @@ User is always nil."
(numchase-limit 20)
symlink-target)
(with-parsed-tramp-file-name result v1
- (with-tramp-file-property v1 v1-localname "file-truename"
- (while (and (setq symlink-target (file-symlink-p result))
- (< numchase numchase-limit))
- (setq numchase (1+ numchase)
- result
- (with-parsed-tramp-file-name (expand-file-name result) v2
- (tramp-make-tramp-file-name
- v2-method v2-user v2-domain v2-host v2-port
- (funcall
- (if (tramp-compat-file-name-quoted-p v2-localname)
- 'tramp-compat-file-name-quote 'identity)
-
- (if (stringp symlink-target)
- (if (file-remote-p symlink-target)
- (let (file-name-handler-alist)
- (tramp-compat-file-name-quote symlink-target))
- (expand-file-name
- symlink-target (file-name-directory v2-localname)))
- v2-localname)))))
- (when (>= numchase numchase-limit)
- (tramp-error
- v1 'file-error
- "Maximum number (%d) of symlinks exceeded" numchase-limit)))
- (directory-file-name result))))))
+ ;; We cache only the localname.
+ (tramp-make-tramp-file-name
+ v1
+ (with-tramp-file-property v1 v1-localname "file-truename"
+ (while (and (setq symlink-target (file-symlink-p result))
+ (< numchase numchase-limit))
+ (setq numchase (1+ numchase)
+ result
+ (with-parsed-tramp-file-name (expand-file-name result) v2
+ (tramp-make-tramp-file-name
+ v2
+ (funcall
+ (if (tramp-compat-file-name-quoted-p v2-localname)
+ #'tramp-compat-file-name-quote #'identity)
+
+ (if (stringp symlink-target)
+ (if (file-remote-p symlink-target)
+ (let (file-name-handler-alist)
+ (tramp-compat-file-name-quote symlink-target))
+ (expand-file-name
+ symlink-target (file-name-directory v2-localname)))
+ v2-localname))
+ 'nohop)))
+ (when (>= numchase numchase-limit)
+ (tramp-error
+ v1 'file-error
+ "Maximum number (%d) of symlinks exceeded" numchase-limit)))
+ (tramp-compat-file-local-name (directory-file-name result))))))))
+
+(defun tramp-handle-file-writable-p (filename)
+ "Like `file-writable-p' for Tramp files."
+ (with-parsed-tramp-file-name filename nil
+ (with-tramp-file-property v localname "file-writable-p"
+ (if (file-exists-p filename)
+ (tramp-check-cached-permissions v ?w)
+ ;; If file doesn't exist, check if directory is writable.
+ (and (file-directory-p (file-name-directory filename))
+ (file-writable-p (file-name-directory filename)))))))
(defun tramp-handle-find-backup-file-name (filename)
"Like `find-backup-file-name' for Tramp files."
@@ -3211,12 +3350,11 @@ User is always nil."
(if (and (stringp (cdr x))
(file-name-absolute-p (cdr x))
(not (tramp-tramp-file-p (cdr x))))
- (tramp-make-tramp-file-name
- method user domain host port (cdr x) hop)
+ (tramp-make-tramp-file-name v (cdr x))
(cdr x))))
tramp-backup-directory-alist)
backup-directory-alist)))
- (tramp-run-real-handler 'find-backup-file-name (list filename)))))
+ (tramp-run-real-handler #'find-backup-file-name (list filename)))))
(defun tramp-handle-insert-directory
(filename switches &optional wildcard full-directory-p)
@@ -3226,16 +3364,20 @@ User is always nil."
(when (and (zerop (length (file-name-nondirectory filename)))
(not full-directory-p))
(setq switches (concat switches "F")))
+ ;; Check, whether directory is accessible.
+ (unless wildcard
+ (access-file filename "Reading directory"))
(with-parsed-tramp-file-name (expand-file-name filename) nil
(with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
- (require 'ls-lisp)
(let (ls-lisp-use-insert-directory-program start)
+ ;; Silence byte compiler.
+ ls-lisp-use-insert-directory-program
(tramp-run-real-handler
- 'insert-directory
+ #'insert-directory
(list filename switches wildcard full-directory-p))
;; `ls-lisp' always returns full listings. We must remove
;; superfluous parts.
- (unless (string-match "l" switches)
+ (unless (string-match-p "l" switches)
(save-excursion
(goto-char (point-min))
(while (setq start
@@ -3245,7 +3387,7 @@ User is always nil."
start
(or (text-property-any start (point-at-eol) 'dired-filename t)
(point-at-eol)))
- (if (= (point-at-bol) (point-at-eol))
+ (if (= (point-at-bol) (point-at-eol))
;; Empty line.
(delete-region (point) (progn (forward-line) (point)))
(forward-line)))))))))
@@ -3273,7 +3415,7 @@ User is always nil."
;; run directly.
(setq result
(tramp-run-real-handler
- 'insert-file-contents
+ #'insert-file-contents
(list localname visit beg end replace)))
;; When we shall insert only a part of the file, we
@@ -3317,7 +3459,7 @@ User is always nil."
((stringp remote-copy)
(file-local-copy
(tramp-make-tramp-file-name
- method user domain host port remote-copy)))
+ v remote-copy 'nohop)))
((stringp tramp-temp-buffer-file-name)
(copy-file
filename tramp-temp-buffer-file-name 'ok)
@@ -3327,7 +3469,7 @@ User is always nil."
;; When the file is not readable for the owner, it
;; cannot be inserted, even if it is readable for the
;; group or for everybody.
- (set-file-modes local-copy (string-to-number "0600" 8))
+ (set-file-modes local-copy #o0600)
(when (and (null remote-copy)
(tramp-get-method-parameter
@@ -3361,9 +3503,7 @@ User is always nil."
(or remote-copy (null tramp-temp-buffer-file-name)))
(delete-file local-copy))
(when (stringp remote-copy)
- (delete-file
- (tramp-make-tramp-file-name
- method user domain host port remote-copy)))))
+ (delete-file (tramp-make-tramp-file-name v remote-copy 'nohop)))))
;; Result.
(list (expand-file-name filename)
@@ -3381,14 +3521,13 @@ User is always nil."
;; The first condition is always true for absolute file names.
;; Included for safety's sake.
(unless (or (file-name-directory file)
- (string-match "\\.elc?\\'" file))
+ (string-match-p "\\.elc?\\'" file))
(tramp-error
v 'file-error
"File `%s' does not include a `.el' or `.elc' suffix" file)))
- (unless noerror
- (when (not (file-exists-p file))
- (tramp-error
- v tramp-file-missing "Cannot load nonexistent file `%s'" file)))
+ (unless (or noerror (file-exists-p file))
+ (tramp-error
+ v tramp-file-missing "Cannot load nonexistent file `%s'" file))
(if (not (file-exists-p file))
nil
(let ((tramp-message-show-message (not nomessage)))
@@ -3411,23 +3550,13 @@ support symbolic links."
;; This is needed prior Emacs 26.1, where TARGET has also be
;; checked for a file name handler.
(tramp-run-real-handler
- 'make-symbolic-link (list target linkname ok-if-already-exists))))
+ #'make-symbolic-link (list target linkname ok-if-already-exists))))
(defun tramp-handle-shell-command
(command &optional output-buffer error-buffer)
"Like `shell-command' for Tramp files."
- (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
- ;; We cannot use `shell-file-name' and `shell-command-switch',
- ;; they are variables of the local host.
- (args (append
- (cons
- (tramp-get-method-parameter
- (tramp-dissect-file-name default-directory)
- 'tramp-remote-shell)
- (tramp-get-method-parameter
- (tramp-dissect-file-name default-directory)
- 'tramp-remote-shell-args))
- (list (substring command 0 asynchronous))))
+ (let* ((asynchronous (string-match-p "[ \t]*&[ \t]*\\'" command))
+ (command (substring command 0 asynchronous))
current-buffer-p
(output-buffer
(cond
@@ -3444,19 +3573,48 @@ support symbolic links."
(cond
((bufferp error-buffer) error-buffer)
((stringp error-buffer) (get-buffer-create error-buffer))))
- (buffer
- (if (and (not asynchronous) error-buffer)
- (with-parsed-tramp-file-name default-directory nil
- (list output-buffer (tramp-make-tramp-temp-file v)))
- output-buffer))
- (p (get-buffer-process output-buffer)))
-
- ;; Check whether there is another process running. Tramp does not
- ;; support 2 (asynchronous) processes in parallel.
+ (bname (buffer-name output-buffer))
+ (p (get-buffer-process output-buffer))
+ buffer)
+
+ ;; The following code is taken from `shell-command', slightly
+ ;; adapted. Shouldn't it be factored out?
(when p
- (if (yes-or-no-p "A command is running. Kill it? ")
- (ignore-errors (kill-process p))
- (tramp-compat-user-error p "Shell command in progress")))
+ (cond
+ ((eq async-shell-command-buffer 'confirm-kill-process)
+ ;; If will kill a process, query first.
+ (if (yes-or-no-p
+ "A command is running in the default buffer. Kill it? ")
+ (kill-process p)
+ (tramp-user-error p "Shell command in progress")))
+ ((eq async-shell-command-buffer 'confirm-new-buffer)
+ ;; If will create a new buffer, query first.
+ (if (yes-or-no-p
+ "A command is running in the default buffer. Use a new buffer? ")
+ (setq output-buffer (generate-new-buffer bname))
+ (tramp-user-error p "Shell command in progress")))
+ ((eq async-shell-command-buffer 'new-buffer)
+ ;; It will create a new buffer.
+ (setq output-buffer (generate-new-buffer bname)))
+ ((eq async-shell-command-buffer 'confirm-rename-buffer)
+ ;; If will rename the buffer, query first.
+ (if (yes-or-no-p
+ "A command is running in the default buffer. Rename it? ")
+ (progn
+ (with-current-buffer output-buffer
+ (rename-uniquely))
+ (setq output-buffer (get-buffer-create bname)))
+ (tramp-user-error p "Shell command in progress")))
+ ((eq async-shell-command-buffer 'rename-buffer)
+ ;; It will rename the buffer.
+ (with-current-buffer output-buffer
+ (rename-uniquely))
+ (setq output-buffer (get-buffer-create bname)))))
+
+ (setq buffer (if (and (not asynchronous) error-buffer)
+ (with-parsed-tramp-file-name default-directory nil
+ (list output-buffer (tramp-make-tramp-temp-file v)))
+ output-buffer))
(if current-buffer-p
(progn
@@ -3467,20 +3625,29 @@ support symbolic links."
(erase-buffer)))
(if (and (not current-buffer-p) (integerp asynchronous))
- (prog1
- ;; Run the process.
- (setq p (apply 'start-file-process "*Async Shell*" buffer args))
- ;; Display output.
- (with-current-buffer output-buffer
- (display-buffer output-buffer '(nil (allow-no-window . t)))
- (setq mode-line-process '(":%s"))
- (shell-mode)
- (set-process-sentinel p 'shell-command-sentinel)
- (set-process-filter p 'comint-output-filter)))
+ (let ((tramp-remote-process-environment
+ ;; `async-shell-command-width' has been introduced with
+ ;; Emacs 27.1.
+ (if (natnump (bound-and-true-p async-shell-command-width))
+ (cons (format "COLUMNS=%d"
+ (bound-and-true-p async-shell-command-width))
+ tramp-remote-process-environment)
+ tramp-remote-process-environment)))
+ (prog1
+ ;; Run the process.
+ (setq p (start-file-process-shell-command
+ (buffer-name output-buffer) buffer command))
+ ;; Display output.
+ (with-current-buffer output-buffer
+ (display-buffer output-buffer '(nil (allow-no-window . t)))
+ (setq mode-line-process '(":%s"))
+ (shell-mode)
+ (set-process-sentinel p #'shell-command-sentinel)
+ (set-process-filter p #'comint-output-filter))))
(prog1
;; Run the process.
- (apply 'process-file (car args) nil buffer nil (cdr args))
+ (process-file-shell-command command nil buffer nil)
;; Insert error messages if they were separated.
(when (listp buffer)
(with-current-buffer error-buffer
@@ -3498,6 +3665,17 @@ support symbolic links."
(when (with-current-buffer output-buffer (> (point-max) (point-min)))
(display-message-or-buffer output-buffer)))))))
+(defun tramp-handle-start-file-process (name buffer program &rest args)
+ "Like `start-file-process' for Tramp files."
+ ;; `make-process' knows the `:file-error' argument since Emacs 27.1.
+ (tramp-file-name-handler
+ 'make-process
+ :name name
+ :buffer buffer
+ :command (and program (cons program args))
+ :noquery nil
+ :file-handler t))
+
(defun tramp-handle-substitute-in-file-name (filename)
"Like `substitute-in-file-name' for Tramp files.
\"//\" and \"/~\" substitute only in the local filename part."
@@ -3507,17 +3685,34 @@ support symbolic links."
;; First, we must replace environment variables.
(setq filename (tramp-replace-environment-variables filename))
(with-parsed-tramp-file-name filename nil
- ;; Ignore in LOCALNAME everything before "//" or "/~".
- (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
- (setq filename
- (concat (file-remote-p filename)
- (replace-match "\\1" nil nil localname)))
- ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
- (when (string-match "~$" filename)
- (setq filename (concat filename "/"))))
- ;; We do not want to replace environment variables, again.
+ ;; We do not want to replace environment variables, again. "//"
+ ;; has a special meaning at the beginning of a file name on
+ ;; Cygwin and MS-Windows, we must remove it.
(let (process-environment)
- (tramp-run-real-handler 'substitute-in-file-name (list filename))))))
+ ;; Ignore in LOCALNAME everything before "//" or "/~".
+ (when (stringp localname)
+ (if (string-match "//\\(/\\|~\\)" localname)
+ (setq filename
+ (replace-regexp-in-string
+ "\\`/+" "/" (substitute-in-file-name localname)))
+ (setq filename
+ (concat (file-remote-p filename)
+ (replace-regexp-in-string
+ "\\`/+" "/"
+ ;; We must disable cygwin-mount file name
+ ;; handlers and alike.
+ (tramp-run-real-handler
+ #'substitute-in-file-name (list localname))))))))
+ ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
+ (if (and (stringp localname) (string-equal "~" localname))
+ (concat filename "/")
+ filename))))
+
+(defconst tramp-time-dont-know '(0 0 0 1000)
+ "An invalid time value, used as \"Don’t know\" value.")
+
+(defconst tramp-time-doesnt-exist '(-1 65535)
+ "An invalid time value, used as \"Doesn’t exist\" value.")
(defun tramp-handle-set-visited-file-modtime (&optional time-list)
"Like `set-visited-file-modtime' for Tramp files."
@@ -3526,14 +3721,12 @@ support symbolic links."
(buffer-name)))
(unless time-list
(let ((remote-file-name-inhibit-cache t))
- ;; '(-1 65535) means file doesn't exists yet.
(setq time-list
(or (tramp-compat-file-attribute-modification-time
(file-attributes (buffer-file-name)))
- '(-1 65535)))))
- ;; We use '(0 0) as a don't-know value.
- (unless (equal time-list '(0 0))
- (tramp-run-real-handler 'set-visited-file-modtime (list time-list))))
+ tramp-time-doesnt-exist))))
+ (unless (tramp-compat-time-equal-p time-list tramp-time-dont-know)
+ (tramp-run-real-handler #'set-visited-file-modtime (list time-list))))
(defun tramp-handle-verify-visited-file-modtime (&optional buf)
"Like `verify-visited-file-modtime' for Tramp files.
@@ -3551,34 +3744,81 @@ of."
(eq (visited-file-modtime) 0)
(not (file-remote-p f nil 'connected)))
t
- (with-parsed-tramp-file-name f nil
- (let* ((remote-file-name-inhibit-cache t)
- (attr (file-attributes f))
- (modtime (tramp-compat-file-attribute-modification-time attr))
- (mt (visited-file-modtime)))
-
- (cond
- ;; File exists, and has a known modtime.
- ((and attr (not (equal modtime '(0 0))))
- (< (abs (tramp-time-diff
- modtime
- ;; For compatibility, deal with both the old
- ;; (HIGH . LOW) and the new (HIGH LOW) return
- ;; values of `visited-file-modtime'.
- (if (atom (cdr mt))
- (list (car mt) (cdr mt))
- mt)))
- 2))
- ;; Modtime has the don't know value.
- (attr t)
- ;; If file does not exist, say it is not modified if and
- ;; only if that agrees with the buffer's record.
- (t (equal mt '(-1 65535))))))))))
+ (let* ((remote-file-name-inhibit-cache t)
+ (attr (file-attributes f))
+ (modtime (tramp-compat-file-attribute-modification-time attr))
+ (mt (visited-file-modtime)))
+ (cond
+ ;; File exists, and has a known modtime.
+ ((and attr
+ (not (tramp-compat-time-equal-p modtime tramp-time-dont-know)))
+ (< (abs (tramp-time-diff modtime mt)) 2))
+ ;; Modtime has the don't know value.
+ (attr t)
+ ;; If file does not exist, say it is not modified if and
+ ;; only if that agrees with the buffer's record.
+ (t (tramp-compat-time-equal-p mt tramp-time-doesnt-exist))))))))
+
+(defun tramp-handle-write-region
+ (start end filename &optional append visit lockname mustbenew)
+ "Like `write-region' for Tramp files."
+ (setq filename (expand-file-name filename))
+ (with-parsed-tramp-file-name filename nil
+ (when (and mustbenew (file-exists-p filename)
+ (or (eq mustbenew 'excl)
+ (not
+ (y-or-n-p
+ (format "File %s exists; overwrite anyway? " filename)))))
+ (tramp-error v 'file-already-exists filename))
+
+ (let ((tmpfile (tramp-compat-make-temp-file filename)))
+ (when (and append (file-exists-p filename))
+ (copy-file filename tmpfile 'ok))
+ ;; We say `no-message' here because we don't want the visited file
+ ;; modtime data to be clobbered from the temp file. We call
+ ;; `set-visited-file-modtime' ourselves later on.
+ (tramp-run-real-handler
+ #'write-region (list start end tmpfile append 'no-message lockname))
+ (condition-case nil
+ (rename-file tmpfile filename 'ok-if-already-exists)
+ (error
+ (delete-file tmpfile)
+ (tramp-error
+ v 'file-error "Couldn't write region to `%s'" filename))))
+
+ (tramp-flush-file-properties v (file-name-directory localname))
+ (tramp-flush-file-properties v localname)
+
+ ;; Set file modification time.
+ (when (or (eq visit t) (stringp visit))
+ (set-visited-file-modtime
+ (tramp-compat-file-attribute-modification-time
+ (file-attributes filename))))
+
+ ;; The end.
+ (when (and (null noninteractive)
+ (or (eq visit t) (null visit) (stringp visit)))
+ (tramp-message v 0 "Wrote %s" filename))
+ (run-hooks 'tramp-handle-write-region-hook)))
+
+;; This is used in tramp-sh.el and tramp-sudoedit.el.
+(defconst tramp-stat-marker "/////"
+ "Marker in stat commands for file attributes.")
+
+(defconst tramp-stat-quoted-marker "\\/\\/\\/\\/\\/"
+ "Quoted marker in stat commands for file attributes.")
+
+;; This is used in tramp-gvfs.el and tramp-sh.el.
+(defconst tramp-gio-events
+ '("attribute-changed" "changed" "changes-done-hint"
+ "created" "deleted" "moved" "pre-unmount" "unmounted")
+ "List of events \"gio monitor\" could send.")
+
+;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
+;; their own one.
(defun tramp-handle-file-notify-add-watch (filename _flags _callback)
"Like `file-notify-add-watch' for Tramp files."
- ;; This is the default handler. tramp-gvfs.el and tramp-sh.el have
- ;; their own one.
(setq filename (expand-file-name filename))
(with-parsed-tramp-file-name filename nil
(tramp-error
@@ -3589,6 +3829,8 @@ of."
;; The descriptor must be a process object.
(unless (processp proc)
(tramp-error proc 'file-notify-error "Not a valid descriptor %S" proc))
+ ;; There might be pending output.
+ (while (tramp-accept-process-output proc 0))
(tramp-message proc 6 "Kill %S" proc)
(delete-process proc))
@@ -3602,6 +3844,12 @@ of."
(concat (file-remote-p default-directory)
(process-get proc 'watch-name))))))
+(defun tramp-file-notify-process-sentinel (proc event)
+ "Call `file-notify-rm-watch'."
+ (unless (process-live-p proc)
+ (tramp-message proc 5 "Sentinel called: `%S' `%s'" proc event)
+ (tramp-compat-funcall 'file-notify-rm-watch proc)))
+
;;; Functions for establishing connection:
;; The following functions are actions to be taken when seeing certain
@@ -3610,17 +3858,17 @@ of."
(defun tramp-action-login (_proc vec)
"Send the login name."
- (when (not (stringp tramp-current-user))
- (setq tramp-current-user
- (with-tramp-connection-property vec "login-as"
- (save-window-excursion
- (let ((enable-recursive-minibuffers t))
- (pop-to-buffer (tramp-get-connection-buffer vec))
- (read-string (match-string 0)))))))
- (with-current-buffer (tramp-get-connection-buffer vec)
- (tramp-message vec 6 "\n%s" (buffer-string)))
- (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
- (tramp-send-string vec (concat tramp-current-user tramp-local-end-of-line)))
+ (let ((user (or (tramp-file-name-user vec)
+ (with-tramp-connection-property vec "login-as"
+ (save-window-excursion
+ (let ((enable-recursive-minibuffers t))
+ (pop-to-buffer (tramp-get-connection-buffer vec))
+ (read-string (match-string 0))))))))
+ (with-current-buffer (tramp-get-connection-buffer vec)
+ (tramp-message vec 6 "\n%s" (buffer-string)))
+ (tramp-message vec 3 "Sending login name `%s'" user)
+ (tramp-send-string vec (concat user tramp-local-end-of-line)))
+ t)
(defun tramp-action-password (proc vec)
"Query the user for a password."
@@ -3636,11 +3884,12 @@ of."
(tramp-check-for-regexp proc tramp-password-prompt-regexp)
(tramp-message vec 3 "Sending %s" (match-string 1))
;; We don't call `tramp-send-string' in order to hide the
- ;; password from the debug buffer.
+ ;; password from the debug buffer and the traces.
(process-send-string
proc (concat (tramp-read-passwd proc) tramp-local-end-of-line))
;; Hide password prompt.
- (narrow-to-region (point-max) (point-max)))))
+ (narrow-to-region (point-max) (point-max))))
+ t)
(defun tramp-action-succeed (_proc _vec)
"Signal success in finding shell prompt."
@@ -3657,13 +3906,14 @@ Send \"yes\" to remote process on confirmation, abort otherwise.
See also `tramp-action-yn'."
(save-window-excursion
(let ((enable-recursive-minibuffers t))
- (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
+ (pop-to-buffer (tramp-get-connection-buffer vec))
(unless (yes-or-no-p (match-string 0))
(kill-process proc)
(throw 'tramp-action 'permission-denied))
(with-current-buffer (tramp-get-connection-buffer vec)
(tramp-message vec 6 "\n%s" (buffer-string)))
- (tramp-send-string vec (concat "yes" tramp-local-end-of-line)))))
+ (tramp-send-string vec (concat "yes" tramp-local-end-of-line))))
+ t)
(defun tramp-action-yn (proc vec)
"Ask the user for confirmation using `y-or-n-p'.
@@ -3671,13 +3921,14 @@ Send \"y\" to remote process on confirmation, abort otherwise.
See also `tramp-action-yesno'."
(save-window-excursion
(let ((enable-recursive-minibuffers t))
- (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
+ (pop-to-buffer (tramp-get-connection-buffer vec))
(unless (y-or-n-p (match-string 0))
(kill-process proc)
(throw 'tramp-action 'permission-denied))
(with-current-buffer (tramp-get-connection-buffer vec)
(tramp-message vec 6 "\n%s" (buffer-string)))
- (tramp-send-string vec (concat "y" tramp-local-end-of-line)))))
+ (tramp-send-string vec (concat "y" tramp-local-end-of-line))))
+ t)
(defun tramp-action-terminal (_proc vec)
"Tell the remote host which terminal type to use.
@@ -3685,7 +3936,8 @@ The terminal type can be configured with `tramp-terminal-type'."
(tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
(with-current-buffer (tramp-get-connection-buffer vec)
(tramp-message vec 6 "\n%s" (buffer-string)))
- (tramp-send-string vec (concat tramp-terminal-type tramp-local-end-of-line)))
+ (tramp-send-string vec (concat tramp-terminal-type tramp-local-end-of-line))
+ t)
(defun tramp-action-process-alive (proc _vec)
"Check, whether a process has finished."
@@ -3695,14 +3947,14 @@ The terminal type can be configured with `tramp-terminal-type'."
(defun tramp-action-out-of-band (proc vec)
"Check, whether an out-of-band copy has finished."
;; There might be pending output for the exit status.
- (tramp-accept-process-output proc 0.1)
+ (while (tramp-accept-process-output proc 0))
(cond ((and (not (process-live-p proc))
(zerop (process-exit-status proc)))
(tramp-message vec 3 "Process has finished.")
(throw 'tramp-action 'ok))
((or (and (memq (process-status proc) '(stop exit))
(not (zerop (process-exit-status proc))))
- (memq (process-status proc) '(signal)))
+ (eq (process-status proc) 'signal))
;; `scp' could have copied correctly, but set modes could have failed.
;; This can be ignored.
(with-current-buffer (process-buffer proc)
@@ -3719,13 +3971,14 @@ The terminal type can be configured with `tramp-terminal-type'."
;;; Functions for processing the actions:
(defun tramp-process-one-action (proc vec actions)
- "Wait for output from the shell and perform one action."
+ "Wait for output from the shell and perform one action.
+See `tramp-process-actions' for the format of ACTIONS."
(let ((case-fold-search t)
found todo item pattern action)
(while (not found)
;; Reread output once all actions have been performed.
;; Obviously, the output was not complete.
- (tramp-accept-process-output proc 1)
+ (while (tramp-accept-process-output proc 0))
(setq todo actions)
(while todo
(setq item (pop todo))
@@ -3742,14 +3995,32 @@ The terminal type can be configured with `tramp-terminal-type'."
"Perform ACTIONS until success or TIMEOUT.
PROC and VEC indicate the remote connection to be used. POS, if
set, is the starting point of the region to be deleted in the
-connection buffer."
+connection buffer.
+
+ACTIONS is a list of (PATTERN ACTION). The PATTERN should be a
+symbol, a variable. The value of this variable gives the regular
+expression to search for. Note that the regexp must match at the
+end of the buffer, \"\\'\" is implicitly appended to it.
+
+The ACTION should also be a symbol, but a function. When the
+corresponding PATTERN matches, the ACTION function is called.
+
+An ACTION function has two arguments (PROC VEC). If it returns
+nil, nothing has been done, and the next action shall be called.
+A non-nil return value indicates that the process output has been
+consumed, and new output shall be retrieved, before starting to
+process all ACTIONs, again. The same happens after calling the
+last ACTION.
+
+If an action determines, that all processing has been done (e.g.,
+because the shell prompt has been detected), it shall throw a
+result. The symbol `ok' means that all ACTIONs have been
+performed successfully. Any other value means an error."
;; Enable `auth-source', unless "emacs -Q" has been called. We must
- ;; use `tramp-current-*' variables in case we have several hops.
+ ;; use the "password-vector" property in case we have several hops.
(tramp-set-connection-property
- (make-tramp-file-name
- :method tramp-current-method :user tramp-current-user
- :domain tramp-current-domain :host tramp-current-host
- :port tramp-current-port)
+ (tramp-get-connection-property
+ proc "password-vector" (process-get proc 'vector))
"first-password-request" tramp-cache-read-persistent-data)
(save-restriction
(with-tramp-progress-reporter
@@ -3768,7 +4039,11 @@ connection buffer."
(with-current-buffer (tramp-get-connection-buffer vec)
(widen)
(tramp-message vec 6 "\n%s" (buffer-string)))
- (unless (eq exit 'ok)
+ (if (eq exit 'ok)
+ (ignore-errors
+ (and (functionp tramp-password-save-function)
+ (funcall tramp-password-save-function)))
+ ;; Not successful.
(tramp-clear-passwd vec)
(delete-process proc)
(tramp-error-with-buffer
@@ -3781,9 +4056,10 @@ connection buffer."
(tramp-get-connection-buffer vec)))
((eq exit 'process-died)
(substitute-command-keys
- (concat
- "Tramp failed to connect. If this happens repeatedly, try\n"
- " `\\[tramp-cleanup-this-connection]'")))
+ (eval-when-compile
+ (concat
+ "Tramp failed to connect. If this happens repeatedly, try\n"
+ " `\\[tramp-cleanup-this-connection]'"))))
((eq exit 'timeout)
(format-message
"Timeout reached, see buffer `%s' for details"
@@ -3791,28 +4067,26 @@ connection buffer."
(t "Login failed")))))
(when (numberp pos)
(with-current-buffer (tramp-get-connection-buffer vec)
- (let (buffer-read-only) (delete-region pos (point))))))))
+ (let ((inhibit-read-only t)) (delete-region pos (point))))))))
;;; Utility functions:
-(defun tramp-accept-process-output (proc timeout)
+(defun tramp-accept-process-output (proc &optional timeout)
"Like `accept-process-output' for Tramp processes.
This is needed in order to hide `last-coding-system-used', which is set
for process communication also."
(with-current-buffer (process-buffer proc)
- (let (buffer-read-only last-coding-system-used
- ;; We do not want to run timers.
- timer-list timer-idle-list)
- ;; Under Windows XP, `accept-process-output' doesn't return
- ;; sometimes. So we add an additional timeout. JUST-THIS-ONE
- ;; is set due to Bug#12145. It is an integer, in order to avoid
- ;; running timers as well.
+ (let ((inhibit-read-only t)
+ last-coding-system-used
+ result)
+ ;; JUST-THIS-ONE is set due to Bug#12145.
(tramp-message
- proc 10 "%s %s %s\n%s"
- proc (process-status proc)
- (with-timeout (timeout)
- (accept-process-output proc timeout nil 0))
- (buffer-string)))))
+ proc 10 "%s %s %s %s\n%s"
+ proc timeout (process-status proc)
+ (with-local-quit
+ (setq result (accept-process-output proc timeout nil t)))
+ (buffer-string))
+ result)))
(defun tramp-check-for-regexp (proc regexp)
"Check, whether REGEXP is contained in process buffer of PROC.
@@ -3855,31 +4129,34 @@ Erase echoed commands if exists."
Expects the output of PROC to be sent to the current buffer. Returns
the string that matched, or nil. Waits indefinitely if TIMEOUT is
nil."
- (with-current-buffer (process-buffer proc)
- (let ((found (tramp-check-for-regexp proc regexp)))
- (cond (timeout
- (with-timeout (timeout)
- (while (not found)
- (tramp-accept-process-output proc 1)
- (unless (process-live-p proc)
- (tramp-error-with-buffer
- nil proc 'file-error "Process has died"))
- (setq found (tramp-check-for-regexp proc regexp)))))
- (t
+ (let ((found (tramp-check-for-regexp proc regexp)))
+ (cond (timeout
+ (with-timeout (timeout)
(while (not found)
- (tramp-accept-process-output proc 1)
+ (tramp-accept-process-output proc)
(unless (process-live-p proc)
(tramp-error-with-buffer
nil proc 'file-error "Process has died"))
(setq found (tramp-check-for-regexp proc regexp)))))
- (tramp-message proc 6 "\n%s" (buffer-string))
- (when (not found)
- (if timeout
- (tramp-error
- proc 'file-error "[[Regexp `%s' not found in %d secs]]"
- regexp timeout)
- (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
- found)))
+ (t
+ (while (not found)
+ (tramp-accept-process-output proc)
+ (unless (process-live-p proc)
+ (tramp-error-with-buffer
+ nil proc 'file-error "Process has died"))
+ (setq found (tramp-check-for-regexp proc regexp)))))
+ ;; The process could have timed out, for example due to session
+ ;; timeout of sudo. The process buffer does not exist any longer then.
+ (ignore-errors
+ (with-current-buffer (process-buffer proc)
+ (tramp-message proc 6 "\n%s" (buffer-string))))
+ (unless found
+ (if timeout
+ (tramp-error
+ proc 'file-error "[[Regexp `%s' not found in %d secs]]"
+ regexp timeout)
+ (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
+ found))
;; It seems that Tru64 Unix does not like it if long strings are sent
;; to it in one go. (This happens when sending the Perl
@@ -3901,26 +4178,40 @@ the remote host use line-endings as defined in the variable
(with-current-buffer (tramp-get-connection-buffer vec)
;; Clean up the buffer. We cannot call `erase-buffer' because
;; narrowing might be in effect.
- (let (buffer-read-only) (delete-region (point-min) (point-max)))
+ (let ((inhibit-read-only t)) (delete-region (point-min) (point-max)))
;; Replace "\n" by `tramp-rsh-end-of-line'.
(setq string
(mapconcat
- 'identity (split-string string "\n") tramp-rsh-end-of-line))
- (unless (or (string= string "")
+ #'identity (split-string string "\n") tramp-rsh-end-of-line))
+ (unless (or (string-empty-p string)
(string-equal (substring string -1) tramp-rsh-end-of-line))
(setq string (concat string tramp-rsh-end-of-line)))
;; Send the string.
- (if (and chunksize (not (zerop chunksize)))
- (let ((pos 0)
- (end (length string)))
- (while (< pos end)
- (tramp-message
- vec 10 "Sending chunk from %s to %s"
- pos (min (+ pos chunksize) end))
- (process-send-string
- p (substring string pos (min (+ pos chunksize) end)))
- (setq pos (+ pos chunksize))))
- (process-send-string p string)))))
+ (with-local-quit
+ (if (and chunksize (not (zerop chunksize)))
+ (let ((pos 0)
+ (end (length string)))
+ (while (< pos end)
+ (tramp-message
+ vec 10 "Sending chunk from %s to %s"
+ pos (min (+ pos chunksize) end))
+ (process-send-string
+ p (substring string pos (min (+ pos chunksize) end)))
+ (setq pos (+ pos chunksize))))
+ (process-send-string p string))))))
+
+(defun tramp-process-sentinel (proc event)
+ "Flush file caches and remove shell prompt."
+ (unless (process-live-p proc)
+ (let ((vec (process-get proc 'vector))
+ (prompt (tramp-get-connection-property proc "prompt" nil)))
+ (when vec
+ (tramp-message vec 5 "Sentinel called: `%S' `%s'" proc event)
+ (tramp-flush-connection-properties proc)
+ (tramp-flush-directory-properties vec ""))
+ (goto-char (point-max))
+ (when (and prompt (re-search-backward (regexp-quote prompt) nil t))
+ (delete-region (point) (point-max))))))
(defun tramp-get-inode (vec)
"Returns the virtual inode number.
@@ -3934,6 +4225,7 @@ If it doesn't exist, generate a new one."
(with-tramp-connection-property (tramp-get-connection-process vec) "device"
(cons -1 (setq tramp-devices (1+ tramp-devices)))))
+;; Comparision of vectors is performed by `tramp-file-name-equal-p'.
(defun tramp-equal-remote (file1 file2)
"Check, whether the remote parts of FILE1 and FILE2 are identical.
The check depends on method, user and host name of the files. If
@@ -3943,16 +4235,17 @@ account.
Example:
- (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
+ (tramp-equal-remote \"/ssh::/etc\" \"/-:<your host name>:/home\")
would yield t. On the other hand, the following check results in nil:
- (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
- (and (tramp-tramp-file-p file1)
- (tramp-tramp-file-p file2)
- (string-equal (file-remote-p file1) (file-remote-p file2))))
+ (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")
+
+If both files are local, the function returns t."
+ (or (and (null (file-remote-p file1)) (null (file-remote-p file2)))
+ (and (tramp-tramp-file-p file1) (tramp-tramp-file-p file2)
+ (string-equal (file-remote-p file1) (file-remote-p file2)))))
-;;;###tramp-autoload
(defun tramp-mode-string-to-int (mode-string)
"Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
(let* (case-fold-search
@@ -3966,53 +4259,52 @@ would yield t. On the other hand, the following check results in nil:
(other-read (aref mode-chars 7))
(other-write (aref mode-chars 8))
(other-execute-or-sticky (aref mode-chars 9)))
- (save-match-data
- (logior
- (cond
- ((char-equal owner-read ?r) (string-to-number "00400" 8))
- ((char-equal owner-read ?-) 0)
- (t (error "Second char `%c' must be one of `r-'" owner-read)))
- (cond
- ((char-equal owner-write ?w) (string-to-number "00200" 8))
- ((char-equal owner-write ?-) 0)
- (t (error "Third char `%c' must be one of `w-'" owner-write)))
- (cond
- ((char-equal owner-execute-or-setid ?x) (string-to-number "00100" 8))
- ((char-equal owner-execute-or-setid ?S) (string-to-number "04000" 8))
- ((char-equal owner-execute-or-setid ?s) (string-to-number "04100" 8))
- ((char-equal owner-execute-or-setid ?-) 0)
- (t (error "Fourth char `%c' must be one of `xsS-'"
- owner-execute-or-setid)))
- (cond
- ((char-equal group-read ?r) (string-to-number "00040" 8))
- ((char-equal group-read ?-) 0)
- (t (error "Fifth char `%c' must be one of `r-'" group-read)))
- (cond
- ((char-equal group-write ?w) (string-to-number "00020" 8))
- ((char-equal group-write ?-) 0)
- (t (error "Sixth char `%c' must be one of `w-'" group-write)))
- (cond
- ((char-equal group-execute-or-setid ?x) (string-to-number "00010" 8))
- ((char-equal group-execute-or-setid ?S) (string-to-number "02000" 8))
- ((char-equal group-execute-or-setid ?s) (string-to-number "02010" 8))
- ((char-equal group-execute-or-setid ?-) 0)
- (t (error "Seventh char `%c' must be one of `xsS-'"
- group-execute-or-setid)))
- (cond
- ((char-equal other-read ?r) (string-to-number "00004" 8))
- ((char-equal other-read ?-) 0)
- (t (error "Eighth char `%c' must be one of `r-'" other-read)))
- (cond
- ((char-equal other-write ?w) (string-to-number "00002" 8))
- ((char-equal other-write ?-) 0)
- (t (error "Ninth char `%c' must be one of `w-'" other-write)))
- (cond
- ((char-equal other-execute-or-sticky ?x) (string-to-number "00001" 8))
- ((char-equal other-execute-or-sticky ?T) (string-to-number "01000" 8))
- ((char-equal other-execute-or-sticky ?t) (string-to-number "01001" 8))
- ((char-equal other-execute-or-sticky ?-) 0)
- (t (error "Tenth char `%c' must be one of `xtT-'"
- other-execute-or-sticky)))))))
+ (logior
+ (cond
+ ((char-equal owner-read ?r) #o0400)
+ ((char-equal owner-read ?-) 0)
+ (t (error "Second char `%c' must be one of `r-'" owner-read)))
+ (cond
+ ((char-equal owner-write ?w) #o0200)
+ ((char-equal owner-write ?-) 0)
+ (t (error "Third char `%c' must be one of `w-'" owner-write)))
+ (cond
+ ((char-equal owner-execute-or-setid ?x) #o0100)
+ ((char-equal owner-execute-or-setid ?S) #o4000)
+ ((char-equal owner-execute-or-setid ?s) #o4100)
+ ((char-equal owner-execute-or-setid ?-) 0)
+ (t (error "Fourth char `%c' must be one of `xsS-'"
+ owner-execute-or-setid)))
+ (cond
+ ((char-equal group-read ?r) #o0040)
+ ((char-equal group-read ?-) 0)
+ (t (error "Fifth char `%c' must be one of `r-'" group-read)))
+ (cond
+ ((char-equal group-write ?w) #o0020)
+ ((char-equal group-write ?-) 0)
+ (t (error "Sixth char `%c' must be one of `w-'" group-write)))
+ (cond
+ ((char-equal group-execute-or-setid ?x) #o0010)
+ ((char-equal group-execute-or-setid ?S) #o2000)
+ ((char-equal group-execute-or-setid ?s) #o2010)
+ ((char-equal group-execute-or-setid ?-) 0)
+ (t (error "Seventh char `%c' must be one of `xsS-'"
+ group-execute-or-setid)))
+ (cond
+ ((char-equal other-read ?r) #o0004)
+ ((char-equal other-read ?-) 0)
+ (t (error "Eighth char `%c' must be one of `r-'" other-read)))
+ (cond
+ ((char-equal other-write ?w) #o0002)
+ ((char-equal other-write ?-) 0)
+ (t (error "Ninth char `%c' must be one of `w-'" other-write)))
+ (cond
+ ((char-equal other-execute-or-sticky ?x) #o0001)
+ ((char-equal other-execute-or-sticky ?T) #o1000)
+ ((char-equal other-execute-or-sticky ?t) #o1001)
+ ((char-equal other-execute-or-sticky ?-) 0)
+ (t (error "Tenth char `%c' must be one of `xtT-'"
+ other-execute-or-sticky))))))
(defconst tramp-file-mode-type-map
'((0 . "-") ; Normal file (SVID-v2 and XPG2)
@@ -4033,17 +4325,16 @@ would yield t. On the other hand, the following check results in nil:
"A list of file types returned from the `stat' system call.
This is used to map a mode number to a permission string.")
-;;;###tramp-autoload
(defun tramp-file-mode-from-int (mode)
"Turn an integer representing a file mode into an ls(1)-like string."
(let ((type (cdr
- (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
- (user (logand (lsh mode -6) 7))
- (group (logand (lsh mode -3) 7))
- (other (logand (lsh mode -0) 7))
- (suid (> (logand (lsh mode -9) 4) 0))
- (sgid (> (logand (lsh mode -9) 2) 0))
- (sticky (> (logand (lsh mode -9) 1) 0)))
+ (assoc (logand (ash mode -12) 15) tramp-file-mode-type-map)))
+ (user (logand (ash mode -6) 7))
+ (group (logand (ash mode -3) 7))
+ (other (logand (ash mode -0) 7))
+ (suid (> (logand (ash mode -9) 4) 0))
+ (sgid (> (logand (ash mode -9) 2) 0))
+ (sticky (> (logand (ash mode -9) 1) 0)))
(setq user (tramp-file-mode-permissions user suid "s"))
(setq group (tramp-file-mode-permissions group sgid "s"))
(setq other (tramp-file-mode-permissions other sticky "t"))
@@ -4061,20 +4352,51 @@ This is used internally by `tramp-file-mode-from-int'."
(and suid (upcase suid-text)) ; suid, !execute
(and x "x") "-")))) ; !suid
-;;;###tramp-autoload
+;; This is a Tramp internal function. A general `set-file-uid-gid'
+;; outside Tramp is not needed, I believe.
+(defun tramp-set-file-uid-gid (filename &optional uid gid)
+ "Set the ownership for FILENAME.
+If UID and GID are provided, these values are used; otherwise uid
+and gid of the corresponding remote or local user is taken,
+depending whether FILENAME is remote or local. Both parameters
+must be non-negative integers.
+The setgid bit of the upper directory is respected.
+If FILENAME is remote, a file name handler is called."
+ (let* ((dir (file-name-directory filename))
+ (modes (file-modes dir)))
+ (when (and modes (not (zerop (logand modes #o2000))))
+ (setq gid (tramp-compat-file-attribute-group-id (file-attributes dir)))))
+
+ (let ((handler (find-file-name-handler filename 'tramp-set-file-uid-gid)))
+ (if handler
+ (funcall handler #'tramp-set-file-uid-gid filename uid gid)
+ ;; On W32 systems, "chown" does not work.
+ (unless (memq system-type '(ms-dos windows-nt))
+ (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
+ (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
+ (tramp-call-process
+ nil "chown" nil nil nil (format "%d:%d" uid gid)
+ (tramp-unquote-shell-quote-argument filename)))))))
+
(defun tramp-get-local-uid (id-format)
"The uid of the local user, in ID-FORMAT.
ID-FORMAT valid values are `string' and `integer'."
- (if (equal id-format 'integer) (user-uid) (user-login-name)))
+ ;; We use key nil for local connection properties.
+ (with-tramp-connection-property nil (format "uid-%s" id-format)
+ (if (equal id-format 'integer) (user-uid) (user-login-name))))
-;;;###tramp-autoload
(defun tramp-get-local-gid (id-format)
"The gid of the local user, in ID-FORMAT.
ID-FORMAT valid values are `string' and `integer'."
- ;; `group-gid' has been introduced with Emacs 24.4.
- (if (and (fboundp 'group-gid) (equal id-format 'integer))
- (tramp-compat-funcall 'group-gid)
- (tramp-compat-file-attribute-group-id (file-attributes "~/" id-format))))
+ ;; We use key nil for local connection properties.
+ (with-tramp-connection-property nil (format "gid-%s" id-format)
+ (cond
+ ((equal id-format 'integer) (group-gid))
+ ;; `group-name' has been introduced with Emacs 27.1.
+ ((and (fboundp 'group-name) (equal id-format 'string))
+ (tramp-compat-funcall 'group-name (group-gid)))
+ ((tramp-compat-file-attribute-group-id
+ (file-attributes "~/" id-format))))))
(defun tramp-get-local-locale (&optional vec)
"Determine locale, supporting UTF8 if possible.
@@ -4089,8 +4411,9 @@ VEC is used for tracing."
nil "locale" nil t nil "-a"))))
(while candidates
(goto-char (point-min))
- (if (string-match (format "^%s\r?$" (regexp-quote (car candidates)))
- (buffer-string))
+ (if (string-match-p
+ (format "^%s\r?$" (regexp-quote (car candidates)))
+ (buffer-string))
(setq locale (car candidates)
candidates nil)
(setq candidates (cdr candidates))))))
@@ -4098,7 +4421,6 @@ VEC is used for tracing."
(when vec (tramp-message vec 7 "locale %s" (or locale "C")))
(or locale "C"))))
-;;;###tramp-autoload
(defun tramp-check-cached-permissions (vec access)
"Check `file-attributes' caches for VEC.
Return t if according to the cache access type ACCESS is known to
@@ -4119,15 +4441,7 @@ be granted."
vec (tramp-file-name-localname vec)
(concat "file-attributes-" suffix) nil)
(file-attributes
- (tramp-make-tramp-file-name
- (tramp-file-name-method vec)
- (tramp-file-name-user vec)
- (tramp-file-name-domain vec)
- (tramp-file-name-host vec)
- (tramp-file-name-port vec)
- (tramp-file-name-localname vec)
- (tramp-file-name-hop vec))
- (intern suffix))))
+ (tramp-make-tramp-file-name vec) (intern suffix))))
(remote-uid
(tramp-get-connection-property
vec (concat "uid-" suffix) nil))
@@ -4167,14 +4481,14 @@ be granted."
(tramp-compat-file-attribute-group-id
file-attr))))))))))))
-;;;###tramp-autoload
(defun tramp-local-host-p (vec)
- "Return t if this points to the local host, nil otherwise."
+ "Return t if this points to the local host, nil otherwise.
+This handles also chrooted environments, which are not regarded as local."
(let ((host (tramp-file-name-host vec))
(port (tramp-file-name-port vec)))
(and
- (stringp host)
- (string-match tramp-local-host-regexp host)
+ (stringp tramp-local-host-regexp) (stringp host)
+ (string-match-p tramp-local-host-regexp host)
;; A port is an indication for an ssh tunnel or alike.
(null port)
;; The method shall be applied to one of the shell file name
@@ -4184,11 +4498,7 @@ be granted."
;; The local temp directory must be writable for the other user.
(file-writable-p
(tramp-make-tramp-file-name
- (tramp-file-name-method vec)
- (tramp-file-name-user vec)
- (tramp-file-name-domain vec)
- host port
- (tramp-compat-temporary-file-directory)))
+ vec (tramp-compat-temporary-file-directory) 'nohop))
;; On some systems, chown runs only for root.
(or (zerop (user-uid))
;; This is defined in tramp-sh.el. Let's assume this is
@@ -4198,20 +4508,14 @@ be granted."
(defun tramp-get-remote-tmpdir (vec)
"Return directory for temporary files on the remote host identified by VEC."
(with-tramp-connection-property vec "tmpdir"
- (let ((dir (tramp-make-tramp-file-name
- (tramp-file-name-method vec)
- (tramp-file-name-user vec)
- (tramp-file-name-domain vec)
- (tramp-file-name-host vec)
- (tramp-file-name-port vec)
- (or (tramp-get-method-parameter vec 'tramp-tmpdir) "/tmp")
- (tramp-file-name-hop vec))))
+ (let ((dir
+ (tramp-make-tramp-file-name
+ vec (or (tramp-get-method-parameter vec 'tramp-tmpdir) "/tmp"))))
(or (and (file-directory-p dir) (file-writable-p dir)
- (file-remote-p dir 'localname))
+ (tramp-compat-file-local-name dir))
(tramp-error vec 'file-error "Directory %s not accessible" dir))
dir)))
-;;;###tramp-autoload
(defun tramp-make-tramp-temp-file (vec)
"Create a temporary file on the remote host identified by VEC.
Return the local name of the temporary file."
@@ -4228,7 +4532,7 @@ Return the local name of the temporary file."
(setq result nil)
;; This creates the file by side effect.
(set-file-times result)
- (set-file-modes result (string-to-number "0700" 8))))
+ (set-file-modes result #o0700)))
;; Return the local part.
(with-parsed-tramp-file-name result nil localname)))
@@ -4238,11 +4542,11 @@ Return the local name of the temporary file."
(when (stringp tramp-temp-buffer-file-name)
(ignore-errors (delete-file tramp-temp-buffer-file-name))))
-(add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
+(add-hook 'kill-buffer-hook #'tramp-delete-temp-file-function)
(add-hook 'tramp-unload-hook
(lambda ()
(remove-hook 'kill-buffer-hook
- 'tramp-delete-temp-file-function)))
+ #'tramp-delete-temp-file-function)))
(defun tramp-handle-make-auto-save-file-name ()
"Like `make-auto-save-file-name' for Tramp files.
@@ -4278,7 +4582,7 @@ this file, if that variable is non-nil."
(tramp-compat-file-name-unquote (buffer-file-name)))
tramp-auto-save-directory))))
;; Run plain `make-auto-save-file-name'.
- (tramp-run-real-handler 'make-auto-save-file-name nil)))
+ (tramp-run-real-handler #'make-auto-save-file-name nil)))
(defun tramp-subst-strs-in-string (alist string)
"Replace all occurrences of the string FROM with TO in STRING.
@@ -4317,22 +4621,19 @@ ALIST is of the form ((FROM . TO) ...)."
It always returns a return code. The Lisp error raised when
PROGRAM is nil is trapped also, returning 1. Furthermore, traces
are written with verbosity of 6."
- (let ((default-directory (tramp-compat-temporary-file-directory))
- (v (or vec
- (make-tramp-file-name
- :method tramp-current-method :user tramp-current-user
- :domain tramp-current-domain :host tramp-current-host
- :port tramp-current-port)))
+ (let ((default-directory (tramp-compat-temporary-file-directory))
+ (process-environment (default-toplevel-value 'process-environment))
(destination (if (eq destination t) (current-buffer) destination))
+ (vec (or vec (car tramp-current-connection)))
output error result)
(tramp-message
- v 6 "`%s %s' %s %s"
- program (mapconcat 'identity args " ") infile destination)
+ vec 6 "`%s %s' %s %s"
+ program (string-join args " ") infile destination)
(condition-case err
(with-temp-buffer
(setq result
(apply
- 'call-process program infile (or destination t) display args))
+ #'call-process program infile (or destination t) display args))
;; `result' could also be an error string.
(when (stringp result)
(setq error result
@@ -4344,8 +4645,8 @@ are written with verbosity of 6."
(setq error (error-message-string err)
result 1)))
(if (zerop (length error))
- (tramp-message v 6 "%d\n%s" result output)
- (tramp-message v 6 "%d\n%s\n%s" result output error))
+ (tramp-message vec 6 "%d\n%s" result output)
+ (tramp-message vec 6 "%d\n%s\n%s" result output error))
result))
(defun tramp-call-process-region
@@ -4354,55 +4655,77 @@ are written with verbosity of 6."
It always returns a return code. The Lisp error raised when
PROGRAM is nil is trapped also, returning 1. Furthermore, traces
are written with verbosity of 6."
- (let ((default-directory (tramp-compat-temporary-file-directory))
- (v (or vec
- (make-tramp-file-name
- :method tramp-current-method :user tramp-current-user
- :domain tramp-current-domain :host tramp-current-host
- :port tramp-current-port)))
+ (let ((default-directory (tramp-compat-temporary-file-directory))
+ (process-environment (default-toplevel-value 'process-environment))
(buffer (if (eq buffer t) (current-buffer) buffer))
result)
(tramp-message
- v 6 "`%s %s' %s %s %s %s"
- program (mapconcat 'identity args " ") start end delete buffer)
+ vec 6 "`%s %s' %s %s %s %s"
+ program (string-join args " ") start end delete buffer)
(condition-case err
(progn
(setq result
(apply
- 'call-process-region
+ #'call-process-region
start end program delete buffer display args))
;; `result' could also be an error string.
(when (stringp result)
(signal 'file-error (list result)))
(with-current-buffer (if (bufferp buffer) buffer (current-buffer))
(if (zerop result)
- (tramp-message v 6 "%d" result)
- (tramp-message v 6 "%d\n%s" result (buffer-string)))))
+ (tramp-message vec 6 "%d" result)
+ (tramp-message vec 6 "%d\n%s" result (buffer-string)))))
(error
(setq result 1)
- (tramp-message v 6 "%d\n%s" result (error-message-string err))))
+ (tramp-message vec 6 "%d\n%s" result (error-message-string err))))
+ result))
+
+(defun tramp-process-lines
+ (vec program &rest args)
+ "Calls `process-lines' on the local host.
+If an error occurs, it returns nil. Traces are written with
+verbosity of 6."
+ (let ((default-directory (tramp-compat-temporary-file-directory))
+ (process-environment (default-toplevel-value 'process-environment))
+ (vec (or vec (car tramp-current-connection)))
+ result)
+ (if args
+ (tramp-message vec 6 "%s %s" program (string-join args " "))
+ (tramp-message vec 6 "%s" program))
+ (setq result
+ (condition-case err
+ (apply #'process-lines program args)
+ (error
+ (tramp-error vec (car err) (cdr err)))))
+ (tramp-message vec 6 "%s" result)
result))
-;;;###tramp-autoload
(defun tramp-read-passwd (proc &optional prompt)
"Read a password from user (compat function).
Consults the auth-source package.
Invokes `password-read' if available, `read-passwd' else."
(let* ((case-fold-search t)
(key (tramp-make-tramp-file-name
- tramp-current-method tramp-current-user tramp-current-domain
- tramp-current-host tramp-current-port ""))
+ ;; In tramp-sh.el, we must use "password-vector" due to
+ ;; multi-hop.
+ (tramp-get-connection-property
+ proc "password-vector" (process-get proc 'vector))
+ 'noloc 'nohop))
(pw-prompt
(or prompt
(with-current-buffer (process-buffer proc)
(tramp-check-for-regexp proc tramp-password-prompt-regexp)
(format "%s for %s " (capitalize (match-string 1)) key))))
+ (auth-source-creation-prompts `((secret . ,pw-prompt)))
;; We suspend the timers while reading the password.
(stimers (with-timeout-suspend))
auth-info auth-passwd)
(unwind-protect
(with-parsed-tramp-file-name key nil
+ (setq tramp-password-save-function nil
+ user
+ (or user (tramp-get-connection-property key "login-as" nil)))
(prog1
(or
;; See if auth-sources contains something useful.
@@ -4411,82 +4734,69 @@ Invokes `password-read' if available, `read-passwd' else."
v "first-password-request" nil)
;; Try with Tramp's current method.
(setq auth-info
- (auth-source-search
- :max 1
- (and tramp-current-user :user)
- (if tramp-current-domain
- (format
- "%s%s%s"
- tramp-current-user tramp-prefix-domain-format
- tramp-current-domain)
- tramp-current-user)
- :host
- (if tramp-current-port
- (format
- "%s%s%s"
- tramp-current-host tramp-prefix-port-format
- tramp-current-port)
- tramp-current-host)
- :port tramp-current-method
- :require
- (cons
- :secret (and tramp-current-user '(:user))))
- auth-passwd (plist-get
- (nth 0 auth-info) :secret)
- auth-passwd (if (functionp auth-passwd)
- (funcall auth-passwd)
- auth-passwd))))
+ (car
+ (auth-source-search
+ :max 1
+ (and user :user)
+ (if domain
+ (concat
+ user tramp-prefix-domain-format domain)
+ user)
+ :host
+ (if port
+ (concat
+ host tramp-prefix-port-format port)
+ host)
+ :port method
+ :require (cons :secret (and user '(:user)))
+ :create t))
+ tramp-password-save-function
+ (plist-get auth-info :save-function)
+ auth-passwd (plist-get auth-info :secret)))
+ (while (functionp auth-passwd)
+ (setq auth-passwd (funcall auth-passwd)))
+ auth-passwd)
+
;; Try the password cache.
- (let ((password (password-read pw-prompt key)))
- (password-cache-add key password)
- password)
- ;; Else, get the password interactively.
+ (progn
+ (setq auth-passwd (password-read pw-prompt key)
+ tramp-password-save-function
+ (lambda () (password-cache-add key auth-passwd)))
+ auth-passwd)
+
+ ;; Else, get the password interactively w/o cache.
(read-passwd pw-prompt))
+
(tramp-set-connection-property v "first-password-request" nil)))
+
;; Reenable the timers.
(with-timeout-unsuspend stimers))))
-;;;###tramp-autoload
(defun tramp-clear-passwd (vec)
"Clear password cache for connection related to VEC."
(let ((method (tramp-file-name-method vec))
- (user (tramp-file-name-user vec))
- (domain (tramp-file-name-domain vec))
(user-domain (tramp-file-name-user-domain vec))
- (host (tramp-file-name-host vec))
- (port (tramp-file-name-port vec))
(host-port (tramp-file-name-host-port vec))
(hop (tramp-file-name-hop vec)))
(when hop
;; Clear also the passwords of the hops.
- (tramp-clear-passwd
- (tramp-dissect-file-name
- (concat
- tramp-prefix-format
- (replace-regexp-in-string
- (concat tramp-postfix-hop-regexp "$")
- tramp-postfix-host-format hop)))))
+ (tramp-clear-passwd (tramp-dissect-hop-name hop)))
(auth-source-forget
`(:max 1 ,(and user-domain :user) ,user-domain
:host ,host-port :port ,method))
- (password-cache-remove
- (tramp-make-tramp-file-name method user domain host port ""))))
-
-;; Snarfed code from time-date.el.
-
-(defconst tramp-half-a-year '(241 17024)
-"Evaluated by \"(days-to-time 183)\".")
+ (password-cache-remove (tramp-make-tramp-file-name vec 'noloc 'nohop))))
-;;;###tramp-autoload
(defun tramp-time-diff (t1 t2)
"Return the difference between the two times, in seconds.
T1 and T2 are time values (as returned by `current-time' for example)."
- ;; Starting with Emacs 25.1, we could change this to use `time-subtract'.
- (float-time (tramp-compat-funcall 'subtract-time t1 t2)))
+ (float-time (time-subtract t1 t2)))
(defun tramp-unquote-shell-quote-argument (s)
- "Remove quotation prefix \"/:\" from string S, and quote it then for shell."
- (shell-quote-argument (tramp-compat-file-name-unquote s)))
+ "Remove quotation prefix \"/:\" from string S, and quote it then for shell.
+Suppress `shell-file-name'. This is needed on w32 systems, which
+would use a wrong quoting for local file names. See `w32-shell-name'."
+ (let (shell-file-name)
+ (shell-quote-argument (tramp-compat-file-name-unquote s))))
;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
;; does not deal well with newline characters. Newline is replaced by
@@ -4509,7 +4819,6 @@ T1 and T2 are time values (as returned by `current-time' for example)."
;;
;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
;; function to work with Bourne-like shells.
-;;;###tramp-autoload
(defun tramp-shell-quote-argument (s)
"Similar to `shell-quote-argument', but groks newlines.
Only works for Bourne-like shells."
@@ -4541,82 +4850,46 @@ Only works for Bourne-like shells."
pid)
;; If it's a Tramp process, send the INT signal remotely.
(when (and (processp proc) (setq pid (process-get proc 'remote-pid)))
- (if (not (process-live-p proc))
+ (if (not (process-live-p proc))
(tramp-error proc 'error "Process %s is not active" proc)
(tramp-message proc 5 "Interrupt process %s with pid %s" proc pid)
;; This is for tramp-sh.el. Other backends do not support this (yet).
(tramp-compat-funcall
'tramp-send-command
- (tramp-get-connection-property proc "vector" nil)
- (format "kill -2 %d" pid))
+ (process-get proc 'vector)
+ (format "kill -2 -%d" pid))
;; Wait, until the process has disappeared. If it doesn't,
;; fall back to the default implementation.
- (with-timeout (1 (ignore))
- (while (process-live-p proc)
- ;; We cannot run `tramp-accept-process-output', it blocks timers.
- (accept-process-output proc 0.1))
- ;; Report success.
- proc)))))
+ (while (tramp-accept-process-output proc 0))
+ (not (process-live-p proc))))))
;; `interrupt-process-functions' exists since Emacs 26.1.
(when (boundp 'interrupt-process-functions)
- (add-hook 'interrupt-process-functions 'tramp-interrupt-process)
+ (add-hook 'interrupt-process-functions #'tramp-interrupt-process)
(add-hook
'tramp-unload-hook
(lambda ()
- (remove-hook 'interrupt-process-functions 'tramp-interrupt-process))))
-
-;;; Integration of eshell.el:
-
-;; eshell.el keeps the path in `eshell-path-env'. We must change it
-;; when `default-directory' points to another host.
-(defun tramp-eshell-directory-change ()
- "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
- (setq eshell-path-env
- (if (tramp-tramp-file-p default-directory)
- (with-parsed-tramp-file-name default-directory nil
- (mapconcat
- 'identity
- (or
- ;; When `tramp-own-remote-path' is in `tramp-remote-path',
- ;; the remote path is only set in the session cache.
- ;; Use `path-separator' as it does eshell.
- (tramp-get-connection-property
- (tramp-get-connection-process v) "remote-path" nil)
- (tramp-get-connection-property v "remote-path" nil))
- path-separator))
- (getenv "PATH"))))
-
-(eval-after-load "esh-util"
- '(progn
- (add-hook 'eshell-mode-hook
- 'tramp-eshell-directory-change)
- (add-hook 'eshell-directory-change-hook
- 'tramp-eshell-directory-change)
- (add-hook 'tramp-unload-hook
- (lambda ()
- (remove-hook 'eshell-mode-hook
- 'tramp-eshell-directory-change)
- (remove-hook 'eshell-directory-change-hook
- 'tramp-eshell-directory-change)))))
+ (remove-hook 'interrupt-process-functions #'tramp-interrupt-process))))
;; Checklist for `tramp-unload-hook'
;; - Unload all `tramp-*' packages
;; - Reset `file-name-handler-alist'
;; - Cleanup hooks where Tramp functions are in
-;; - Cleanup advised functions
;; - Cleanup autoloads
;;;###autoload
(defun tramp-unload-tramp ()
"Discard Tramp from loading remote files."
(interactive)
- ;; ange-ftp settings must be enabled.
+ ;; ange-ftp settings must be re-enabled.
(tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
;; Maybe it's not loaded yet.
(ignore-errors (unload-feature 'tramp 'force)))
(provide 'tramp)
+(run-hooks 'tramp--startup-hook)
+(setq tramp--startup-hook nil)
+
;;; TODO:
;;
;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
@@ -4639,6 +4912,12 @@ Only works for Bourne-like shells."
;; and friends, for most of the handlers this is the major
;; difference between the different backends. Other handlers but
;; *-process-file would profit from this as well.
+;;
+;; * Get rid of `shell-command'. In its primary implementation, it
+;; uses `process-file-shell-command' and
+;; `start-file-process-shell-command', which is sufficient due to
+;; connection-local `shell-file-name'.
+
;;; tramp.el ends here