url-file.el 8.8 KB

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