* lisp/url/url-cache.el (url-cache-expire-time): Doc fix.
[emacs.git] / lisp / url / url-cache.el
blob418a8e22672980481a02ad8a7bc3ffae1be413d9
1 ;;; url-cache.el --- Uniform Resource Locator retrieval tool
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 Free Software Foundation, Inc.
6 ;; Keywords: comm, data, processes, hypermedia
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'url-parse)
26 (require 'url-util)
27 (require 'url) ;E.g. for url-configuration-directory.
29 (defcustom url-cache-directory
30 (expand-file-name "cache" url-configuration-directory)
31 "The directory where cache files should be stored."
32 :type 'directory
33 :group 'url-file)
35 (defcustom url-cache-expire-time 3600
36 "Default maximum time in seconds before cache files expire.
37 Used by the function `url-cache-expired'."
38 :version "24.1"
39 :type 'integer
40 :group 'url-cache)
42 ;; Cache manager
43 (defun url-cache-file-writable-p (file)
44 "Follows the documentation of `file-writable-p', unlike `file-writable-p'."
45 (and (file-writable-p file)
46 (if (file-exists-p file)
47 (not (file-directory-p file))
48 (file-directory-p (file-name-directory file)))))
50 (defun url-cache-prepare (file)
51 "Makes it possible to cache data in FILE.
52 Creates any necessary parent directories, deleting any non-directory files
53 that would stop this. Returns nil if parent directories can not be
54 created. If FILE already exists as a non-directory, it changes
55 permissions of FILE or deletes FILE to make it possible to write a new
56 version of FILE. Returns nil if this can not be done, or if FILE already
57 exists as a directory. Otherwise, returns t, indicating that
58 FILE can be created or overwritten."
59 (cond
60 ((url-cache-file-writable-p file)
62 ((file-directory-p file)
63 nil)
65 (condition-case ()
66 (or (make-directory (file-name-directory file) t) t)
67 (error nil)))))
69 ;;;###autoload
70 (defun url-store-in-cache (&optional buff)
71 "Store buffer BUFF in the cache."
72 (with-current-buffer (get-buffer (or buff (current-buffer)))
73 (let ((fname (url-cache-create-filename (url-view-url t))))
74 (if (url-cache-prepare fname)
75 (let ((coding-system-for-write 'binary))
76 (write-region (point-min) (point-max) fname nil 5))))))
78 (defun url-fetch-from-cache (url)
79 "Fetch URL from cache and return a buffer with the content."
80 (with-current-buffer (generate-new-buffer " *temp*")
81 (url-cache-extract (url-cache-create-filename url))
82 (current-buffer)))
84 ;;;###autoload
85 (defun url-is-cached (url)
86 "Return non-nil if the URL is cached."
87 (let* ((fname (url-cache-create-filename url))
88 (attribs (file-attributes fname)))
89 (and fname ; got a filename
90 (file-exists-p fname) ; file exists
91 (not (eq (nth 0 attribs) t)) ; Its not a directory
92 (nth 5 attribs)))) ; Can get last mod-time
94 (defun url-cache-create-filename-human-readable (url)
95 "Return a filename in the local cache for URL."
96 (if url
97 (let* ((url (if (vectorp url) (url-recreate-url url) url))
98 (urlobj (url-generic-parse-url url))
99 (protocol (url-type urlobj))
100 (hostname (url-host urlobj))
101 (host-components
102 (cons
103 (user-real-login-name)
104 (cons (or protocol "file")
105 (reverse (split-string (or hostname "localhost")
106 (eval-when-compile
107 (regexp-quote ".")))))))
108 (fname (url-filename urlobj)))
109 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/))
110 (setq fname (substring fname 1 nil)))
111 (if fname
112 (let ((slash nil))
113 (setq fname
114 (mapconcat
115 (function
116 (lambda (x)
117 (cond
118 ((and (= ?/ x) slash)
119 (setq slash nil)
120 "%2F")
121 ((= ?/ x)
122 (setq slash t)
123 "/")
125 (setq slash nil)
126 (char-to-string x))))) fname ""))))
128 (setq fname (and fname
129 (mapconcat
130 (function (lambda (x)
131 (if (= x ?~) "" (char-to-string x))))
132 fname ""))
133 fname (cond
134 ((null fname) nil)
135 ((or (string= "" fname) (string= "/" fname))
136 url-directory-index-file)
137 ((= (string-to-char fname) ?/)
138 (if (string= (substring fname -1 nil) "/")
139 (concat fname url-directory-index-file)
140 (substring fname 1 nil)))
142 (if (string= (substring fname -1 nil) "/")
143 (concat fname url-directory-index-file)
144 fname))))
145 (and fname
146 (expand-file-name fname
147 (expand-file-name
148 (mapconcat 'identity host-components "/")
149 url-cache-directory))))))
151 (defun url-cache-create-filename-using-md5 (url)
152 "Create a cached filename using MD5.
153 Very fast if you have an `md5' primitive function, suitably fast otherwise."
154 (require 'md5)
155 (if url
156 (let* ((url (if (vectorp url) (url-recreate-url url) url))
157 (checksum (md5 url))
158 (urlobj (url-generic-parse-url url))
159 (protocol (url-type urlobj))
160 (hostname (url-host urlobj))
161 (host-components
162 (cons
163 (user-real-login-name)
164 (cons (or protocol "file")
165 (nreverse
166 (delq nil
167 (split-string (or hostname "localhost")
168 (eval-when-compile
169 (regexp-quote "."))))))))
170 (fname (url-filename urlobj)))
171 (and fname
172 (expand-file-name checksum
173 (expand-file-name
174 (mapconcat 'identity host-components "/")
175 url-cache-directory))))))
177 (defcustom url-cache-creation-function 'url-cache-create-filename-using-md5
178 "What function to use to create a cached filename."
179 :type '(choice (const :tag "MD5 of filename (low collision rate)"
180 :value url-cache-create-filename-using-md5)
181 (const :tag "Human readable filenames (higher collision rate)"
182 :value url-cache-create-filename-human-readable)
183 (function :tag "Other"))
184 :group 'url-cache)
186 (defun url-cache-create-filename (url)
187 (funcall url-cache-creation-function url))
189 ;;;###autoload
190 (defun url-cache-extract (fnam)
191 "Extract FNAM from the local disk cache."
192 (erase-buffer)
193 (insert-file-contents-literally fnam))
195 (defun url-cache-expired (url &optional expire-time)
196 "Return non-nil if a cached URL is older than EXPIRE-TIME seconds.
197 The default value of EXPIRE-TIME is `url-cache-expire-time'.
198 If `url-standalone-mode' is non-nil, cached items never expire."
199 (if url-standalone-mode
200 (not (file-exists-p (url-cache-create-filename url)))
201 (let ((cache-time (url-is-cached url)))
202 (or (not cache-time)
203 (time-less-p
204 (time-add
205 cache-time
206 (seconds-to-time (or expire-time url-cache-expire-time)))
207 (current-time))))))
209 (provide 'url-cache)
211 ;; arch-tag: 95b050a6-8e81-4f23-8e63-191b9d1d657c
212 ;;; url-cache.el ends here