1 ;;; tramp-ftp.el --- Tramp convenience functions for Ange-FTP -*- lexical-binding:t -*-
3 ;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Convenience functions for calling Ange-FTP from Tramp.
27 ;; Most of them are displaced from tramp.el.
33 ;; Pacify byte-compiler.
37 (defvar ange-ftp-ftp-name-arg
)
38 (defvar ange-ftp-ftp-name-res
)
39 (defvar ange-ftp-name-format
)
41 ;; Disable Ange-FTP from file-name-handler-alist.
42 (defun tramp-disable-ange-ftp ()
44 This is useful for unified remoting. See
45 `tramp-file-name-structure' for details. Requests suitable for
46 Ange-FTP will be forwarded to Ange-FTP. Also see the variables
47 `tramp-ftp-method', `tramp-default-method', and
48 `tramp-default-method-alist'.
50 This function is not needed in Emacsen which include Tramp, but is
51 present for backward compatibility."
52 (let ((a1 (rassq 'ange-ftp-hook-function file-name-handler-alist
))
53 (a2 (rassq 'ange-ftp-completion-hook-function file-name-handler-alist
)))
54 (setq file-name-handler-alist
55 (delete a1
(delete a2 file-name-handler-alist
)))))
57 (eval-after-load "ange-ftp"
58 '(when (functionp 'tramp-disable-ange-ftp
)
59 (tramp-disable-ange-ftp)))
62 (defun tramp-ftp-enable-ange-ftp ()
63 "Reenable Ange-FTP, when Tramp is unloaded."
64 ;; The following code is commented out in Ange-FTP.
66 ;;; This regexp takes care of real ange-ftp file names (with a slash
68 ;;; Don't allow the host name to end in a period--some systems use /.:
69 (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist
)
70 (setq file-name-handler-alist
71 (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function
)
72 file-name-handler-alist
)))
74 ;;; This regexp recognizes absolute filenames with only one component,
75 ;;; for the sake of hostname completion.
76 (or (assoc "^/[^/:]*\\'" file-name-handler-alist
)
77 (setq file-name-handler-alist
78 (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function
)
79 file-name-handler-alist
)))
81 ;;; This regexp recognizes absolute filenames with only one component
82 ;;; on Windows, for the sake of hostname completion.
83 (and (memq system-type
'(ms-dos windows-nt
))
84 (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist
)
85 (setq file-name-handler-alist
86 (cons '("^[a-zA-Z]:/[^/:]*\\'" .
87 ange-ftp-completion-hook-function
)
88 file-name-handler-alist
)))))
90 (add-hook 'tramp-ftp-unload-hook
'tramp-ftp-enable-ange-ftp
)
92 ;; Define FTP method ...
94 (defconst tramp-ftp-method
"ftp"
95 "When this method name is used, forward all calls to Ange-FTP.")
97 ;; ... and add it to the method list.
99 (add-to-list 'tramp-methods
(cons tramp-ftp-method nil
))
101 ;; Add some defaults for `tramp-default-method-alist'.
103 (add-to-list 'tramp-default-method-alist
104 (list "\\`ftp\\." nil tramp-ftp-method
))
106 (add-to-list 'tramp-default-method-alist
107 (list nil
"\\`\\(anonymous\\|ftp\\)\\'" tramp-ftp-method
))
109 ;; Add completion function for FTP method.
111 (eval-after-load 'tramp
112 '(tramp-set-completion-function
114 '((tramp-parse-netrc "~/.netrc"))))
117 (defun tramp-ftp-file-name-handler (operation &rest args
)
118 "Invoke the Ange-FTP handler for OPERATION.
119 First arg specifies the OPERATION, second arg is a list of arguments to
120 pass to the OPERATION."
122 (or (boundp 'ange-ftp-name-format
)
123 (let (file-name-handler-alist) (require 'ange-ftp
)))
124 (let ((ange-ftp-name-format
125 (list (nth 0 (tramp-file-name-structure))
126 (nth 3 (tramp-file-name-structure))
127 (nth 2 (tramp-file-name-structure))
128 (nth 4 (tramp-file-name-structure))))
129 ;; ange-ftp uses `ange-ftp-ftp-name-arg' and `ange-ftp-ftp-name-res'
130 ;; for optimization in `ange-ftp-ftp-name'. If Tramp wasn't active,
131 ;; there could be incorrect values from previous calls in case the
132 ;; "ftp" method is used in the Tramp file name. So we unset
134 (ange-ftp-ftp-name-arg "")
135 (ange-ftp-ftp-name-res nil
))
137 ;; If argument is a symlink, `file-directory-p' and
138 ;; `file-exists-p' call the traversed file recursively. So we
139 ;; cannot disable the file-name-handler this case. We set the
140 ;; connection property "started" in order to put the remote
141 ;; location into the cache, which is helpful for further
142 ;; completion. We don't use `with-parsed-tramp-file-name',
143 ;; because this returns another user but the one declared in
145 ((memq operation
'(file-directory-p file-exists-p
))
146 (if (apply 'ange-ftp-hook-function operation args
)
147 (let ((v (tramp-dissect-file-name (car args
) t
)))
148 (setf (tramp-file-name-method v
) tramp-ftp-method
)
149 (tramp-set-connection-property v
"started" t
))
152 ;; If the second argument of `copy-file' or `rename-file' is a
153 ;; remote file name but via FTP, ange-ftp doesn't check this.
154 ;; We must copy it locally first, because there is no place in
155 ;; ange-ftp for correct handling.
156 ((and (memq operation
'(copy-file rename-file
))
157 (tramp-tramp-file-p (cadr args
))
158 (not (tramp-ftp-file-name-p (cadr args
))))
159 (let* ((filename (car args
))
160 (newname (cadr args
))
161 (tmpfile (tramp-compat-make-temp-file filename
))
163 ;; We must set `ok-if-already-exists' to t in the first
164 ;; step, because the temp file has been created already.
165 (if (eq operation
'copy-file
)
166 (apply operation filename tmpfile t
(cdr args
))
167 (apply operation filename tmpfile t
))
169 (rename-file tmpfile newname
(car args
))
171 (ignore-errors (delete-file tmpfile
)))))
173 ;; Normally, the handlers must be discarded.
174 (t (let* ((inhibit-file-name-handlers
175 (list 'tramp-file-name-handler
176 'tramp-completion-file-name-handler
177 (and (eq inhibit-file-name-operation operation
)
178 inhibit-file-name-handlers
)))
179 (inhibit-file-name-operation operation
))
180 (apply 'ange-ftp-hook-function operation args
)))))))
182 ;; It must be a `defsubst' in order to push the whole code into
183 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
185 (defsubst tramp-ftp-file-name-p
(filename)
186 "Check if it's a filename that should be forwarded to Ange-FTP."
187 (string= (tramp-file-name-method (tramp-dissect-file-name filename
))
191 (add-to-list 'tramp-foreign-file-name-handler-alist
192 (cons 'tramp-ftp-file-name-p
'tramp-ftp-file-name-handler
))
194 (add-hook 'tramp-unload-hook
196 (unload-feature 'tramp-ftp
'force
)))
202 ;; * There are no backup files on FTP hosts.
204 ;;; tramp-ftp.el ends here