url-file.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ;;; url-file.el --- File retrieval code
  2. ;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes
  4. ;; This file is part of GNU Emacs.
  5. ;;
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'mailcap)
  20. (require 'url-vars)
  21. (require 'url-parse)
  22. (require 'url-dired)
  23. (defconst url-file-default-port 21 "Default FTP port.")
  24. (defconst url-file-asynchronous-p t "FTP transfers are asynchronous.")
  25. (defalias 'url-file-expand-file-name 'url-default-expander)
  26. (defun url-file-find-possibly-compressed-file (fname &rest args)
  27. "Find the exact file referenced by `fname'.
  28. This tries the common compression extensions, because things like
  29. ange-ftp and efs are not quite smart enough to realize when a server
  30. can do automatic decompression for them, and won't find 'foo' if
  31. 'foo.gz' exists, even though the FTP server would happily serve it up
  32. to them."
  33. (let ((scratch nil)
  34. (compressed-extensions '("" ".gz" ".z" ".Z" ".bz2"))
  35. (found nil))
  36. (while (and compressed-extensions (not found))
  37. (if (file-exists-p (setq scratch (concat fname (pop compressed-extensions))))
  38. (setq found scratch)))
  39. found))
  40. (defun url-file-host-is-local-p (host)
  41. "Return t if HOST references our local machine."
  42. (let ((case-fold-search t))
  43. (or
  44. (null host)
  45. (string= "" host)
  46. (equal (downcase host) (downcase (system-name)))
  47. (and (string-match "^localhost$" host) t)
  48. (and (not (string-match (regexp-quote ".") host))
  49. (equal (downcase host) (if (string-match (regexp-quote ".")
  50. (system-name))
  51. (substring (system-name) 0
  52. (match-beginning 0))
  53. (system-name)))))))
  54. (defun url-file-asynch-callback (x y name buff func args &optional efs)
  55. (if (not (featurep 'ange-ftp))
  56. ;; EFS passes us an extra argument
  57. (setq name buff
  58. buff func
  59. func args
  60. args efs))
  61. (let ((size (nth 7 (file-attributes name))))
  62. (with-current-buffer buff
  63. (goto-char (point-max))
  64. (if (/= -1 size)
  65. (insert (format "Content-length: %d\n" size)))
  66. (insert "\n")
  67. (insert-file-contents-literally name)
  68. (if (not (url-file-host-is-local-p (url-host url-current-object)))
  69. (condition-case ()
  70. (delete-file name)
  71. (error nil)))
  72. (apply func args))))
  73. (declare-function ange-ftp-set-passwd "ange-ftp" (host user passwd))
  74. (declare-function ange-ftp-copy-file-internal "ange-ftp"
  75. (filename newname ok-if-already-exists
  76. keep-date &optional msg cont nowait))
  77. (defun url-file-build-filename (url)
  78. (if (not (vectorp url))
  79. (setq url (url-generic-parse-url url)))
  80. (let* ((user (url-user url))
  81. (pass (url-password url))
  82. (port (url-port url))
  83. (host (url-host url))
  84. (site (if (and port (/= port 21))
  85. (if (featurep 'ange-ftp)
  86. (format "%s %d" host port)
  87. ;; This works in Emacs 21's ange-ftp too.
  88. (format "%s#%d" host port))
  89. host))
  90. (file (url-unhex-string (url-filename url)))
  91. (filename (cond
  92. ;; ftp: URL.
  93. ((or user (not (url-file-host-is-local-p host)))
  94. (concat "/" (or user "anonymous") "@" site ":" file))
  95. ;; file: URL on Windows.
  96. ((and (string-match "\\`/[a-zA-Z]:/" file)
  97. (memq system-type '(ms-dos windows-nt)))
  98. (substring file 1))
  99. ;; file: URL with a file:/bar:/foo-like spec.
  100. ((string-match "\\`/[^/]+:/" file)
  101. (concat "/:" file))
  102. (t
  103. file)))
  104. pos-index)
  105. (and user pass
  106. (cond
  107. ((featurep 'ange-ftp)
  108. (ange-ftp-set-passwd host user pass))
  109. ((when (featurep 'xemacs)
  110. (or (featurep 'efs) (featurep 'efs-auto)
  111. (efs-set-passwd host user pass))))
  112. (t
  113. nil)))
  114. ;; This makes sure that directories have a trailing directory
  115. ;; separator on them so URL expansion works right.
  116. ;;
  117. ;; FIXME? What happens if the remote system doesn't use our local
  118. ;; directory-sep-char as its separator? Would it be safer to just
  119. ;; use '/' unconditionally and rely on the FTP server to
  120. ;; straighten it out for us?
  121. ;; (if (and (file-directory-p filename)
  122. ;; (not (string-match (format "%c$" directory-sep-char) filename)))
  123. ;; (setf (url-filename url)
  124. ;; (format "%s%c" filename directory-sep-char)))
  125. (if (and (file-directory-p filename)
  126. (not (string-match "/\\'" filename)))
  127. (setf (url-filename url) (format "%s/" filename)))
  128. ;; If it is a directory, look for an index file first.
  129. (if (and (file-directory-p filename)
  130. url-directory-index-file
  131. (setq pos-index (expand-file-name url-directory-index-file filename))
  132. (file-exists-p pos-index)
  133. (file-readable-p pos-index))
  134. (setq filename pos-index))
  135. ;; Find the (possibly compressed) file
  136. (setq filename (url-file-find-possibly-compressed-file filename))
  137. filename))
  138. ;;;###autoload
  139. (defun url-file (url callback cbargs)
  140. "Handle file: and ftp: URLs."
  141. (let* ((buffer nil)
  142. (uncompressed-filename nil)
  143. (content-type nil)
  144. (content-encoding nil)
  145. (coding-system-for-read 'binary)
  146. (filename (url-file-build-filename url)))
  147. (or filename (error "File does not exist: %s" (url-recreate-url url)))
  148. ;; Need to figure out the content-type from the real extension,
  149. ;; not the compressed one.
  150. (setq uncompressed-filename (if (string-match "\\.\\(gz\\|Z\\|z\\)$" filename)
  151. (substring filename 0 (match-beginning 0))
  152. filename))
  153. (setq content-type (mailcap-extension-to-mime
  154. (url-file-extension uncompressed-filename))
  155. content-encoding (case (intern (url-file-extension filename))
  156. ((\.z \.gz) "gzip")
  157. (\.Z "compress")
  158. (\.uue "x-uuencoded")
  159. (\.hqx "x-hqx")
  160. (\.bz2 "x-bzip2")
  161. (otherwise nil)))
  162. (if (file-directory-p filename)
  163. ;; A directory is done the same whether we are local or remote
  164. (url-find-file-dired filename)
  165. (with-current-buffer
  166. (setq buffer (generate-new-buffer " *url-file*"))
  167. (mm-disable-multibyte)
  168. (setq url-current-object url)
  169. (insert "Content-type: " (or content-type "application/octet-stream") "\n")
  170. (if content-encoding
  171. (insert "Content-transfer-encoding: " content-encoding "\n"))
  172. (if (url-file-host-is-local-p (url-host url))
  173. ;; Local files are handled slightly oddly
  174. (if (featurep 'ange-ftp)
  175. (url-file-asynch-callback nil nil
  176. filename
  177. (current-buffer)
  178. callback cbargs)
  179. (url-file-asynch-callback nil nil nil
  180. filename
  181. (current-buffer)
  182. callback cbargs))
  183. ;; FTP handling
  184. (let ((new (make-temp-file
  185. (format "url-tmp.%d" (user-real-uid)))))
  186. (if (featurep 'ange-ftp)
  187. (ange-ftp-copy-file-internal filename (expand-file-name new) t
  188. nil t
  189. (list 'url-file-asynch-callback
  190. new (current-buffer)
  191. callback cbargs)
  192. t)
  193. (when (featurep 'xemacs)
  194. (autoload 'efs-copy-file-internal "efs")
  195. (efs-copy-file-internal filename (efs-ftp-path filename)
  196. new (efs-ftp-path new)
  197. t nil 0
  198. (list 'url-file-asynch-callback
  199. new (current-buffer)
  200. callback cbargs)
  201. 0 nil)))))))
  202. buffer))
  203. (defmacro url-file-create-wrapper (method args)
  204. `(defalias ',(intern (format "url-ftp-%s" method))
  205. (defun ,(intern (format "url-file-%s" method)) ,args
  206. ,(format "FTP/FILE URL wrapper around `%s' call." method)
  207. (setq url (url-file-build-filename url))
  208. (and url (,method ,@(remove '&rest (remove '&optional args)))))))
  209. (url-file-create-wrapper file-exists-p (url))
  210. (url-file-create-wrapper file-attributes (url &optional id-format))
  211. (url-file-create-wrapper file-symlink-p (url))
  212. (url-file-create-wrapper file-readable-p (url))
  213. (url-file-create-wrapper file-writable-p (url))
  214. (url-file-create-wrapper file-executable-p (url))
  215. (url-file-create-wrapper directory-files (url &optional full match nosort))
  216. (url-file-create-wrapper file-truename (url &optional counter prev-dirs))
  217. (provide 'url-file)
  218. ;;; url-file.el ends here