Mercurial > emacs
annotate lisp/url/url-cache.el @ 111360:3c958232fff8
Backport Bug#6765 fix from trunk.
* mouse.el (mouse-fixup-help-message): Match "mouse-2" only at the
beginning of the string. Use `string-match-p'. (Bug#6765)
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Thu, 04 Nov 2010 15:27:46 -0400 |
parents | 4bca1a43c0d3 |
children | b799d38f522a 376148b31b5e |
rev | line source |
---|---|
54695 | 1 ;;; url-cache.el --- Uniform Resource Locator retrieval tool |
57612 | 2 |
110522
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004, 2005, 2006, 2007, 2008, |
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
4 ;; 2009, 2010 Free Software Foundation, Inc. |
57612 | 5 |
54695 | 6 ;; Keywords: comm, data, processes, hypermedia |
7 | |
57612 | 8 ;; This file is part of GNU Emacs. |
9 | |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
57612 | 11 ;; it under the terms of the GNU General Public License as published by |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
13 ;; (at your option) any later version. |
57612 | 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 | |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79720
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
57612 | 22 |
23 ;;; Code: | |
24 | |
54695 | 25 (require 'url-parse) |
54793
4051557a5374
(url-util): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
26 (require 'url-util) |
67974 | 27 (require 'url) ;E.g. for url-configuration-directory. |
54695 | 28 |
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) | |
34 | |
35 ;; Cache manager | |
36 (defun url-cache-file-writable-p (file) | |
37 "Follows the documentation of `file-writable-p', unlike `file-writable-p'." | |
38 (and (file-writable-p file) | |
39 (if (file-exists-p file) | |
40 (not (file-directory-p file)) | |
41 (file-directory-p (file-name-directory file))))) | |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
42 |
54695 | 43 (defun url-cache-prepare (file) |
44 "Makes it possible to cache data in FILE. | |
45 Creates any necessary parent directories, deleting any non-directory files | |
46 that would stop this. Returns nil if parent directories can not be | |
47 created. If FILE already exists as a non-directory, it changes | |
48 permissions of FILE or deletes FILE to make it possible to write a new | |
96486
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
49 version of FILE. Returns nil if this can not be done, or if FILE already |
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
50 exists as a directory. Otherwise, returns t, indicating that |
54695 | 51 FILE can be created or overwritten." |
52 (cond | |
53 ((url-cache-file-writable-p file) | |
54 t) | |
55 ((file-directory-p file) | |
56 nil) | |
57 (t | |
58 (condition-case () | |
59 (or (make-directory (file-name-directory file) t) t) | |
60 (error nil))))) | |
61 | |
62 ;;;###autoload | |
63 (defun url-store-in-cache (&optional buff) | |
64 "Store buffer BUFF in the cache." | |
110343
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
65 (with-current-buffer (get-buffer (or buff (current-buffer))) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
66 (let ((fname (url-cache-create-filename (url-view-url t)))) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
67 (if (url-cache-prepare fname) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
68 (let ((coding-system-for-write 'binary)) |
94013d67543b
* lisp/url/url-cache (url-store-in-cache): Make `buff' argument really optional.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
69 (write-region (point-min) (point-max) fname nil 5)))))) |
64748
875dcc490074
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64084
diff
changeset
|
70 |
54695 | 71 ;;;###autoload |
72 (defun url-is-cached (url) | |
110522
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
73 "Return non-nil if the URL is cached. |
4bca1a43c0d3
* lisp/url/url-cache.el (url-is-cached): Doc fix.
Glenn Morris <rgm@gnu.org>
parents:
110343
diff
changeset
|
74 The actual return value is the last modification time of the cache file." |
54695 | 75 (let* ((fname (url-cache-create-filename url)) |
76 (attribs (file-attributes fname))) | |
77 (and fname ; got a filename | |
78 (file-exists-p fname) ; file exists | |
79 (not (eq (nth 0 attribs) t)) ; Its not a directory | |
80 (nth 5 attribs)))) ; Can get last mod-time | |
81 | |
82 (defun url-cache-create-filename-human-readable (url) | |
96486
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
83 "Return a filename in the local cache for URL." |
54695 | 84 (if url |
85 (let* ((url (if (vectorp url) (url-recreate-url url) url)) | |
86 (urlobj (url-generic-parse-url url)) | |
87 (protocol (url-type urlobj)) | |
88 (hostname (url-host urlobj)) | |
89 (host-components | |
90 (cons | |
91 (user-real-login-name) | |
92 (cons (or protocol "file") | |
93 (reverse (split-string (or hostname "localhost") | |
94 (eval-when-compile | |
95 (regexp-quote "."))))))) | |
96 (fname (url-filename urlobj))) | |
97 (if (and fname (/= (length fname) 0) (= (aref fname 0) ?/)) | |
98 (setq fname (substring fname 1 nil))) | |
99 (if fname | |
100 (let ((slash nil)) | |
101 (setq fname | |
102 (mapconcat | |
103 (function | |
104 (lambda (x) | |
105 (cond | |
106 ((and (= ?/ x) slash) | |
107 (setq slash nil) | |
108 "%2F") | |
109 ((= ?/ x) | |
110 (setq slash t) | |
111 "/") | |
112 (t | |
113 (setq slash nil) | |
114 (char-to-string x))))) fname "")))) | |
115 | |
116 (setq fname (and fname | |
117 (mapconcat | |
118 (function (lambda (x) | |
119 (if (= x ?~) "" (char-to-string x)))) | |
120 fname "")) | |
121 fname (cond | |
122 ((null fname) nil) | |
123 ((or (string= "" fname) (string= "/" fname)) | |
124 url-directory-index-file) | |
125 ((= (string-to-char fname) ?/) | |
126 (if (string= (substring fname -1 nil) "/") | |
127 (concat fname url-directory-index-file) | |
128 (substring fname 1 nil))) | |
129 (t | |
130 (if (string= (substring fname -1 nil) "/") | |
131 (concat fname url-directory-index-file) | |
132 fname)))) | |
133 (and fname | |
134 (expand-file-name fname | |
135 (expand-file-name | |
136 (mapconcat 'identity host-components "/") | |
137 url-cache-directory)))))) | |
138 | |
139 (defun url-cache-create-filename-using-md5 (url) | |
140 "Create a cached filename using MD5. | |
54793
4051557a5374
(url-util): Require.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54770
diff
changeset
|
141 Very fast if you have an `md5' primitive function, suitably fast otherwise." |
54695 | 142 (require 'md5) |
143 (if url | |
144 (let* ((url (if (vectorp url) (url-recreate-url url) url)) | |
145 (checksum (md5 url)) | |
146 (urlobj (url-generic-parse-url url)) | |
147 (protocol (url-type urlobj)) | |
148 (hostname (url-host urlobj)) | |
149 (host-components | |
150 (cons | |
151 (user-real-login-name) | |
152 (cons (or protocol "file") | |
153 (nreverse | |
154 (delq nil | |
155 (split-string (or hostname "localhost") | |
156 (eval-when-compile | |
157 (regexp-quote ".")))))))) | |
158 (fname (url-filename urlobj))) | |
159 (and fname | |
160 (expand-file-name checksum | |
161 (expand-file-name | |
162 (mapconcat 'identity host-components "/") | |
163 url-cache-directory)))))) | |
164 | |
165 (defcustom url-cache-creation-function 'url-cache-create-filename-using-md5 | |
166 "*What function to use to create a cached filename." | |
167 :type '(choice (const :tag "MD5 of filename (low collision rate)" | |
168 :value url-cache-create-filename-using-md5) | |
169 (const :tag "Human readable filenames (higher collision rate)" | |
170 :value url-cache-create-filename-human-readable) | |
171 (function :tag "Other")) | |
172 :group 'url-cache) | |
173 | |
174 (defun url-cache-create-filename (url) | |
175 (funcall url-cache-creation-function url)) | |
176 | |
177 ;;;###autoload | |
178 (defun url-cache-extract (fnam) | |
96486
7369ded3b436
Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
94668
diff
changeset
|
179 "Extract FNAM from the local disk cache." |
54695 | 180 (erase-buffer) |
181 (insert-file-contents-literally fnam)) | |
182 | |
183 ;;;###autoload | |
184 (defun url-cache-expired (url mod) | |
78481
bc53aa750f3b
Replace `iff' in doc-strings and comments.
Glenn Morris <rgm@gnu.org>
parents:
78222
diff
changeset
|
185 "Return t if a cached file has expired." |
54695 | 186 (let* ((urlobj (if (vectorp url) url (url-generic-parse-url url))) |
187 (type (url-type urlobj))) | |
188 (cond | |
189 (url-standalone-mode | |
190 (not (file-exists-p (url-cache-create-filename url)))) | |
191 ((string= type "http") | |
192 t) | |
193 ((member type '("file" "ftp")) | |
194 (if (or (equal mod '(0 0)) (not mod)) | |
195 t | |
196 (or (> (nth 0 mod) (nth 0 (current-time))) | |
197 (> (nth 1 mod) (nth 1 (current-time)))))) | |
198 (t nil)))) | |
199 | |
200 (provide 'url-cache) | |
54699 | 201 |
67851
eebc9bf86f06
(url-store-in-cache): Use save-current-buffer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
64748
diff
changeset
|
202 ;; arch-tag: 95b050a6-8e81-4f23-8e63-191b9d1d657c |
57612 | 203 ;;; url-cache.el ends here |