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