Mercurial > emacs
annotate lisp/url/url-file.el @ 86762:8f0e97a8df34
Move to etc/nxml
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Wed, 28 Nov 2007 05:11:26 +0000 |
parents | 22dc0bc9daf8 |
children | 7193e8ecbb2e |
rev | line source |
---|---|
54695 | 1 ;;; url-file.el --- File retrieval code |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
2 |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, |
75347 | 4 ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
5 |
54695 | 6 ;; Keywords: comm, data, processes |
7 | |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
9 ;; |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
78222
8932997d0b62
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
12 ;; the Free Software Foundation; either version 3, or (at your option) |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
13 ;; any later version. |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
14 ;; |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
18 ;; GNU General Public License for more details. |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
19 ;; |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64084 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
24 |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
25 ;;; Commentary: |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
26 |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
27 ;;; Code: |
54695 | 28 |
29 (eval-when-compile (require 'cl)) | |
30 (require 'mailcap) | |
31 (require 'url-vars) | |
32 (require 'url-parse) | |
33 (require 'url-dired) | |
34 | |
35 (defconst url-file-default-port 21 "Default FTP port.") | |
36 (defconst url-file-asynchronous-p t "FTP transfers are asynchronous.") | |
37 (defalias 'url-file-expand-file-name 'url-default-expander) | |
38 | |
39 (defun url-file-find-possibly-compressed-file (fname &rest args) | |
40 "Find the exact file referenced by `fname'. | |
41 This tries the common compression extensions, because things like | |
42 ange-ftp and efs are not quite smart enough to realize when a server | |
43 can do automatic decompression for them, and won't find 'foo' if | |
44 'foo.gz' exists, even though the ftp server would happily serve it up | |
45 to them." | |
46 (let ((scratch nil) | |
47 (compressed-extensions '("" ".gz" ".z" ".Z" ".bz2")) | |
48 (found nil)) | |
49 (while (and compressed-extensions (not found)) | |
50 (if (file-exists-p (setq scratch (concat fname (pop compressed-extensions)))) | |
51 (setq found scratch))) | |
52 found)) | |
53 | |
54 (defun url-file-host-is-local-p (host) | |
78481
bc53aa750f3b
Replace `iff' in doc-strings and comments.
Glenn Morris <rgm@gnu.org>
parents:
78222
diff
changeset
|
55 "Return t if HOST references our local machine." |
54695 | 56 (let ((case-fold-search t)) |
57 (or | |
58 (null host) | |
59 (string= "" host) | |
60 (equal (downcase host) (downcase (system-name))) | |
61 (and (string-match "^localhost$" host) t) | |
62 (and (not (string-match (regexp-quote ".") host)) | |
63 (equal (downcase host) (if (string-match (regexp-quote ".") | |
64 (system-name)) | |
65 (substring (system-name) 0 | |
66 (match-beginning 0)) | |
67 (system-name))))))) | |
68 | |
69 (defun url-file-asynch-callback (x y name buff func args &optional efs) | |
70 (if (not (featurep 'ange-ftp)) | |
71 ;; EFS passes us an extra argument | |
72 (setq name buff | |
73 buff func | |
74 func args | |
75 args efs)) | |
76 (let ((size (nth 7 (file-attributes name)))) | |
63379
70cb4d2371b4
(url-file, url-file-asynch-callback): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57427
diff
changeset
|
77 (with-current-buffer buff |
54695 | 78 (goto-char (point-max)) |
79 (if (/= -1 size) | |
80 (insert (format "Content-length: %d\n" size))) | |
81 (insert "\n") | |
82 (insert-file-contents-literally name) | |
83 (if (not (url-file-host-is-local-p (url-host url-current-object))) | |
84 (condition-case () | |
85 (delete-file name) | |
86 (error nil))) | |
87 (apply func args)))) | |
88 | |
86243
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
83823
diff
changeset
|
89 (declare-function ange-ftp-set-passwd "../net/ange-ftp" (host user passwd)) |
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
83823
diff
changeset
|
90 (declare-function ange-ftp-copy-file-internal "../net/ange-ftp" |
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
83823
diff
changeset
|
91 (filename newname ok-if-already-exists |
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
83823
diff
changeset
|
92 keep-date &optional msg cont nowait)) |
86265
22dc0bc9daf8
* frame.el (msdos-mouse-p):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
86247
diff
changeset
|
93 (declare-function url-generate-unique-filename "url-util" (&optional fmt)) |
86243
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
83823
diff
changeset
|
94 |
54695 | 95 (defun url-file-build-filename (url) |
96 (if (not (vectorp url)) | |
97 (setq url (url-generic-parse-url url))) | |
98 (let* ((user (url-user url)) | |
99 (pass (url-password url)) | |
100 (port (url-port url)) | |
101 (host (url-host url)) | |
102 (site (if (and port (/= port 21)) | |
103 (if (featurep 'ange-ftp) | |
104 (format "%s %d" host port) | |
105 ;; This works in Emacs 21's ange-ftp too. | |
106 (format "%s#%d" host port)) | |
107 host)) | |
108 (file (url-unhex-string (url-filename url))) | |
109 (filename (if (or user (not (url-file-host-is-local-p host))) | |
110 (concat "/" (or user "anonymous") "@" site ":" file) | |
111 (if (and (memq system-type | |
112 '(emx ms-dos windows-nt ms-windows)) | |
113 (string-match "^/[a-zA-Z]:/" file)) | |
114 (substring file 1) | |
115 file))) | |
116 pos-index) | |
117 | |
118 (and user pass | |
119 (cond | |
120 ((featurep 'ange-ftp) | |
121 (ange-ftp-set-passwd host user pass)) | |
122 ((or (featurep 'efs) (featurep 'efs-auto)) | |
123 (efs-set-passwd host user pass)) | |
124 (t | |
125 nil))) | |
126 | |
127 ;; This makes sure that directories have a trailing directory | |
128 ;; separator on them so URL expansion works right. | |
129 ;; | |
130 ;; FIXME? What happens if the remote system doesn't use our local | |
131 ;; directory-sep-char as its separator? Would it be safer to just | |
132 ;; use '/' unconditionally and rely on the FTP server to | |
133 ;; straighten it out for us? | |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
134 ;; (if (and (file-directory-p filename) |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
135 ;; (not (string-match (format "%c$" directory-sep-char) filename))) |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78481
diff
changeset
|
136 ;; (setf (url-filename url) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78481
diff
changeset
|
137 ;; (format "%s%c" filename directory-sep-char))) |
54695 | 138 (if (and (file-directory-p filename) |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
139 (not (string-match "/\\'" filename))) |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78481
diff
changeset
|
140 (setf (url-filename url) (format "%s/" filename))) |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
141 |
54695 | 142 |
143 ;; If it is a directory, look for an index file first. | |
144 (if (and (file-directory-p filename) | |
145 url-directory-index-file | |
146 (setq pos-index (expand-file-name url-directory-index-file filename)) | |
147 (file-exists-p pos-index) | |
148 (file-readable-p pos-index)) | |
149 (setq filename pos-index)) | |
150 | |
151 ;; Find the (possibly compressed) file | |
152 (setq filename (url-file-find-possibly-compressed-file filename)) | |
153 filename)) | |
154 | |
155 ;;;###autoload | |
156 (defun url-file (url callback cbargs) | |
157 "Handle file: and ftp: URLs." | |
158 (let* ((buffer nil) | |
159 (uncompressed-filename nil) | |
160 (content-type nil) | |
161 (content-encoding nil) | |
162 (coding-system-for-read 'binary)) | |
163 | |
164 (setq filename (url-file-build-filename url)) | |
165 | |
166 (if (not filename) | |
167 (error "File does not exist: %s" (url-recreate-url url))) | |
168 | |
169 ;; Need to figure out the content-type from the real extension, | |
170 ;; not the compressed one. | |
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)) | |
176 content-encoding (case (intern (url-file-extension filename)) | |
177 ((\.z \.gz) "gzip") | |
178 (\.Z "compress") | |
179 (\.uue "x-uuencoded") | |
180 (\.hqx "x-hqx") | |
181 (\.bz2 "x-bzip2") | |
182 (otherwise nil))) | |
183 | |
184 (if (file-directory-p filename) | |
185 ;; A directory is done the same whether we are local or remote | |
186 (url-find-file-dired filename) | |
63379
70cb4d2371b4
(url-file, url-file-asynch-callback): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57427
diff
changeset
|
187 (with-current-buffer |
70cb4d2371b4
(url-file, url-file-asynch-callback): Use with-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57427
diff
changeset
|
188 (setq buffer (generate-new-buffer " *url-file*")) |
54695 | 189 (mm-disable-multibyte) |
190 (setq url-current-object url) | |
191 (insert "Content-type: " (or content-type "application/octet-stream") "\n") | |
192 (if content-encoding | |
193 (insert "Content-transfer-encoding: " content-encoding "\n")) | |
194 (if (url-file-host-is-local-p (url-host url)) | |
195 ;; Local files are handled slightly oddly | |
196 (if (featurep 'ange-ftp) | |
197 (url-file-asynch-callback nil nil | |
198 filename | |
199 (current-buffer) | |
200 callback cbargs) | |
201 (url-file-asynch-callback nil nil nil | |
202 filename | |
203 (current-buffer) | |
204 callback cbargs)) | |
205 ;; FTP handling | |
206 (let* ((extension (url-file-extension filename)) | |
207 (new (url-generate-unique-filename | |
208 (and (> (length extension) 0) | |
209 (concat "%s." extension))))) | |
210 (if (featurep 'ange-ftp) | |
211 (ange-ftp-copy-file-internal filename (expand-file-name new) t | |
212 nil t | |
213 (list 'url-file-asynch-callback | |
214 new (current-buffer) | |
215 callback cbargs) | |
216 t) | |
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)))))) | |
225 buffer)) | |
226 | |
227 (defmacro url-file-create-wrapper (method args) | |
54930
ca220b74ea4b
(url-file-create-wrapper): Use new backquote syntax.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54796
diff
changeset
|
228 `(defalias ',(intern (format "url-ftp-%s" method)) |
ca220b74ea4b
(url-file-create-wrapper): Use new backquote syntax.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54796
diff
changeset
|
229 (defun ,(intern (format "url-file-%s" method)) ,args |
ca220b74ea4b
(url-file-create-wrapper): Use new backquote syntax.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54796
diff
changeset
|
230 ,(format "FTP/FILE URL wrapper around `%s' call." method) |
ca220b74ea4b
(url-file-create-wrapper): Use new backquote syntax.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54796
diff
changeset
|
231 (setq url (url-file-build-filename url)) |
ca220b74ea4b
(url-file-create-wrapper): Use new backquote syntax.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54796
diff
changeset
|
232 (and url (,method ,@(remove '&rest (remove '&optional args))))))) |
54695 | 233 |
234 (url-file-create-wrapper file-exists-p (url)) | |
54930
ca220b74ea4b
(url-file-create-wrapper): Use new backquote syntax.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54796
diff
changeset
|
235 (url-file-create-wrapper file-attributes (url &optional id-format)) |
54695 | 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)) | |
66225 | 240 (url-file-create-wrapper directory-files (url &optional full match nosort)) |
241 (url-file-create-wrapper file-truename (url &optional counter prev-dirs)) | |
54695 | 242 |
243 (provide 'url-file) | |
54699 | 244 |
54796
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
245 ;; arch-tag: 010e914a-7313-494b-8a8c-6495a862157d |
351fde140ac4
(url-file-build-filename): Don't use directory-sep-char.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
246 ;;; url-file.el ends here |