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