1 ;;; url-file.el --- File retrieval code
3 ;; Copyright (c) 1996 - 1999,2004 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes
7 ;; This file is part of GNU Emacs.
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)
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.
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.
28 (eval-when-compile (require 'cl
))
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
)
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
46 (compressed-extensions '("" ".gz" ".z" ".Z" ".bz2"))
48 (while (and compressed-extensions
(not found
))
49 (if (file-exists-p (setq scratch
(concat fname
(pop compressed-extensions
))))
50 (setq found scratch
)))
53 (defun url-file-host-is-local-p (host)
54 "Return t iff HOST references our local machine."
55 (let ((case-fold-search t
))
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 ".")
64 (substring (system-name) 0
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
75 (let ((size (nth 7 (file-attributes name
))))
76 (with-current-buffer buff
77 (goto-char (point-max))
79 (insert (format "Content-length: %d\n" size
)))
81 (insert-file-contents-literally name
)
82 (if (not (url-file-host-is-local-p (url-host url-current-object
)))
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
))
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
))
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
))
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
))
120 ;; This makes sure that directories have a trailing directory
121 ;; separator on them so URL expansion works right.
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
)))
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
))
143 ;; Find the (possibly compressed) file
144 (setq filename
(url-file-find-possibly-compressed-file filename
))
148 (defun url-file (url callback cbargs
)
149 "Handle file: and ftp: URLs."
151 (uncompressed-filename nil
)
153 (content-encoding nil
)
154 (coding-system-for-read 'binary
))
156 (setq filename
(url-file-build-filename url
))
159 (error "File does not exist: %s" (url-recreate-url url
)))
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))
166 (setq content-type
(mailcap-extension-to-mime
167 (url-file-extension uncompressed-filename
))
168 content-encoding
(case (intern (url-file-extension filename
))
171 (\.uue
"x-uuencoded")
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
)
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")
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
193 (url-file-asynch-callback nil nil nil
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
205 (list 'url-file-asynch-callback
209 (autoload 'efs-copy-file-internal
"efs")
210 (efs-copy-file-internal filename
(efs-ftp-path filename
)
211 new
(efs-ftp-path new
)
213 (list 'url-file-asynch-callback
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
)))))))
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
)
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
)))
241 ;; arch-tag: 010e914a-7313-494b-8a8c-6495a862157d
242 ;;; url-file.el ends here