Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / gnus-cache.el
blobfa3df7b14aac830ac917ed0fd31b2bfb0721e185
1 ;;; gnus-cache.el --- cache interface for Gnus
3 ;; Copyright (C) 1995-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (require 'gnus)
30 (require 'gnus-sum)
32 (eval-when-compile
33 (unless (fboundp 'gnus-agent-load-alist)
34 (defun gnus-agent-load-alist (group))))
36 (defcustom gnus-cache-active-file
37 (expand-file-name "active" gnus-cache-directory)
38 "The cache active file."
39 :group 'gnus-cache
40 :type 'file)
42 (defcustom gnus-cache-enter-articles '(ticked dormant)
43 "Classes of articles to enter into the cache."
44 :group 'gnus-cache
45 :type '(set (const ticked) (const dormant) (const unread) (const read)))
47 (defcustom gnus-cache-remove-articles '(read)
48 "Classes of articles to remove from the cache."
49 :group 'gnus-cache
50 :type '(set (const ticked) (const dormant) (const unread) (const read)))
52 (defcustom gnus-cacheable-groups nil
53 "Groups that match this regexp will be cached.
55 If you only want to cache your nntp groups, you could set this
56 variable to \"^nntp\".
58 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
59 it's not cached."
60 :group 'gnus-cache
61 :type '(choice (const :tag "off" nil)
62 regexp))
64 (defcustom gnus-uncacheable-groups nil
65 "Groups that match this regexp will not be cached.
67 If you want to avoid caching your nnml groups, you could set this
68 variable to \"^nnml\".
70 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
71 it's not cached."
72 :group 'gnus-cache
73 :type '(choice (const :tag "off" nil)
74 regexp))
76 (defvar gnus-cache-overview-coding-system 'raw-text
77 "Coding system used on Gnus cache files.")
79 (defvar gnus-cache-coding-system 'raw-text
80 "Coding system used on Gnus cache files.")
84 ;;; Internal variables.
86 (defvar gnus-cache-removable-articles nil)
87 (defvar gnus-cache-buffer nil)
88 (defvar gnus-cache-active-hashtb nil)
89 (defvar gnus-cache-active-altered nil)
90 (defvar gnus-cache-total-fetched-hashtb nil)
92 (declare-function nnvirtual-find-group-art "nnvirtual" (group article))
94 (autoload 'nnml-generate-nov-databases-directory "nnml")
95 (autoload 'nnvirtual-find-group-art "nnvirtual")
99 ;;; Functions called from Gnus.
101 (defun gnus-cache-open ()
102 "Initialize the cache."
103 (when (or (file-exists-p gnus-cache-directory)
104 (and gnus-use-cache
105 (not (eq gnus-use-cache 'passive))))
106 (gnus-cache-read-active)))
108 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
109 (ignore-errors
110 (gnus-add-shutdown 'gnus-cache-close 'gnus))
112 (defun gnus-cache-close ()
113 "Shut down the cache."
114 (gnus-cache-write-active)
115 (gnus-cache-save-buffers)
116 (setq gnus-cache-active-hashtb nil))
118 (defun gnus-cache-save-buffers ()
119 ;; save the overview buffer if it exists and has been modified
120 ;; delete empty cache subdirectories
121 (when gnus-cache-buffer
122 (let ((buffer (cdr gnus-cache-buffer))
123 (overview-file (gnus-cache-file-name
124 (car gnus-cache-buffer) ".overview")))
125 ;; write the overview only if it was modified
126 (when (and (buffer-live-p buffer) (buffer-modified-p buffer))
127 (with-current-buffer buffer
128 (if (> (buffer-size) 0)
129 ;; Non-empty overview, write it to a file.
130 (let ((coding-system-for-write
131 gnus-cache-overview-coding-system))
132 (gnus-write-buffer overview-file))
133 (let ((file-name-coding-system nnmail-pathname-coding-system))
134 ;; Empty overview file, remove it
135 (when (file-exists-p overview-file)
136 (delete-file overview-file))
137 ;; If possible, remove group's cache subdirectory.
138 (condition-case nil
139 ;; FIXME: we can detect the error type and warn the user
140 ;; of any inconsistencies (articles w/o nov entries?).
141 ;; for now, just be conservative...delete only if safe -- sj
142 (delete-directory (file-name-directory overview-file))
143 (error))))
145 (gnus-cache-update-overview-total-fetched-for
146 (car gnus-cache-buffer) overview-file)))
147 ;; Kill the buffer -- it's either unmodified or saved.
148 (gnus-kill-buffer buffer)
149 (setq gnus-cache-buffer nil))))
151 (defun gnus-cache-possibly-enter-article
152 (group article ticked dormant unread &optional force)
153 (when (and (or force (not (eq gnus-use-cache 'passive)))
154 (numberp article)
155 (> article 0)) ; This might be a dummy article.
156 (let ((number article)
157 file headers lines-chars
158 (file-name-coding-system nnmail-pathname-coding-system))
159 ;; If this is a virtual group, we find the real group.
160 (when (gnus-virtual-group-p group)
161 (let ((result (nnvirtual-find-group-art
162 (gnus-group-real-name group) article)))
163 (setq group (car result)
164 number (cdr result))))
165 (when (and number
166 (> number 0) ; Reffed article.
167 (or force
168 (and (gnus-cache-fully-p group)
169 (gnus-cache-member-of-class
170 gnus-cache-enter-articles ticked dormant unread)))
171 (not (file-exists-p (setq file (gnus-cache-file-name
172 group number)))))
173 ;; Possibly create the cache directory.
174 (gnus-make-directory (file-name-directory file))
175 ;; Save the article in the cache.
176 (if (file-exists-p file)
177 t ; The article already is saved.
178 (with-current-buffer nntp-server-buffer
179 (require 'gnus-art)
180 (let ((gnus-use-cache nil)
181 (gnus-article-decode-hook nil))
182 (gnus-request-article-this-buffer number group))
183 (when (> (buffer-size) 0)
184 (let ((coding-system-for-write gnus-cache-coding-system))
185 (gnus-write-buffer file)
186 (gnus-cache-update-file-total-fetched-for group file))
187 (setq lines-chars (nnheader-get-lines-and-char))
188 (nnheader-remove-body)
189 (setq headers (nnheader-parse-naked-head))
190 (mail-header-set-number headers number)
191 (mail-header-set-lines headers (car lines-chars))
192 (mail-header-set-chars headers (cadr lines-chars))
193 (gnus-cache-change-buffer group)
194 (set-buffer (cdr gnus-cache-buffer))
195 (goto-char (point-max))
196 (forward-line -1)
197 (while (condition-case ()
198 (when (not (bobp))
199 (> (read (current-buffer)) number))
200 (error
201 ;; The line was malformed, so we just remove it!!
202 (gnus-delete-line)
204 (forward-line -1))
205 (if (bobp)
206 (if (not (eobp))
207 (progn
208 (beginning-of-line)
209 (when (< (read (current-buffer)) number)
210 (forward-line 1)))
211 (beginning-of-line))
212 (forward-line 1))
213 (beginning-of-line)
214 (nnheader-insert-nov headers)
215 ;; Update the active info.
216 (set-buffer gnus-summary-buffer)
217 (gnus-cache-possibly-update-active group (cons number number))
218 (setq gnus-newsgroup-cached
219 (gnus-add-to-sorted-list gnus-newsgroup-cached article))
220 (gnus-summary-update-secondary-mark article))
221 t))))))
223 (defun gnus-cache-enter-remove-article (article)
224 "Mark ARTICLE for later possible removal."
225 (when article
226 (push article gnus-cache-removable-articles)))
228 (defun gnus-cache-possibly-remove-articles ()
229 "Possibly remove some of the removable articles."
230 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
231 (gnus-cache-possibly-remove-articles-1)
232 (let ((arts gnus-cache-removable-articles)
234 (while arts
235 (when (setq ga (nnvirtual-find-group-art
236 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
237 (let ((gnus-cache-removable-articles (list (cdr ga)))
238 (gnus-newsgroup-name (car ga)))
239 (gnus-cache-possibly-remove-articles-1)))))
240 (setq gnus-cache-removable-articles nil)))
242 (defun gnus-cache-possibly-remove-articles-1 ()
243 "Possibly remove some of the removable articles."
244 (when (gnus-cache-fully-p gnus-newsgroup-name)
245 (let ((cache-articles gnus-newsgroup-cached))
246 (gnus-cache-change-buffer gnus-newsgroup-name)
247 (dolist (article gnus-cache-removable-articles)
248 (when (memq article cache-articles)
249 ;; The article was in the cache, so we see whether we are
250 ;; supposed to remove it from the cache.
251 (gnus-cache-possibly-remove-article
252 article (memq article gnus-newsgroup-marked)
253 (memq article gnus-newsgroup-dormant)
254 (or (memq article gnus-newsgroup-unreads)
255 (memq article gnus-newsgroup-unselected))))))
256 ;; The overview file might have been modified, save it
257 ;; safe because we're only called at group exit anyway.
258 (gnus-cache-save-buffers)))
260 (defun gnus-cache-request-article (article group)
261 "Retrieve ARTICLE in GROUP from the cache."
262 (let ((file (gnus-cache-file-name group article))
263 (buffer-read-only nil)
264 (file-name-coding-system nnmail-pathname-coding-system))
265 (when (file-exists-p file)
266 (erase-buffer)
267 (gnus-kill-all-overlays)
268 (let ((coding-system-for-read gnus-cache-coding-system))
269 (insert-file-contents file))
270 t)))
272 (defun gnus-cache-possibly-alter-active (group active)
273 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
274 (when gnus-cache-active-hashtb
275 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
276 (when cache-active
277 (when (< (car cache-active) (car active))
278 (setcar active (car cache-active)))
279 (when (> (cdr cache-active) (cdr active))
280 (setcdr active (cdr cache-active)))))))
282 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
283 "Retrieve the headers for ARTICLES in GROUP."
284 (let ((cached
285 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
286 (if (not cached)
287 ;; No cached articles here, so we just retrieve them
288 ;; the normal way.
289 (let ((gnus-use-cache nil))
290 (gnus-retrieve-headers articles group fetch-old))
291 (let ((uncached-articles (gnus-sorted-difference articles cached))
292 (cache-file (gnus-cache-file-name group ".overview"))
293 type
294 (file-name-coding-system nnmail-pathname-coding-system))
295 ;; We first retrieve all the headers that we don't have in
296 ;; the cache.
297 (let ((gnus-use-cache nil))
298 (when uncached-articles
299 (setq type (and articles
300 (gnus-retrieve-headers
301 uncached-articles group fetch-old)))))
302 (gnus-cache-save-buffers)
303 ;; Then we insert the cached headers.
304 (save-excursion
305 (cond
306 ((not (file-exists-p cache-file))
307 ;; There are no cached headers.
308 type)
309 ((null type)
310 ;; There were no uncached headers (or retrieval was
311 ;; unsuccessful), so we use the cached headers exclusively.
312 (set-buffer nntp-server-buffer)
313 (erase-buffer)
314 (let ((coding-system-for-read
315 gnus-cache-overview-coding-system))
316 (insert-file-contents cache-file))
317 'nov)
318 ((eq type 'nov)
319 ;; We have both cached and uncached NOV headers, so we
320 ;; braid them.
321 (gnus-cache-braid-nov group cached)
322 type)
324 ;; We braid HEADs.
325 (gnus-cache-braid-heads group (gnus-sorted-intersection
326 cached articles))
327 type)))))))
329 (defun gnus-cache-enter-article (&optional n)
330 "Enter the next N articles into the cache.
331 If not given a prefix, use the process marked articles instead.
332 Returns the list of articles entered."
333 (interactive "P")
334 (let (out)
335 (dolist (article (gnus-summary-work-articles n))
336 (gnus-summary-remove-process-mark article)
337 (if (natnump article)
338 (when (gnus-cache-possibly-enter-article
339 gnus-newsgroup-name article
340 nil nil nil t)
341 (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
342 (push article out))
343 (gnus-message 2 "Can't cache article %d" article))
344 (gnus-summary-update-download-mark article)
345 (gnus-summary-update-secondary-mark article))
346 (gnus-summary-next-subject 1)
347 (gnus-summary-position-point)
348 (nreverse out)))
350 (defun gnus-cache-remove-article (&optional n)
351 "Remove the next N articles from the cache.
352 If not given a prefix, use the process marked articles instead.
353 Returns the list of articles removed."
354 (interactive "P")
355 (gnus-cache-change-buffer gnus-newsgroup-name)
356 (let (out)
357 (dolist (article (gnus-summary-work-articles n))
358 (gnus-summary-remove-process-mark article)
359 (when (gnus-cache-possibly-remove-article article nil nil nil t)
360 (when gnus-newsgroup-agentized
361 (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
362 (unless (cdr (assoc article alist))
363 (setq gnus-newsgroup-undownloaded
364 (gnus-add-to-sorted-list
365 gnus-newsgroup-undownloaded article)))))
366 (push article out))
367 (gnus-summary-update-download-mark article)
368 (gnus-summary-update-secondary-mark article))
369 (gnus-summary-next-subject 1)
370 (gnus-summary-position-point)
371 (nreverse out)))
373 (defun gnus-cached-article-p (article)
374 "Say whether ARTICLE is cached in the current group."
375 (memq article gnus-newsgroup-cached))
377 (defun gnus-summary-insert-cached-articles ()
378 "Insert all the articles cached for this group into the current buffer."
379 (interactive)
380 (let ((gnus-verbose (max 6 gnus-verbose)))
381 (cond
382 ((not gnus-newsgroup-cached)
383 (gnus-message 3 "No cached articles for this group"))
384 ;; This is faster if there are few articles to insert.
385 ((< (length gnus-newsgroup-cached) 20)
386 (gnus-summary-goto-subjects gnus-newsgroup-cached))
388 (gnus-summary-include-articles gnus-newsgroup-cached)))))
390 (defun gnus-summary-limit-include-cached ()
391 "Limit the summary buffer to articles that are cached."
392 (interactive)
393 (let ((gnus-verbose (max 6 gnus-verbose)))
394 (if gnus-newsgroup-cached
395 (progn
396 (gnus-summary-limit gnus-newsgroup-cached)
397 (gnus-summary-position-point))
398 (gnus-message 3 "No cached articles for this group"))))
400 ;;; Internal functions.
402 (defun gnus-cache-change-buffer (group)
403 (and gnus-cache-buffer
404 ;; See if the current group's overview cache has been loaded.
405 (or (string= group (car gnus-cache-buffer))
406 ;; Another overview cache is current, save it.
407 (gnus-cache-save-buffers)))
408 ;; if gnus-cache buffer is nil, create it
409 (unless gnus-cache-buffer
410 ;; Create cache buffer
411 (save-excursion
412 (setq gnus-cache-buffer
413 (cons group
414 (set-buffer (gnus-get-buffer-create
415 " *gnus-cache-overview*"))))
416 ;; Insert the contents of this group's cache overview.
417 (erase-buffer)
418 (let ((file (gnus-cache-file-name group ".overview"))
419 (file-name-coding-system nnmail-pathname-coding-system))
420 (when (file-exists-p file)
421 (nnheader-insert-file-contents file)))
422 ;; We have a fresh (empty/just loaded) buffer,
423 ;; mark it as unmodified to save a redundant write later.
424 (set-buffer-modified-p nil))))
426 ;; Return whether an article is a member of a class.
427 (defun gnus-cache-member-of-class (class ticked dormant unread)
428 (or (and ticked (memq 'ticked class))
429 (and dormant (memq 'dormant class))
430 (and unread (memq 'unread class))
431 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
433 (defvar gnus-cache-decoded-group-names nil
434 "Alist of original group names and decoded group names.
435 Decoding is done according to `gnus-group-name-charset-method-alist'
436 or `gnus-group-name-charset-group-alist'.")
438 (defvar gnus-cache-unified-group-names nil
439 "Alist of unified decoded group names and original group names.
440 A group name is decoded according to
441 `gnus-group-name-charset-method-alist' or
442 `gnus-group-name-charset-group-alist' first, and is encoded and
443 decoded again according to `nnmail-pathname-coding-system',
444 `file-name-coding-system', or `default-file-name-coding-system'.
446 It is used when asking for a original group name from a cache
447 directory name, in which non-ASCII characters might have been unified
448 into the ones of a certain charset particularly if the `utf-8' coding
449 system for example was used.")
451 (defun gnus-cache-decoded-group-name (group)
452 "Return a decoded group name of GROUP."
453 (or (cdr (assoc group gnus-cache-decoded-group-names))
454 (let ((decoded (gnus-group-decoded-name group))
455 (coding (or nnmail-pathname-coding-system
456 file-name-coding-system
457 default-file-name-coding-system)))
458 (push (cons group decoded) gnus-cache-decoded-group-names)
459 (push (cons (decode-coding-string
460 (encode-coding-string decoded coding)
461 coding)
462 group)
463 gnus-cache-unified-group-names)
464 decoded)))
466 (defun gnus-cache-file-name (group article)
467 (setq group (gnus-cache-decoded-group-name group))
468 (expand-file-name
469 (if (stringp article) article (int-to-string article))
470 (file-name-as-directory
471 (expand-file-name
472 (nnheader-translate-file-chars
473 (if (gnus-use-long-file-name 'not-cache)
474 group
475 (let ((group (nnheader-replace-duplicate-chars-in-string
476 (nnheader-replace-chars-in-string group ?/ ?_)
477 ?. ?_)))
478 ;; Translate the first colon into a slash.
479 (when (string-match ":" group)
480 (setq group (concat (substring group 0 (match-beginning 0))
481 "/" (substring group (match-end 0)))))
482 (nnheader-replace-chars-in-string group ?. ?/)))
484 gnus-cache-directory))))
486 (defun gnus-cache-update-article (group article)
487 "If ARTICLE is in the cache, remove it and re-enter it."
488 (gnus-cache-change-buffer group)
489 (when (gnus-cache-possibly-remove-article article nil nil nil t)
490 (let ((gnus-use-cache nil))
491 (gnus-cache-possibly-enter-article
492 gnus-newsgroup-name article
493 nil nil nil t))))
495 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
496 &optional force)
497 "Possibly remove ARTICLE from the cache."
498 (let ((group gnus-newsgroup-name)
499 (number article)
500 file
501 (file-name-coding-system nnmail-pathname-coding-system))
502 ;; If this is a virtual group, we find the real group.
503 (when (gnus-virtual-group-p group)
504 (let ((result (nnvirtual-find-group-art
505 (gnus-group-real-name group) article)))
506 (setq group (car result)
507 number (cdr result))))
508 (setq file (gnus-cache-file-name group number))
509 (when (and (file-exists-p file)
510 (or force
511 (gnus-cache-member-of-class
512 gnus-cache-remove-articles ticked dormant unread)))
513 (save-excursion
514 (gnus-cache-update-file-total-fetched-for group file t)
515 (delete-file file)
517 (set-buffer (cdr gnus-cache-buffer))
518 (goto-char (point-min))
519 (when (or (looking-at (concat (int-to-string number) "\t"))
520 (search-forward (concat "\n" (int-to-string number) "\t")
521 (point-max) t))
522 (gnus-delete-line)))
523 (unless (setq gnus-newsgroup-cached
524 (delq article gnus-newsgroup-cached))
525 (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
526 (setq gnus-cache-active-altered t))
527 (gnus-summary-update-secondary-mark article)
528 t)))
530 (defun gnus-cache-articles-in-group (group)
531 "Return a sorted list of cached articles in GROUP."
532 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
533 articles
534 (file-name-coding-system nnmail-pathname-coding-system))
535 (when (file-exists-p dir)
536 (setq articles
537 (sort (mapcar (lambda (name) (string-to-number name))
538 (directory-files dir nil "^[0-9]+$" t))
539 '<))
540 ;; Update the cache active file, just to synch more.
541 (if articles
542 (progn
543 (gnus-cache-update-active group (car articles) t)
544 (gnus-cache-update-active group (car (last articles))))
545 (when (gnus-gethash group gnus-cache-active-hashtb)
546 (gnus-sethash group nil gnus-cache-active-hashtb)
547 (setq gnus-cache-active-altered t)))
548 articles)))
550 (defun gnus-cache-braid-nov (group cached &optional file)
551 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
552 beg end)
553 (gnus-cache-save-buffers)
554 (with-current-buffer cache-buf
555 (erase-buffer)
556 (let ((coding-system-for-read gnus-cache-overview-coding-system)
557 (file-name-coding-system nnmail-pathname-coding-system))
558 (insert-file-contents
559 (or file (gnus-cache-file-name group ".overview"))))
560 (goto-char (point-min))
561 (insert "\n")
562 (goto-char (point-min)))
563 (set-buffer nntp-server-buffer)
564 (goto-char (point-min))
565 (while cached
566 (while (and (not (eobp))
567 (< (read (current-buffer)) (car cached)))
568 (forward-line 1))
569 (beginning-of-line)
570 (set-buffer cache-buf)
571 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
572 nil t)
573 (setq beg (point-at-bol)
574 end (progn (end-of-line) (point)))
575 (setq beg nil))
576 (set-buffer nntp-server-buffer)
577 (when beg
578 (insert-buffer-substring cache-buf beg end)
579 (insert "\n"))
580 (setq cached (cdr cached)))
581 (kill-buffer cache-buf)))
583 (defun gnus-cache-braid-heads (group cached)
584 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
585 (with-current-buffer cache-buf
586 (erase-buffer))
587 (set-buffer nntp-server-buffer)
588 (goto-char (point-min))
589 (dolist (entry cached)
590 (while (and (not (eobp))
591 (looking-at "2.. +\\([0-9]+\\) ")
592 (< (progn (goto-char (match-beginning 1))
593 (read (current-buffer)))
594 entry))
595 (search-forward "\n.\n" nil 'move))
596 (beginning-of-line)
597 (set-buffer cache-buf)
598 (erase-buffer)
599 (let ((coding-system-for-read gnus-cache-coding-system)
600 (file-name-coding-system nnmail-pathname-coding-system))
601 (insert-file-contents (gnus-cache-file-name group entry)))
602 (goto-char (point-min))
603 (insert "220 ")
604 (princ (pop cached) (current-buffer))
605 (insert " Article retrieved.\n")
606 (search-forward "\n\n" nil 'move)
607 (delete-region (point) (point-max))
608 (forward-char -1)
609 (insert ".")
610 (set-buffer nntp-server-buffer)
611 (insert-buffer-substring cache-buf))
612 (kill-buffer cache-buf)))
614 ;;;###autoload
615 (defun gnus-jog-cache ()
616 "Go through all groups and put the articles into the cache.
618 Usage:
619 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
620 (interactive)
621 (let ((gnus-mark-article-hook nil)
622 (gnus-expert-user t)
623 (mail-sources nil)
624 (gnus-use-dribble-file nil)
625 (gnus-novice-user nil)
626 (gnus-large-newsgroup nil))
627 ;; Start Gnus.
628 (gnus)
629 ;; Go through all groups...
630 (gnus-group-mark-buffer)
631 (gnus-group-iterate nil
632 (lambda (group)
633 (let (gnus-auto-select-next)
634 (gnus-summary-read-group group nil t)
635 ;; ... and enter the articles into the cache.
636 (when (eq major-mode 'gnus-summary-mode)
637 (gnus-uu-mark-buffer)
638 (gnus-cache-enter-article)
639 (kill-buffer (current-buffer))))))))
641 (defun gnus-cache-read-active (&optional force)
642 "Read the cache active file."
643 (gnus-make-directory gnus-cache-directory)
644 (if (or (not (file-exists-p gnus-cache-active-file))
645 (zerop (nth 7 (file-attributes gnus-cache-active-file)))
646 force)
647 ;; There is no active file, so we generate one.
648 (gnus-cache-generate-active)
649 ;; We simply read the active file.
650 (save-excursion
651 (gnus-set-work-buffer)
652 (nnheader-insert-file-contents gnus-cache-active-file)
653 (gnus-active-to-gnus-format
654 nil (setq gnus-cache-active-hashtb
655 (gnus-make-hashtable
656 (count-lines (point-min) (point-max)))))
657 (setq gnus-cache-active-altered nil))))
659 (defun gnus-cache-write-active (&optional force)
660 "Write the active hashtb to the active file."
661 (when (or force
662 (and gnus-cache-active-hashtb
663 gnus-cache-active-altered))
664 (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
665 ;; Mark the active hashtb as unaltered.
666 (setq gnus-cache-active-altered nil)))
668 (defun gnus-cache-possibly-update-active (group active)
669 "Update active info bounds of GROUP with ACTIVE if necessary.
670 The update is performed if ACTIVE contains a higher or lower bound
671 than the current."
672 (let ((lower t) (higher t))
673 (if gnus-cache-active-hashtb
674 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
675 (when cache-active
676 (unless (< (car active) (car cache-active))
677 (setq lower nil))
678 (unless (> (cdr active) (cdr cache-active))
679 (setq higher nil))))
680 (gnus-cache-read-active))
681 (when lower
682 (gnus-cache-update-active group (car active) t))
683 (when higher
684 (gnus-cache-update-active group (cdr active)))))
686 (defun gnus-cache-update-active (group number &optional low)
687 "Update the upper bound of the active info of GROUP to NUMBER.
688 If LOW, update the lower bound instead."
689 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
690 (if (null active)
691 ;; We just create a new active entry for this group.
692 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
693 ;; Update the lower or upper bound.
694 (if low
695 (setcar active number)
696 (setcdr active number)))
697 ;; Mark the active hashtb as altered.
698 (setq gnus-cache-active-altered t)))
700 ;;;###autoload
701 (defun gnus-cache-generate-active (&optional directory)
702 "Generate the cache active file."
703 (interactive)
704 (let* ((top (null directory))
705 (directory (expand-file-name (or directory gnus-cache-directory)))
706 (file-name-coding-system nnmail-pathname-coding-system)
707 (files (directory-files directory 'full))
708 (group
709 (if top
711 (string-match
712 (concat "^" (regexp-quote
713 (file-name-as-directory
714 (expand-file-name gnus-cache-directory))))
715 (directory-file-name directory))
716 (nnheader-replace-chars-in-string
717 (substring (directory-file-name directory) (match-end 0))
718 ?/ ?.)))
719 nums alphs)
720 (when top
721 (gnus-message 5 "Generating the cache active file...")
722 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
723 (when (string-match "^\\(nn[^_]+\\)_" group)
724 (setq group (replace-match "\\1:" t nil group)))
725 ;; Separate articles from all other files and directories.
726 (while files
727 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
728 (push (string-to-number (file-name-nondirectory (pop files))) nums)
729 (push (pop files) alphs)))
730 ;; If we have nums, then this is probably a valid group.
731 (when (setq nums (sort nums '<))
732 ;; Use non-decoded group name.
733 ;; FIXME: this is kind of a workaround. The active file should
734 ;; be updated at the time articles are cached. It will make
735 ;; `gnus-cache-unified-group-names' needless.
736 (gnus-sethash (or (cdr (assoc group gnus-cache-unified-group-names))
737 group)
738 (cons (car nums) (gnus-last-element nums))
739 gnus-cache-active-hashtb))
740 ;; Go through all the other files.
741 (dolist (file alphs)
742 (when (and (file-directory-p file)
743 (not (string-match "^\\."
744 (file-name-nondirectory file))))
745 ;; We descend directories.
746 (gnus-cache-generate-active file)))
747 ;; Write the new active file.
748 (when top
749 (gnus-cache-write-active t)
750 (gnus-message 5 "Generating the cache active file...done"))))
752 ;;;###autoload
753 (defun gnus-cache-generate-nov-databases (dir)
754 "Generate NOV files recursively starting in DIR."
755 (interactive (list gnus-cache-directory))
756 (gnus-cache-close)
757 (let ((nnml-generate-active-function 'identity))
758 (nnml-generate-nov-databases-directory dir))
760 (setq gnus-cache-total-fetched-hashtb nil)
762 (gnus-cache-open))
764 (defun gnus-cache-move-cache (dir)
765 "Move the cache tree to somewhere else."
766 (interactive "FMove the cache tree to: ")
767 (rename-file gnus-cache-directory dir))
769 (defun gnus-cache-fully-p (&optional group)
770 "Returns non-nil if the cache should be fully used.
771 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
772 `gnus-uncacheable-groups'."
773 (and gnus-use-cache
774 (not (eq gnus-use-cache 'passive))
775 (if (null group)
777 (and (or (not gnus-cacheable-groups)
778 (string-match gnus-cacheable-groups group))
779 (or (not gnus-uncacheable-groups)
780 (not (string-match gnus-uncacheable-groups group)))))))
782 ;;;###autoload
783 (defun gnus-cache-rename-group (old-group new-group)
784 "Rename OLD-GROUP as NEW-GROUP.
785 Always updates the cache, even when disabled, as the old cache
786 files would corrupt Gnus when the cache was next enabled. It
787 depends on the caller to determine whether group renaming is
788 supported."
789 (let ((old-dir (gnus-cache-file-name old-group ""))
790 (new-dir (gnus-cache-file-name new-group ""))
791 (file-name-coding-system nnmail-pathname-coding-system))
792 (gnus-rename-file old-dir new-dir t))
794 (gnus-cache-rename-group-total-fetched-for old-group new-group)
796 (let ((no-save gnus-cache-active-hashtb))
797 (unless gnus-cache-active-hashtb
798 (gnus-cache-read-active))
799 (let* ((old-group-hash-value
800 (gnus-gethash old-group gnus-cache-active-hashtb))
801 (new-group-hash-value
802 (gnus-gethash new-group gnus-cache-active-hashtb))
803 (delta
804 (or old-group-hash-value new-group-hash-value)))
805 (gnus-sethash new-group old-group-hash-value gnus-cache-active-hashtb)
806 (gnus-sethash old-group nil gnus-cache-active-hashtb)
808 (if no-save
809 (setq gnus-cache-active-altered delta)
810 (gnus-cache-write-active delta)))))
812 ;;;###autoload
813 (defun gnus-cache-delete-group (group)
814 "Delete GROUP from the cache.
815 Always updates the cache, even when disabled, as the old cache
816 files would corrupt gnus when the cache was next enabled.
817 Depends upon the caller to determine whether group deletion is
818 supported."
819 (let ((dir (gnus-cache-file-name group ""))
820 (file-name-coding-system nnmail-pathname-coding-system))
821 (gnus-delete-directory dir))
823 (gnus-cache-delete-group-total-fetched-for group)
825 (let ((no-save gnus-cache-active-hashtb))
826 (unless gnus-cache-active-hashtb
827 (gnus-cache-read-active))
828 (let* ((group-hash-value (gnus-gethash group gnus-cache-active-hashtb)))
829 (gnus-sethash group nil gnus-cache-active-hashtb)
831 (if no-save
832 (setq gnus-cache-active-altered group-hash-value)
833 (gnus-cache-write-active group-hash-value)))))
835 (defvar gnus-cache-inhibit-update-total-fetched-for nil)
836 (defvar gnus-cache-need-update-total-fetched-for nil)
838 (defmacro gnus-cache-with-refreshed-group (group &rest body)
839 `(prog1 (let ((gnus-cache-inhibit-update-total-fetched-for t))
840 ,@body)
841 (when (and gnus-cache-need-update-total-fetched-for
842 (not gnus-cache-inhibit-update-total-fetched-for))
843 (with-current-buffer gnus-group-buffer
844 (setq gnus-cache-need-update-total-fetched-for nil)
845 (gnus-group-update-group ,group t)))))
847 (defun gnus-cache-update-file-total-fetched-for (group file &optional subtract)
848 (when gnus-cache-total-fetched-hashtb
849 (gnus-cache-with-refreshed-group
850 group
851 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
852 (gnus-sethash group (make-vector 2 0)
853 gnus-cache-total-fetched-hashtb)))
854 size)
856 (if file
857 (setq size (or (nth 7 (file-attributes file)) 0))
858 (let* ((file-name-coding-system nnmail-pathname-coding-system)
859 (files (directory-files (gnus-cache-file-name group "")
860 t nil t))
861 file attrs)
862 (setq size 0.0)
863 (while (setq file (pop files))
864 (setq attrs (file-attributes file))
865 (unless (nth 0 attrs)
866 (incf size (float (nth 7 attrs)))))))
868 (setq gnus-cache-need-update-total-fetched-for t)
870 (incf (nth 1 entry) (if subtract (- size) size))))))
872 (defun gnus-cache-update-overview-total-fetched-for (group file)
873 (when gnus-cache-total-fetched-hashtb
874 (gnus-cache-with-refreshed-group
875 group
876 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
877 (gnus-sethash group (make-list 2 0)
878 gnus-cache-total-fetched-hashtb)))
879 (file-name-coding-system nnmail-pathname-coding-system)
880 (size (or (nth 7 (file-attributes
881 (or file
882 (gnus-cache-file-name group ".overview"))))
883 0)))
884 (setq gnus-cache-need-update-total-fetched-for t)
885 (setf (nth 0 entry) size)))))
887 (defun gnus-cache-rename-group-total-fetched-for (old-group new-group)
888 "Record of disk space used by OLD-GROUP now associated with NEW-GROUP."
889 (when gnus-cache-total-fetched-hashtb
890 (let ((entry (gnus-gethash old-group gnus-cache-total-fetched-hashtb)))
891 (gnus-sethash new-group entry gnus-cache-total-fetched-hashtb)
892 (gnus-sethash old-group nil gnus-cache-total-fetched-hashtb))))
894 (defun gnus-cache-delete-group-total-fetched-for (group)
895 "Delete record of disk space used by GROUP being deleted."
896 (when gnus-cache-total-fetched-hashtb
897 (gnus-sethash group nil gnus-cache-total-fetched-hashtb)))
899 (defun gnus-cache-total-fetched-for (group &optional no-inhibit)
900 "Get total disk space used by the cache for the specified GROUP."
901 (unless (equal group "dummy.group")
902 (unless gnus-cache-total-fetched-hashtb
903 (setq gnus-cache-total-fetched-hashtb (gnus-make-hashtable 1024)))
905 (let* ((entry (gnus-gethash group gnus-cache-total-fetched-hashtb)))
906 (if entry
907 (apply '+ entry)
908 (let ((gnus-cache-inhibit-update-total-fetched-for (not no-inhibit)))
910 (gnus-cache-update-overview-total-fetched-for group nil)
911 (gnus-cache-update-file-total-fetched-for group nil)))))))
913 (provide 'gnus-cache)
915 ;;; gnus-cache.el ends here