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