tramp-ftp.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; tramp-ftp.el --- Tramp convenience functions for Ange-FTP
  2. ;; Copyright (C) 2002-2012 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. (eval-when-compile
  23. ;; Pacify byte-compiler.
  24. (require 'cl)
  25. (require 'custom))
  26. ;; Disable Ange-FTP from file-name-handler-alist.
  27. ;; To handle EFS, the following functions need to be dealt with:
  28. ;;
  29. ;; * dired-before-readin-hook contains efs-dired-before-readin
  30. ;; * file-name-handler-alist contains efs-file-handler-function
  31. ;; and efs-root-handler-function and efs-sifn-handler-function
  32. ;; * find-file-hooks contains efs-set-buffer-mode
  33. ;;
  34. ;; But it won't happen for EFS since the XEmacs maintainers
  35. ;; don't want to use a unified filename syntax.
  36. (defun tramp-disable-ange-ftp ()
  37. "Turn Ange-FTP off.
  38. This is useful for unified remoting. See
  39. `tramp-file-name-structure-unified' and
  40. `tramp-file-name-structure-separate' for details. Requests suitable
  41. for Ange-FTP will be forwarded to Ange-FTP. Also see the variables
  42. `tramp-ftp-method', `tramp-default-method', and
  43. `tramp-default-method-alist'.
  44. This function is not needed in Emacsen which include Tramp, but is
  45. present for backward compatibility."
  46. (let ((a1 (rassq 'ange-ftp-hook-function file-name-handler-alist))
  47. (a2 (rassq 'ange-ftp-completion-hook-function file-name-handler-alist)))
  48. (setq file-name-handler-alist
  49. (delete a1 (delete a2 file-name-handler-alist)))))
  50. (eval-after-load "ange-ftp"
  51. '(when (functionp 'tramp-disable-ange-ftp)
  52. (tramp-disable-ange-ftp)))
  53. ;;;###autoload
  54. (defun tramp-ftp-enable-ange-ftp ()
  55. ;; The following code is commented out in Ange-FTP.
  56. ;;; This regexp takes care of real ange-ftp file names (with a slash
  57. ;;; and colon).
  58. ;;; Don't allow the host name to end in a period--some systems use /.:
  59. (or (assoc "^/[^/:]*[^/:.]:" file-name-handler-alist)
  60. (setq file-name-handler-alist
  61. (cons '("^/[^/:]*[^/:.]:" . ange-ftp-hook-function)
  62. file-name-handler-alist)))
  63. ;;; This regexp recognizes absolute filenames with only one component,
  64. ;;; for the sake of hostname completion.
  65. (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
  66. (setq file-name-handler-alist
  67. (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
  68. file-name-handler-alist)))
  69. ;;; This regexp recognizes absolute filenames with only one component
  70. ;;; on Windows, for the sake of hostname completion.
  71. (and (memq system-type '(ms-dos windows-nt))
  72. (or (assoc "^[a-zA-Z]:/[^/:]*\\'" file-name-handler-alist)
  73. (setq file-name-handler-alist
  74. (cons '("^[a-zA-Z]:/[^/:]*\\'" .
  75. ange-ftp-completion-hook-function)
  76. file-name-handler-alist)))))
  77. (add-hook 'tramp-ftp-unload-hook 'tramp-ftp-enable-ange-ftp)
  78. ;; Define FTP method ...
  79. ;;;###tramp-autoload
  80. (defconst tramp-ftp-method "ftp"
  81. "*When this method name is used, forward all calls to Ange-FTP.")
  82. ;; ... and add it to the method list.
  83. ;;;###tramp-autoload
  84. (unless (featurep 'xemacs)
  85. (add-to-list 'tramp-methods (cons tramp-ftp-method nil))
  86. ;; Add some defaults for `tramp-default-method-alist'.
  87. (add-to-list 'tramp-default-method-alist
  88. (list "\\`ftp\\." nil tramp-ftp-method))
  89. (add-to-list 'tramp-default-method-alist
  90. (list nil "\\`\\(anonymous\\|ftp\\)\\'" tramp-ftp-method)))
  91. ;; Add completion function for FTP method.
  92. ;;;###tramp-autoload
  93. (eval-after-load 'tramp
  94. '(tramp-set-completion-function
  95. tramp-ftp-method
  96. '((tramp-parse-netrc "~/.netrc"))))
  97. ;; If there is URL syntax, `substitute-in-file-name' needs special
  98. ;; handling.
  99. (put 'substitute-in-file-name 'ange-ftp 'tramp-handle-substitute-in-file-name)
  100. (add-hook 'tramp-ftp-unload-hook
  101. (lambda ()
  102. (setplist 'substitute-in-file-name
  103. (delete 'ange-ftp
  104. (delete 'tramp-handle-substitute-in-file-name
  105. (symbol-plist
  106. 'substitute-in-file-name))))))
  107. ;;;###tramp-autoload
  108. (defun tramp-ftp-file-name-handler (operation &rest args)
  109. "Invoke the Ange-FTP handler for OPERATION.
  110. First arg specifies the OPERATION, second arg is a list of arguments to
  111. pass to the OPERATION."
  112. (save-match-data
  113. (or (boundp 'ange-ftp-name-format)
  114. (let (file-name-handler-alist) (require 'ange-ftp)))
  115. (let ((ange-ftp-name-format
  116. (list (nth 0 tramp-file-name-structure)
  117. (nth 3 tramp-file-name-structure)
  118. (nth 2 tramp-file-name-structure)
  119. (nth 4 tramp-file-name-structure)))
  120. ;; ange-ftp uses `ange-ftp-ftp-name-arg' and `ange-ftp-ftp-name-res'
  121. ;; for optimization in `ange-ftp-ftp-name'. If Tramp wasn't active,
  122. ;; there could be incorrect values from previous calls in case the
  123. ;; "ftp" method is used in the Tramp file name. So we unset
  124. ;; those values.
  125. (ange-ftp-ftp-name-arg "")
  126. (ange-ftp-ftp-name-res nil))
  127. (cond
  128. ;; If argument is a symlink, `file-directory-p' and
  129. ;; `file-exists-p' call the traversed file recursively. So we
  130. ;; cannot disable the file-name-handler this case. We set the
  131. ;; connection property "started" in order to put the remote
  132. ;; location into the cache, which is helpful for further
  133. ;; completion. We don't use `with-parsed-tramp-file-name',
  134. ;; because this returns another user but the one declared in
  135. ;; "~/.netrc".
  136. ((memq operation '(file-directory-p file-exists-p))
  137. (if (apply 'ange-ftp-hook-function operation args)
  138. (let ((v (tramp-dissect-file-name (car args) t)))
  139. (aset v 0 tramp-ftp-method)
  140. (tramp-set-connection-property v "started" t))
  141. nil))
  142. ;; If the second argument of `copy-file' or `rename-file' is a
  143. ;; remote file name but via FTP, ange-ftp doesn't check this.
  144. ;; We must copy it locally first, because there is no place in
  145. ;; ange-ftp for correct handling.
  146. ((and (memq operation '(copy-file rename-file))
  147. (file-remote-p (cadr args))
  148. (not (tramp-ftp-file-name-p (cadr args))))
  149. (let* ((filename (car args))
  150. (newname (cadr args))
  151. (tmpfile (tramp-compat-make-temp-file filename))
  152. (args (cddr args)))
  153. ;; We must set `ok-if-already-exists' to t in the first
  154. ;; step, because the temp file has been created already.
  155. (if (eq operation 'copy-file)
  156. (apply operation filename tmpfile t (cdr args))
  157. (apply operation filename tmpfile t))
  158. (unwind-protect
  159. (rename-file tmpfile newname (car args))
  160. ;; Cleanup.
  161. (ignore-errors (delete-file tmpfile)))))
  162. ;; Normally, the handlers must be discarded.
  163. ;; `inhibit-file-name-handlers' isn't sufficient, because the
  164. ;; local file name could be in Tramp syntax as well (for
  165. ;; example, returning VMS file names like "/DISK$CAM:/AAA").
  166. ;; That's why we set also `tramp-mode' to nil.
  167. (t (let* (;(tramp-mode nil)
  168. (inhibit-file-name-handlers
  169. (list 'tramp-file-name-handler
  170. 'tramp-completion-file-name-handler
  171. (and (eq inhibit-file-name-operation operation)
  172. inhibit-file-name-handlers)))
  173. (inhibit-file-name-operation operation))
  174. (apply 'ange-ftp-hook-function operation args)))))))
  175. ;;;###tramp-autoload
  176. (defsubst tramp-ftp-file-name-p (filename)
  177. "Check if it's a filename that should be forwarded to Ange-FTP."
  178. (let ((v (tramp-dissect-file-name filename)))
  179. (string= (tramp-file-name-method v) tramp-ftp-method)))
  180. ;;;###tramp-autoload
  181. (unless (featurep 'xemacs)
  182. (add-to-list 'tramp-foreign-file-name-handler-alist
  183. (cons 'tramp-ftp-file-name-p 'tramp-ftp-file-name-handler)))
  184. (add-hook 'tramp-unload-hook
  185. (lambda ()
  186. (unload-feature 'tramp-ftp 'force)))
  187. (provide 'tramp-ftp)
  188. ;;; TODO:
  189. ;; * There are no backup files on FTP hosts.
  190. ;;; tramp-ftp.el ends here