1 ;;; gnus-cache.el --- cache interface for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
34 (unless (fboundp 'gnus-agent-load-alist)
35 (defun gnus-agent-load-alist (group)))
38 (defcustom gnus-cache-active-file
39 (expand-file-name "active" gnus-cache-directory)
40 "*The cache active file."
44 (defcustom gnus-cache-enter-articles '(ticked dormant)
45 "Classes of articles to enter into the cache."
47 :type '(set (const ticked) (const dormant) (const unread) (const read)))
49 (defcustom gnus-cache-remove-articles '(read)
50 "Classes of articles to remove from the cache."
52 :type '(set (const ticked) (const dormant) (const unread) (const read)))
54 (defcustom gnus-cacheable-groups nil
55 "*Groups that match this regexp will be cached.
57 If you only want to cache your nntp groups, you could set this
58 variable to \"^nntp\".
60 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
63 :type '(choice (const :tag "off" nil)
66 (defcustom gnus-uncacheable-groups nil
67 "*Groups that match this regexp will not be cached.
69 If you want to avoid caching your nnml groups, you could set this
70 variable to \"^nnml\".
72 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
75 :type '(choice (const :tag "off" nil)
78 (defvar gnus-cache-overview-coding-system 'raw-text
79 "Coding system used on Gnus cache files.")
81 (defvar gnus-cache-coding-system 'raw-text
82 "Coding system used on Gnus cache files.")
86 ;;; Internal variables.
88 (defvar gnus-cache-removable-articles nil)
89 (defvar gnus-cache-buffer nil)
90 (defvar gnus-cache-active-hashtb nil)
91 (defvar gnus-cache-active-altered nil)
92 (defvar gnus-cache-total-fetched-hashtb nil)
95 (autoload 'nnml-generate-nov-databases-1 "nnml")
96 (autoload 'nnvirtual-find-group-art "nnvirtual"))
100 ;;; Functions called from Gnus.
102 (defun gnus-cache-open ()
103 "Initialize the cache."
104 (when (or (file-exists-p gnus-cache-directory)
106 (not (eq gnus-use-cache 'passive))))
107 (gnus-cache-read-active)))
109 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
111 (gnus-add-shutdown 'gnus-cache-close 'gnus))
113 (defun gnus-cache-close ()
114 "Shut down the cache."
115 (gnus-cache-write-active)
116 (gnus-cache-save-buffers)
117 (setq gnus-cache-active-hashtb nil))
119 (defun gnus-cache-save-buffers ()
120 ;; save the overview buffer if it exists and has been modified
121 ;; delete empty cache subdirectories
122 (when gnus-cache-buffer
123 (let ((buffer (cdr gnus-cache-buffer))
124 (overview-file (gnus-cache-file-name
125 (car gnus-cache-buffer) ".overview")))
126 ;; write the overview only if it was modified
127 (when (and (buffer-live-p buffer) (buffer-modified-p buffer))
128 (with-current-buffer buffer
129 (if (> (buffer-size) 0)
130 ;; Non-empty overview, write it to a file.
131 (let ((coding-system-for-write
132 gnus-cache-overview-coding-system))
133 (gnus-write-buffer overview-file))
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.
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))
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)))
155 (> article 0)) ; This might be a dummy article.
156 (let ((number article)
157 file headers lines-chars)
158 ;; If this is a virtual group, we find the real group.
159 (when (gnus-virtual-group-p group)
160 (let ((result (nnvirtual-find-group-art
161 (gnus-group-real-name group) article)))
162 (setq group (car result)
163 number (cdr result))))
165 (> number 0) ; Reffed article.
167 (and (gnus-cache-fully-p group)
168 (gnus-cache-member-of-class
169 gnus-cache-enter-articles ticked dormant unread)))
170 (not (file-exists-p (setq file (gnus-cache-file-name
172 ;; Possibly create the cache directory.
173 (gnus-make-directory (file-name-directory file))
174 ;; Save the article in the cache.
175 (if (file-exists-p file)
176 t ; The article already is saved.
178 (set-buffer nntp-server-buffer)
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))
197 (while (condition-case ()
199 (> (read (current-buffer)) number))
201 ;; The line was malformed, so we just remove it!!
209 (when (< (read (current-buffer)) number)
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))
223 (defun gnus-cache-enter-remove-article (article)
224 "Mark ARTICLE for later possible removal."
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)
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 (when (file-exists-p file)
266 (gnus-kill-all-overlays)
267 (let ((coding-system-for-read gnus-cache-coding-system))
268 (insert-file-contents file))
271 (defun gnus-cache-possibly-alter-active (group active)
272 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
273 (when gnus-cache-active-hashtb
274 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
276 (when (< (car cache-active) (car active))
277 (setcar active (car cache-active)))
278 (when (> (cdr cache-active) (cdr active))
279 (setcdr active (cdr cache-active)))))))
281 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
282 "Retrieve the headers for ARTICLES in GROUP."
284 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
286 ;; No cached articles here, so we just retrieve them
288 (let ((gnus-use-cache nil))
289 (gnus-retrieve-headers articles group fetch-old))
290 (let ((uncached-articles (gnus-sorted-difference articles cached))
291 (cache-file (gnus-cache-file-name group ".overview"))
293 ;; We first retrieve all the headers that we don't have in
295 (let ((gnus-use-cache nil))
296 (when uncached-articles
297 (setq type (and articles
298 (gnus-retrieve-headers
299 uncached-articles group fetch-old)))))
300 (gnus-cache-save-buffers)
301 ;; Then we insert the cached headers.
304 ((not (file-exists-p cache-file))
305 ;; There are no cached headers.
308 ;; There were no uncached headers (or retrieval was
309 ;; unsuccessful), so we use the cached headers exclusively.
310 (set-buffer nntp-server-buffer)
312 (let ((coding-system-for-read
313 gnus-cache-overview-coding-system))
314 (insert-file-contents cache-file))
317 ;; We have both cached and uncached NOV headers, so we
319 (gnus-cache-braid-nov group cached)
323 (gnus-cache-braid-heads group (gnus-sorted-intersection
327 (defun gnus-cache-enter-article (&optional n)
328 "Enter the next N articles into the cache.
329 If not given a prefix, use the process marked articles instead.
330 Returns the list of articles entered."
333 (dolist (article (gnus-summary-work-articles n))
334 (gnus-summary-remove-process-mark article)
335 (if (natnump article)
336 (when (gnus-cache-possibly-enter-article
337 gnus-newsgroup-name article
339 (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
341 (gnus-message 2 "Can't cache article %d" article))
342 (gnus-summary-update-download-mark article)
343 (gnus-summary-update-secondary-mark article))
344 (gnus-summary-next-subject 1)
345 (gnus-summary-position-point)
348 (defun gnus-cache-remove-article (&optional n)
349 "Remove the next N articles from the cache.
350 If not given a prefix, use the process marked articles instead.
351 Returns the list of articles removed."
353 (gnus-cache-change-buffer gnus-newsgroup-name)
355 (dolist (article (gnus-summary-work-articles n))
356 (gnus-summary-remove-process-mark article)
357 (when (gnus-cache-possibly-remove-article article nil nil nil t)
358 (when gnus-newsgroup-agentized
359 (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
360 (unless (cdr (assoc article alist))
361 (setq gnus-newsgroup-undownloaded
362 (gnus-add-to-sorted-list
363 gnus-newsgroup-undownloaded article)))))
365 (gnus-summary-update-download-mark article)
366 (gnus-summary-update-secondary-mark article))
367 (gnus-summary-next-subject 1)
368 (gnus-summary-position-point)
371 (defun gnus-cached-article-p (article)
372 "Say whether ARTICLE is cached in the current group."
373 (memq article gnus-newsgroup-cached))
375 (defun gnus-summary-insert-cached-articles ()
376 "Insert all the articles cached for this group into the current buffer."
378 (let ((gnus-verbose (max 6 gnus-verbose)))
379 (if (not gnus-newsgroup-cached)
380 (gnus-message 3 "No cached articles for this group")
381 (gnus-summary-goto-subjects gnus-newsgroup-cached))))
383 (defun gnus-summary-limit-include-cached ()
384 "Limit the summary buffer to articles that are cached."
386 (let ((gnus-verbose (max 6 gnus-verbose)))
387 (if gnus-newsgroup-cached
389 (gnus-summary-limit gnus-newsgroup-cached)
390 (gnus-summary-position-point))
391 (gnus-message 3 "No cached articles for this group"))))
393 ;;; Internal functions.
395 (defun gnus-cache-change-buffer (group)
396 (and gnus-cache-buffer
397 ;; See if the current group's overview cache has been loaded.
398 (or (string= group (car gnus-cache-buffer))
399 ;; Another overview cache is current, save it.
400 (gnus-cache-save-buffers)))
401 ;; if gnus-cache buffer is nil, create it
402 (unless gnus-cache-buffer
403 ;; Create cache buffer
405 (setq gnus-cache-buffer
407 (set-buffer (gnus-get-buffer-create
408 " *gnus-cache-overview*"))))
409 ;; Insert the contents of this group's cache overview.
411 (let ((file (gnus-cache-file-name group ".overview")))
412 (when (file-exists-p file)
413 (nnheader-insert-file-contents file)))
414 ;; We have a fresh (empty/just loaded) buffer,
415 ;; mark it as unmodified to save a redundant write later.
416 (set-buffer-modified-p nil))))
418 ;; Return whether an article is a member of a class.
419 (defun gnus-cache-member-of-class (class ticked dormant unread)
420 (or (and ticked (memq 'ticked class))
421 (and dormant (memq 'dormant class))
422 (and unread (memq 'unread class))
423 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
425 (defun gnus-cache-file-name (group article)
426 (setq group (gnus-group-decoded-name group))
428 (if (stringp article) article (int-to-string article))
429 (file-name-as-directory
431 (nnheader-translate-file-chars
432 (if (gnus-use-long-file-name 'not-cache)
434 (let ((group (nnheader-replace-duplicate-chars-in-string
435 (nnheader-replace-chars-in-string group ?/ ?_)
437 ;; Translate the first colon into a slash.
438 (when (string-match ":" group)
439 (setq group (concat (substring group 0 (match-beginning 0))
440 "/" (substring group (match-end 0)))))
441 (nnheader-replace-chars-in-string group ?. ?/)))
443 gnus-cache-directory))))
445 (defun gnus-cache-update-article (group article)
446 "If ARTICLE is in the cache, remove it and re-enter it."
447 (gnus-cache-change-buffer group)
448 (when (gnus-cache-possibly-remove-article article nil nil nil t)
449 (let ((gnus-use-cache nil))
450 (gnus-cache-possibly-enter-article
451 gnus-newsgroup-name article
454 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
456 "Possibly remove ARTICLE from the cache."
457 (let ((group gnus-newsgroup-name)
460 ;; If this is a virtual group, we find the real group.
461 (when (gnus-virtual-group-p group)
462 (let ((result (nnvirtual-find-group-art
463 (gnus-group-real-name group) article)))
464 (setq group (car result)
465 number (cdr result))))
466 (setq file (gnus-cache-file-name group number))
467 (when (and (file-exists-p file)
469 (gnus-cache-member-of-class
470 gnus-cache-remove-articles ticked dormant unread)))
472 (gnus-cache-update-file-total-fetched-for group file t)
475 (set-buffer (cdr gnus-cache-buffer))
476 (goto-char (point-min))
477 (when (or (looking-at (concat (int-to-string number) "\t"))
478 (search-forward (concat "\n" (int-to-string number) "\t")
481 (unless (setq gnus-newsgroup-cached
482 (delq article gnus-newsgroup-cached))
483 (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
484 (setq gnus-cache-active-altered t))
485 (gnus-summary-update-secondary-mark article)
488 (defun gnus-cache-articles-in-group (group)
489 "Return a sorted list of cached articles in GROUP."
490 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
492 (when (file-exists-p dir)
494 (sort (mapcar (lambda (name) (string-to-number name))
495 (directory-files dir nil "^[0-9]+$" t))
497 ;; Update the cache active file, just to synch more.
500 (gnus-cache-update-active group (car articles) t)
501 (gnus-cache-update-active group (car (last articles))))
502 (when (gnus-gethash group gnus-cache-active-hashtb)
503 (gnus-sethash group nil gnus-cache-active-hashtb)
504 (setq gnus-cache-active-altered t)))
507 (defun gnus-cache-braid-nov (group cached &optional file)
508 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
510 (gnus-cache-save-buffers)
512 (set-buffer cache-buf)
514 (let ((coding-system-for-read
515 gnus-cache-overview-coding-system))
516 (insert-file-contents
517 (or file (gnus-cache-file-name group ".overview"))))
518 (goto-char (point-min))
520 (goto-char (point-min)))
521 (set-buffer nntp-server-buffer)
522 (goto-char (point-min))
524 (while (and (not (eobp))
525 (< (read (current-buffer)) (car cached)))
528 (set-buffer cache-buf)
529 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
531 (setq beg (point-at-bol)
532 end (progn (end-of-line) (point)))
534 (set-buffer nntp-server-buffer)
536 (insert-buffer-substring cache-buf beg end)
538 (setq cached (cdr cached)))
539 (kill-buffer cache-buf)))
541 (defun gnus-cache-braid-heads (group cached)
542 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
543 (with-current-buffer cache-buf
545 (set-buffer nntp-server-buffer)
546 (goto-char (point-min))
547 (dolist (entry cached)
548 (while (and (not (eobp))
549 (looking-at "2.. +\\([0-9]+\\) ")
550 (< (progn (goto-char (match-beginning 1))
551 (read (current-buffer)))
553 (search-forward "\n.\n" nil 'move))
555 (set-buffer cache-buf)
557 (let ((coding-system-for-read
558 gnus-cache-coding-system))
559 (insert-file-contents (gnus-cache-file-name group entry)))
560 (goto-char (point-min))
562 (princ (car cached) (current-buffer))
563 (insert " Article retrieved.\n")
564 (search-forward "\n\n" nil 'move)
565 (delete-region (point) (point-max))
568 (set-buffer nntp-server-buffer)
569 (insert-buffer-substring cache-buf))
570 (kill-buffer cache-buf)))
573 (defun gnus-jog-cache ()
574 "Go through all groups and put the articles into the cache.
577 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
579 (let ((gnus-mark-article-hook nil)
581 (nnmail-spool-file nil)
583 (gnus-use-dribble-file nil)
584 (gnus-novice-user nil)
585 (gnus-large-newsgroup nil))
588 ;; Go through all groups...
589 (gnus-group-mark-buffer)
590 (gnus-group-iterate nil
592 (let (gnus-auto-select-next)
593 (gnus-summary-read-group group nil t)
594 ;; ... and enter the articles into the cache.
595 (when (eq major-mode 'gnus-summary-mode)
596 (gnus-uu-mark-buffer)
597 (gnus-cache-enter-article)
598 (kill-buffer (current-buffer))))))))
600 (defun gnus-cache-read-active (&optional force)
601 "Read the cache active file."
602 (gnus-make-directory gnus-cache-directory)
603 (if (or (not (file-exists-p gnus-cache-active-file))
604 (zerop (nth 7 (file-attributes gnus-cache-active-file)))
606 ;; There is no active file, so we generate one.
607 (gnus-cache-generate-active)
608 ;; We simply read the active file.
610 (gnus-set-work-buffer)
611 (nnheader-insert-file-contents gnus-cache-active-file)
612 (gnus-active-to-gnus-format
613 nil (setq gnus-cache-active-hashtb
615 (count-lines (point-min) (point-max)))))
616 (setq gnus-cache-active-altered nil))))
618 (defun gnus-cache-write-active (&optional force)
619 "Write the active hashtb to the active file."
621 (and gnus-cache-active-hashtb
622 gnus-cache-active-altered))
623 (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
624 ;; Mark the active hashtb as unaltered.
625 (setq gnus-cache-active-altered nil)))
627 (defun gnus-cache-possibly-update-active (group active)
628 "Update active info bounds of GROUP with ACTIVE if necessary.
629 The update is performed if ACTIVE contains a higher or lower bound
631 (let ((lower t) (higher t))
632 (if gnus-cache-active-hashtb
633 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
635 (unless (< (car active) (car cache-active))
637 (unless (> (cdr active) (cdr cache-active))
639 (gnus-cache-read-active))
641 (gnus-cache-update-active group (car active) t))
643 (gnus-cache-update-active group (cdr active)))))
645 (defun gnus-cache-update-active (group number &optional low)
646 "Update the upper bound of the active info of GROUP to NUMBER.
647 If LOW, update the lower bound instead."
648 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
650 ;; We just create a new active entry for this group.
651 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
652 ;; Update the lower or upper bound.
654 (setcar active number)
655 (setcdr active number)))
656 ;; Mark the active hashtb as altered.
657 (setq gnus-cache-active-altered t)))
660 (defun gnus-cache-generate-active (&optional directory)
661 "Generate the cache active file."
663 (let* ((top (null directory))
664 (directory (expand-file-name (or directory gnus-cache-directory)))
665 (files (directory-files directory 'full))
670 (concat "^" (regexp-quote
671 (file-name-as-directory
672 (expand-file-name gnus-cache-directory))))
673 (directory-file-name directory))
674 (nnheader-replace-chars-in-string
675 (substring (directory-file-name directory) (match-end 0))
679 (gnus-message 5 "Generating the cache active file...")
680 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
681 (when (string-match "^\\(nn[^_]+\\)_" group)
682 (setq group (replace-match "\\1:" t nil group)))
683 ;; Separate articles from all other files and directories.
685 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
686 (push (string-to-number (file-name-nondirectory (pop files))) nums)
687 (push (pop files) alphs)))
688 ;; If we have nums, then this is probably a valid group.
689 (when (setq nums (sort nums '<))
690 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
691 gnus-cache-active-hashtb))
692 ;; Go through all the other files.
694 (when (and (file-directory-p file)
695 (not (string-match "^\\."
696 (file-name-nondirectory file))))
697 ;; We descend directories.
698 (gnus-cache-generate-active file)))
699 ;; Write the new active file.
701 (gnus-cache-write-active t)
702 (gnus-message 5 "Generating the cache active file...done"))))
705 (defun gnus-cache-generate-nov-databases (dir)
706 "Generate NOV files recursively starting in DIR."
707 (interactive (list gnus-cache-directory))
709 (let ((nnml-generate-active-function 'identity))
710 (nnml-generate-nov-databases-1 dir))
712 (setq gnus-cache-total-fetched-hashtb nil)
716 (defun gnus-cache-move-cache (dir)
717 "Move the cache tree to somewhere else."
718 (interactive "FMove the cache tree to: ")
719 (rename-file gnus-cache-directory dir))
721 (defun gnus-cache-fully-p (&optional group)
722 "Returns non-nil if the cache should be fully used.
723 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
724 `gnus-uncacheable-groups'."
726 (not (eq gnus-use-cache 'passive))
729 (and (or (not gnus-cacheable-groups)
730 (string-match gnus-cacheable-groups group))
731 (or (not gnus-uncacheable-groups)
732 (not (string-match gnus-uncacheable-groups group)))))))
735 (defun gnus-cache-rename-group (old-group new-group)
736 "Rename OLD-GROUP as NEW-GROUP. Always updates the cache, even when
737 disabled, as the old cache files would corrupt gnus when the cache was
738 next enabled. Depends upon the caller to determine whether group renaming is supported."
739 (let ((old-dir (gnus-cache-file-name old-group ""))
740 (new-dir (gnus-cache-file-name new-group "")))
741 (gnus-rename-file old-dir new-dir t))
743 (gnus-cache-rename-group-total-fetched-for old-group new-group)
745 (let ((no-save gnus-cache-active-hashtb))
746 (unless gnus-cache-active-hashtb
747 (gnus-cache-read-active))
748 (let* ((old-group-hash-value (gnus-gethash old-group gnus-cache-active-hashtb))
749 (new-group-hash-value (gnus-gethash new-group gnus-cache-active-hashtb))
750 (delta (or old-group-hash-value new-group-hash-value)))
751 (gnus-sethash new-group old-group-hash-value gnus-cache-active-hashtb)
752 (gnus-sethash old-group nil gnus-cache-active-hashtb)
755 (setq gnus-cache-active-altered delta)
756 (gnus-cache-write-active delta)))))
759 (defun gnus-cache-delete-group (group)
760 "Delete GROUP. Always updates the cache, even when
761 disabled, as the old cache files would corrupt gnus when the cache was
762 next enabled. Depends upon the caller to determine whether group deletion is supported."
763 (let ((dir (gnus-cache-file-name group "")))
764 (gnus-delete-directory dir))
766 (gnus-cache-delete-group-total-fetched-for group)
768 (let ((no-save gnus-cache-active-hashtb))
769 (unless gnus-cache-active-hashtb
770 (gnus-cache-read-active))
771 (let* ((group-hash-value (gnus-gethash group gnus-cache-active-hashtb)))
772 (gnus-sethash group nil gnus-cache-active-hashtb)
775 (setq gnus-cache-active-altered group-hash-value)
776 (gnus-cache-write-active group-hash-value)))))
778 (defvar gnus-cache-inhibit-update-total-fetched-for nil)
779 (defvar gnus-cache-need-update-total-fetched-for nil)
781 (defmacro gnus-cache-with-refreshed-group (group &rest body)
782 `(prog1 (let ((gnus-cache-inhibit-update-total-fetched-for t))
784 (when (and gnus-cache-need-update-total-fetched-for
785 (not gnus-cache-inhibit-update-total-fetched-for))
787 (set-buffer gnus-group-buffer)
788 (setq gnus-cache-need-update-total-fetched-for nil)
789 (gnus-group-update-group ,group t)))))
791 (defun gnus-cache-update-file-total-fetched-for (group file &optional subtract)
792 (when gnus-cache-total-fetched-hashtb
793 (gnus-cache-with-refreshed-group
795 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
796 (gnus-sethash group (make-vector 2 0)
797 gnus-cache-total-fetched-hashtb)))
801 (setq size (or (nth 7 (file-attributes file)) 0))
802 (let ((files (directory-files (gnus-cache-file-name group "")
806 (while (setq file (pop files))
807 (setq attrs (file-attributes file))
808 (unless (nth 0 attrs)
809 (incf size (float (nth 7 attrs)))))))
811 (setq gnus-cache-need-update-total-fetched-for t)
813 (incf (nth 1 entry) (if subtract (- size) size))))))
815 (defun gnus-cache-update-overview-total-fetched-for (group file)
816 (when gnus-cache-total-fetched-hashtb
817 (gnus-cache-with-refreshed-group
819 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
820 (gnus-sethash group (make-list 2 0)
821 gnus-cache-total-fetched-hashtb)))
822 (size (or (nth 7 (file-attributes
824 (gnus-cache-file-name group ".overview"))))
826 (setq gnus-cache-need-update-total-fetched-for t)
827 (setf (nth 0 entry) size)))))
829 (defun gnus-cache-rename-group-total-fetched-for (old-group new-group)
830 "Record of disk space used by OLD-GROUP now associated with NEW-GROUP."
831 (when gnus-cache-total-fetched-hashtb
832 (let ((entry (gnus-gethash old-group gnus-cache-total-fetched-hashtb)))
833 (gnus-sethash new-group entry gnus-cache-total-fetched-hashtb)
834 (gnus-sethash old-group nil gnus-cache-total-fetched-hashtb))))
836 (defun gnus-cache-delete-group-total-fetched-for (group)
837 "Delete record of disk space used by GROUP being deleted."
838 (when gnus-cache-total-fetched-hashtb
839 (gnus-sethash group nil gnus-cache-total-fetched-hashtb)))
841 (defun gnus-cache-total-fetched-for (group &optional no-inhibit)
842 "Get total disk space used by the cache for the specified GROUP."
843 (unless gnus-cache-total-fetched-hashtb
844 (setq gnus-cache-total-fetched-hashtb (gnus-make-hashtable 1024)))
846 (let* ((entry (gnus-gethash group gnus-cache-total-fetched-hashtb)))
849 (let ((gnus-cache-inhibit-update-total-fetched-for (not no-inhibit)))
851 (gnus-cache-update-overview-total-fetched-for group nil)
852 (gnus-cache-update-file-total-fetched-for group nil))))))
854 (provide 'gnus-cache)
856 ;;; arch-tag: 05a79442-8c58-4e65-bd0a-3cbb1b89a33a
857 ;;; gnus-cache.el ends here