*** empty log message ***
[bpt/emacs.git] / lisp / url / url-cache.el
1 ;;; url-cache.el --- Uniform Resource Locator retrieval tool
2 ;; Keywords: comm, data, processes, hypermedia
3
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;;; Copyright (c) 1993 - 1996 by William M. Perry <wmperry@cs.indiana.edu>
6 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
7 ;;;
8 ;;; This file is part of GNU Emacs.
9 ;;;
10 ;;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2, or (at your option)
13 ;;; any later version.
14 ;;;
15 ;;; GNU Emacs is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;;; Boston, MA 02111-1307, USA.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 (require 'url-parse)
26 (require 'url-util)
27
28 (defcustom url-cache-directory
29 (expand-file-name "cache" url-configuration-directory)
30 "*The directory where cache files should be stored."
31 :type 'directory
32 :group 'url-file)
33
34 ;; Cache manager
35 (defun url-cache-file-writable-p (file)
36 "Follows the documentation of `file-writable-p', unlike `file-writable-p'."
37 (and (file-writable-p file)
38 (if (file-exists-p file)
39 (not (file-directory-p file))
40 (file-directory-p (file-name-directory file)))))
41
42 (defun url-cache-prepare (file)
43 "Makes it possible to cache data in FILE.
44 Creates any necessary parent directories, deleting any non-directory files
45 that would stop this. Returns nil if parent directories can not be
46 created. If FILE already exists as a non-directory, it changes
47 permissions of FILE or deletes FILE to make it possible to write a new
48 version of FILE. Returns nil if this can not be done. Returns nil if
49 FILE already exists as a directory. Otherwise, returns t, indicating that
50 FILE can be created or overwritten."
51 (cond
52 ((url-cache-file-writable-p file)
53 t)
54 ((file-directory-p file)
55 nil)
56 (t
57 (condition-case ()
58 (or (make-directory (file-name-directory file) t) t)
59 (error nil)))))
60
61 ;;;###autoload
62 (defun url-store-in-cache (&optional buff)
63 "Store buffer BUFF in the cache."
64 (if (not (and buff (get-buffer buff)))
65 nil
66 (save-excursion
67 (and buff (set-buffer buff))
68 (let* ((fname (url-cache-create-filename (url-view-url t))))
69 (if (url-cache-prepare fname)
70 (let ((coding-system-for-write 'binary))
71 (write-region (point-min) (point-max) fname nil 5)))))))
72
73 ;;;###autoload
74 (defun url-is-cached (url)
75 "Return non-nil if the URL is cached."
76 (let* ((fname (url-cache-create-filename url))
77 (attribs (file-attributes fname)))
78 (and fname ; got a filename
79 (file-exists-p fname) ; file exists
80 (not (eq (nth 0 attribs) t)) ; Its not a directory
81 (nth 5 attribs)))) ; Can get last mod-time
82
83 (defun url-cache-create-filename-human-readable (url)
84 "Return a filename in the local cache for URL"
85 (if url
86 (let* ((url (if (vectorp url) (url-recreate-url url) url))
87 (urlobj (url-generic-parse-url url))
88 (protocol (url-type urlobj))
89 (hostname (url-host urlobj))
90 (host-components
91 (cons
92 (user-real-login-name)
93 (cons (or protocol "file")
94 (reverse (split-string (or hostname "localhost")
95 (eval-when-compile
96 (regexp-quote ".")))))))
97 (fname (url-filename urlobj)))
98 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/))
99 (setq fname (substring fname 1 nil)))
100 (if fname
101 (let ((slash nil))
102 (setq fname
103 (mapconcat
104 (function
105 (lambda (x)
106 (cond
107 ((and (= ?/ x) slash)
108 (setq slash nil)
109 "%2F")
110 ((= ?/ x)
111 (setq slash t)
112 "/")
113 (t
114 (setq slash nil)
115 (char-to-string x))))) fname ""))))
116
117 (setq fname (and fname
118 (mapconcat
119 (function (lambda (x)
120 (if (= x ?~) "" (char-to-string x))))
121 fname ""))
122 fname (cond
123 ((null fname) nil)
124 ((or (string= "" fname) (string= "/" fname))
125 url-directory-index-file)
126 ((= (string-to-char fname) ?/)
127 (if (string= (substring fname -1 nil) "/")
128 (concat fname url-directory-index-file)
129 (substring fname 1 nil)))
130 (t
131 (if (string= (substring fname -1 nil) "/")
132 (concat fname url-directory-index-file)
133 fname))))
134 (and fname
135 (expand-file-name fname
136 (expand-file-name
137 (mapconcat 'identity host-components "/")
138 url-cache-directory))))))
139
140 (defun url-cache-create-filename-using-md5 (url)
141 "Create a cached filename using MD5.
142 Very fast if you have an `md5' primitive function, suitably fast otherwise."
143 (require 'md5)
144 (if url
145 (let* ((url (if (vectorp url) (url-recreate-url url) url))
146 (checksum (md5 url))
147 (urlobj (url-generic-parse-url url))
148 (protocol (url-type urlobj))
149 (hostname (url-host urlobj))
150 (host-components
151 (cons
152 (user-real-login-name)
153 (cons (or protocol "file")
154 (nreverse
155 (delq nil
156 (split-string (or hostname "localhost")
157 (eval-when-compile
158 (regexp-quote "."))))))))
159 (fname (url-filename urlobj)))
160 (and fname
161 (expand-file-name checksum
162 (expand-file-name
163 (mapconcat 'identity host-components "/")
164 url-cache-directory))))))
165
166 (defcustom url-cache-creation-function 'url-cache-create-filename-using-md5
167 "*What function to use to create a cached filename."
168 :type '(choice (const :tag "MD5 of filename (low collision rate)"
169 :value url-cache-create-filename-using-md5)
170 (const :tag "Human readable filenames (higher collision rate)"
171 :value url-cache-create-filename-human-readable)
172 (function :tag "Other"))
173 :group 'url-cache)
174
175 (defun url-cache-create-filename (url)
176 (funcall url-cache-creation-function url))
177
178 ;;;###autoload
179 (defun url-cache-extract (fnam)
180 "Extract FNAM from the local disk cache"
181 (erase-buffer)
182 (insert-file-contents-literally fnam))
183
184 ;;;###autoload
185 (defun url-cache-expired (url mod)
186 "Return t iff a cached file has expired."
187 (let* ((urlobj (if (vectorp url) url (url-generic-parse-url url)))
188 (type (url-type urlobj)))
189 (cond
190 (url-standalone-mode
191 (not (file-exists-p (url-cache-create-filename url))))
192 ((string= type "http")
193 t)
194 ((member type '("file" "ftp"))
195 (if (or (equal mod '(0 0)) (not mod))
196 t
197 (or (> (nth 0 mod) (nth 0 (current-time)))
198 (> (nth 1 mod) (nth 1 (current-time))))))
199 (t nil))))
200
201 (provide 'url-cache)
202
203 ;;; arch-tag: 95b050a6-8e81-4f23-8e63-191b9d1d657c