tramp-ftp.el 7.5 KB

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