1 ;;; gnus-cache.el --- cache interface for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
30 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
32 (eval-when-compile (require 'cl
))
38 (unless (fboundp 'gnus-agent-load-alist
)
39 (defun gnus-agent-load-alist (group))))
41 (defcustom gnus-cache-active-file
42 (expand-file-name "active" gnus-cache-directory
)
43 "*The cache active file."
47 (defcustom gnus-cache-enter-articles
'(ticked dormant
)
48 "Classes of articles to enter into the cache."
50 :type
'(set (const ticked
) (const dormant
) (const unread
) (const read
)))
52 (defcustom gnus-cache-remove-articles
'(read)
53 "Classes of articles to remove from the cache."
55 :type
'(set (const ticked
) (const dormant
) (const unread
) (const read
)))
57 (defcustom gnus-cacheable-groups nil
58 "*Groups that match this regexp will be cached.
60 If you only want to cache your nntp groups, you could set this
61 variable to \"^nntp\".
63 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
66 :type
'(choice (const :tag
"off" nil
)
69 (defcustom gnus-uncacheable-groups nil
70 "*Groups that match this regexp will not be cached.
72 If you want to avoid caching your nnml groups, you could set this
73 variable to \"^nnml\".
75 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
78 :type
'(choice (const :tag
"off" nil
)
81 (defvar gnus-cache-overview-coding-system
'raw-text
82 "Coding system used on Gnus cache files.")
84 (defvar gnus-cache-coding-system
'raw-text
85 "Coding system used on Gnus cache files.")
89 ;;; Internal variables.
91 (defvar gnus-cache-removable-articles nil
)
92 (defvar gnus-cache-buffer nil
)
93 (defvar gnus-cache-active-hashtb nil
)
94 (defvar gnus-cache-active-altered nil
)
95 (defvar gnus-cache-total-fetched-hashtb nil
)
97 (declare-function nnvirtual-find-group-art
"nnvirtual" (group article
))
99 (autoload 'nnml-generate-nov-databases-directory
"nnml")
100 (autoload 'nnvirtual-find-group-art
"nnvirtual")
104 ;;; Functions called from Gnus.
106 (defun gnus-cache-open ()
107 "Initialize the cache."
108 (when (or (file-exists-p gnus-cache-directory
)
110 (not (eq gnus-use-cache
'passive
))))
111 (gnus-cache-read-active)))
113 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
115 (gnus-add-shutdown 'gnus-cache-close
'gnus
))
117 (defun gnus-cache-close ()
118 "Shut down the cache."
119 (gnus-cache-write-active)
120 (gnus-cache-save-buffers)
121 (setq gnus-cache-active-hashtb nil
))
123 (defun gnus-cache-save-buffers ()
124 ;; save the overview buffer if it exists and has been modified
125 ;; delete empty cache subdirectories
126 (when gnus-cache-buffer
127 (let ((buffer (cdr gnus-cache-buffer
))
128 (overview-file (gnus-cache-file-name
129 (car gnus-cache-buffer
) ".overview")))
130 ;; write the overview only if it was modified
131 (when (and (buffer-live-p buffer
) (buffer-modified-p buffer
))
132 (with-current-buffer buffer
133 (if (> (buffer-size) 0)
134 ;; Non-empty overview, write it to a file.
135 (let ((coding-system-for-write
136 gnus-cache-overview-coding-system
))
137 (gnus-write-buffer overview-file
))
138 (let ((file-name-coding-system nnmail-pathname-coding-system
))
139 ;; Empty overview file, remove it
140 (when (file-exists-p overview-file
)
141 (delete-file overview-file
))
142 ;; If possible, remove group's cache subdirectory.
144 ;; FIXME: we can detect the error type and warn the user
145 ;; of any inconsistencies (articles w/o nov entries?).
146 ;; for now, just be conservative...delete only if safe -- sj
147 (delete-directory (file-name-directory overview-file
))
150 (gnus-cache-update-overview-total-fetched-for
151 (car gnus-cache-buffer
) overview-file
)))
152 ;; Kill the buffer -- it's either unmodified or saved.
153 (gnus-kill-buffer buffer
)
154 (setq gnus-cache-buffer nil
))))
156 (defun gnus-cache-possibly-enter-article
157 (group article ticked dormant unread
&optional force
)
158 (when (and (or force
(not (eq gnus-use-cache
'passive
)))
160 (> article
0)) ; This might be a dummy article.
161 (let ((number article
)
162 file headers lines-chars
163 (file-name-coding-system nnmail-pathname-coding-system
))
164 ;; If this is a virtual group, we find the real group.
165 (when (gnus-virtual-group-p group
)
166 (let ((result (nnvirtual-find-group-art
167 (gnus-group-real-name group
) article
)))
168 (setq group
(car result
)
169 number
(cdr result
))))
171 (> number
0) ; Reffed article.
173 (and (gnus-cache-fully-p group
)
174 (gnus-cache-member-of-class
175 gnus-cache-enter-articles ticked dormant unread
)))
176 (not (file-exists-p (setq file
(gnus-cache-file-name
178 ;; Possibly create the cache directory.
179 (gnus-make-directory (file-name-directory file
))
180 ;; Save the article in the cache.
181 (if (file-exists-p file
)
182 t
; The article already is saved.
184 (set-buffer nntp-server-buffer
)
186 (let ((gnus-use-cache nil
)
187 (gnus-article-decode-hook nil
))
188 (gnus-request-article-this-buffer number group
))
189 (when (> (buffer-size) 0)
190 (let ((coding-system-for-write gnus-cache-coding-system
))
191 (gnus-write-buffer file
)
192 (gnus-cache-update-file-total-fetched-for group file
))
193 (setq lines-chars
(nnheader-get-lines-and-char))
194 (nnheader-remove-body)
195 (setq headers
(nnheader-parse-naked-head))
196 (mail-header-set-number headers number
)
197 (mail-header-set-lines headers
(car lines-chars
))
198 (mail-header-set-chars headers
(cadr lines-chars
))
199 (gnus-cache-change-buffer group
)
200 (set-buffer (cdr gnus-cache-buffer
))
201 (goto-char (point-max))
203 (while (condition-case ()
205 (> (read (current-buffer)) number
))
207 ;; The line was malformed, so we just remove it!!
215 (when (< (read (current-buffer)) number
)
220 (nnheader-insert-nov headers
)
221 ;; Update the active info.
222 (set-buffer gnus-summary-buffer
)
223 (gnus-cache-possibly-update-active group
(cons number number
))
224 (setq gnus-newsgroup-cached
225 (gnus-add-to-sorted-list gnus-newsgroup-cached article
))
226 (gnus-summary-update-secondary-mark article
))
229 (defun gnus-cache-enter-remove-article (article)
230 "Mark ARTICLE for later possible removal."
232 (push article gnus-cache-removable-articles
)))
234 (defun gnus-cache-possibly-remove-articles ()
235 "Possibly remove some of the removable articles."
236 (if (not (gnus-virtual-group-p gnus-newsgroup-name
))
237 (gnus-cache-possibly-remove-articles-1)
238 (let ((arts gnus-cache-removable-articles
)
241 (when (setq ga
(nnvirtual-find-group-art
242 (gnus-group-real-name gnus-newsgroup-name
) (pop arts
)))
243 (let ((gnus-cache-removable-articles (list (cdr ga
)))
244 (gnus-newsgroup-name (car ga
)))
245 (gnus-cache-possibly-remove-articles-1)))))
246 (setq gnus-cache-removable-articles nil
)))
248 (defun gnus-cache-possibly-remove-articles-1 ()
249 "Possibly remove some of the removable articles."
250 (when (gnus-cache-fully-p gnus-newsgroup-name
)
251 (let ((cache-articles gnus-newsgroup-cached
))
252 (gnus-cache-change-buffer gnus-newsgroup-name
)
253 (dolist (article gnus-cache-removable-articles
)
254 (when (memq article cache-articles
)
255 ;; The article was in the cache, so we see whether we are
256 ;; supposed to remove it from the cache.
257 (gnus-cache-possibly-remove-article
258 article
(memq article gnus-newsgroup-marked
)
259 (memq article gnus-newsgroup-dormant
)
260 (or (memq article gnus-newsgroup-unreads
)
261 (memq article gnus-newsgroup-unselected
))))))
262 ;; The overview file might have been modified, save it
263 ;; safe because we're only called at group exit anyway.
264 (gnus-cache-save-buffers)))
266 (defun gnus-cache-request-article (article group
)
267 "Retrieve ARTICLE in GROUP from the cache."
268 (let ((file (gnus-cache-file-name group article
))
269 (buffer-read-only nil
)
270 (file-name-coding-system nnmail-pathname-coding-system
))
271 (when (file-exists-p file
)
273 (gnus-kill-all-overlays)
274 (let ((coding-system-for-read gnus-cache-coding-system
))
275 (insert-file-contents file
))
278 (defun gnus-cache-possibly-alter-active (group active
)
279 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
280 (when gnus-cache-active-hashtb
281 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb
)))
283 (when (< (car cache-active
) (car active
))
284 (setcar active
(car cache-active
)))
285 (when (> (cdr cache-active
) (cdr active
))
286 (setcdr active
(cdr cache-active
)))))))
288 (defun gnus-cache-retrieve-headers (articles group
&optional fetch-old
)
289 "Retrieve the headers for ARTICLES in GROUP."
291 (setq gnus-newsgroup-cached
(gnus-cache-articles-in-group group
))))
293 ;; No cached articles here, so we just retrieve them
295 (let ((gnus-use-cache nil
))
296 (gnus-retrieve-headers articles group fetch-old
))
297 (let ((uncached-articles (gnus-sorted-difference articles cached
))
298 (cache-file (gnus-cache-file-name group
".overview"))
300 (file-name-coding-system nnmail-pathname-coding-system
))
301 ;; We first retrieve all the headers that we don't have in
303 (let ((gnus-use-cache nil
))
304 (when uncached-articles
305 (setq type
(and articles
306 (gnus-retrieve-headers
307 uncached-articles group fetch-old
)))))
308 (gnus-cache-save-buffers)
309 ;; Then we insert the cached headers.
312 ((not (file-exists-p cache-file
))
313 ;; There are no cached headers.
316 ;; There were no uncached headers (or retrieval was
317 ;; unsuccessful), so we use the cached headers exclusively.
318 (set-buffer nntp-server-buffer
)
320 (let ((coding-system-for-read
321 gnus-cache-overview-coding-system
))
322 (insert-file-contents cache-file
))
325 ;; We have both cached and uncached NOV headers, so we
327 (gnus-cache-braid-nov group cached
)
331 (gnus-cache-braid-heads group
(gnus-sorted-intersection
335 (defun gnus-cache-enter-article (&optional n
)
336 "Enter the next N articles into the cache.
337 If not given a prefix, use the process marked articles instead.
338 Returns the list of articles entered."
341 (dolist (article (gnus-summary-work-articles n
))
342 (gnus-summary-remove-process-mark article
)
343 (if (natnump article
)
344 (when (gnus-cache-possibly-enter-article
345 gnus-newsgroup-name article
347 (setq gnus-newsgroup-undownloaded
(delq article gnus-newsgroup-undownloaded
))
349 (gnus-message 2 "Can't cache article %d" article
))
350 (gnus-summary-update-download-mark article
)
351 (gnus-summary-update-secondary-mark article
))
352 (gnus-summary-next-subject 1)
353 (gnus-summary-position-point)
356 (defun gnus-cache-remove-article (&optional n
)
357 "Remove the next N articles from the cache.
358 If not given a prefix, use the process marked articles instead.
359 Returns the list of articles removed."
361 (gnus-cache-change-buffer gnus-newsgroup-name
)
363 (dolist (article (gnus-summary-work-articles n
))
364 (gnus-summary-remove-process-mark article
)
365 (when (gnus-cache-possibly-remove-article article nil nil nil t
)
366 (when gnus-newsgroup-agentized
367 (let ((alist (gnus-agent-load-alist gnus-newsgroup-name
)))
368 (unless (cdr (assoc article alist
))
369 (setq gnus-newsgroup-undownloaded
370 (gnus-add-to-sorted-list
371 gnus-newsgroup-undownloaded article
)))))
373 (gnus-summary-update-download-mark article
)
374 (gnus-summary-update-secondary-mark article
))
375 (gnus-summary-next-subject 1)
376 (gnus-summary-position-point)
379 (defun gnus-cached-article-p (article)
380 "Say whether ARTICLE is cached in the current group."
381 (memq article gnus-newsgroup-cached
))
383 (defun gnus-summary-insert-cached-articles ()
384 "Insert all the articles cached for this group into the current buffer."
386 (let ((gnus-verbose (max 6 gnus-verbose
)))
387 (if (not gnus-newsgroup-cached
)
388 (gnus-message 3 "No cached articles for this group")
389 (gnus-summary-goto-subjects gnus-newsgroup-cached
))))
391 (defun gnus-summary-limit-include-cached ()
392 "Limit the summary buffer to articles that are cached."
394 (let ((gnus-verbose (max 6 gnus-verbose
)))
395 (if gnus-newsgroup-cached
397 (gnus-summary-limit gnus-newsgroup-cached
)
398 (gnus-summary-position-point))
399 (gnus-message 3 "No cached articles for this group"))))
401 ;;; Internal functions.
403 (defun gnus-cache-change-buffer (group)
404 (and gnus-cache-buffer
405 ;; See if the current group's overview cache has been loaded.
406 (or (string= group
(car gnus-cache-buffer
))
407 ;; Another overview cache is current, save it.
408 (gnus-cache-save-buffers)))
409 ;; if gnus-cache buffer is nil, create it
410 (unless gnus-cache-buffer
411 ;; Create cache buffer
413 (setq gnus-cache-buffer
415 (set-buffer (gnus-get-buffer-create
416 " *gnus-cache-overview*"))))
417 ;; Insert the contents of this group's cache overview.
419 (let ((file (gnus-cache-file-name group
".overview"))
420 (file-name-coding-system nnmail-pathname-coding-system
))
421 (when (file-exists-p file
)
422 (nnheader-insert-file-contents file
)))
423 ;; We have a fresh (empty/just loaded) buffer,
424 ;; mark it as unmodified to save a redundant write later.
425 (set-buffer-modified-p nil
))))
427 ;; Return whether an article is a member of a class.
428 (defun gnus-cache-member-of-class (class ticked dormant unread
)
429 (or (and ticked
(memq 'ticked class
))
430 (and dormant
(memq 'dormant class
))
431 (and unread
(memq 'unread class
))
432 (and (not unread
) (not ticked
) (not dormant
) (memq 'read class
))))
434 (defvar gnus-cache-decoded-group-names nil
435 "Alist of original group names and decoded group names.
436 Decoding is done according to `gnus-group-name-charset-method-alist'
437 or `gnus-group-name-charset-group-alist'.")
439 (defvar gnus-cache-unified-group-names nil
440 "Alist of unified decoded group names and original group names.
441 A group name is decoded according to
442 `gnus-group-name-charset-method-alist' or
443 `gnus-group-name-charset-group-alist' first, and is encoded and
444 decoded again according to `nnmail-pathname-coding-system',
445 `file-name-coding-system', or `default-file-name-coding-system'.
447 It is used when asking for a original group name from a cache
448 directory name, in which non-ASCII characters might have been unified
449 into the ones of a certain charset particularly if the `utf-8' coding
450 system for example was used.")
452 (defun gnus-cache-decoded-group-name (group)
453 "Return a decoded group name of GROUP."
454 (or (cdr (assoc group gnus-cache-decoded-group-names
))
455 (let ((decoded (gnus-group-decoded-name group
))
456 (coding (or nnmail-pathname-coding-system
457 (and (boundp 'file-name-coding-system
)
458 file-name-coding-system
)
459 (and (boundp 'default-file-name-coding-system
)
460 default-file-name-coding-system
))))
461 (push (cons group decoded
) gnus-cache-decoded-group-names
)
462 (push (cons (mm-decode-coding-string
463 (mm-encode-coding-string decoded coding
)
466 gnus-cache-unified-group-names
)
469 (defun gnus-cache-file-name (group article
)
470 (setq group
(gnus-cache-decoded-group-name group
))
472 (if (stringp article
) article
(int-to-string article
))
473 (file-name-as-directory
475 (nnheader-translate-file-chars
476 (if (gnus-use-long-file-name 'not-cache
)
478 (let ((group (nnheader-replace-duplicate-chars-in-string
479 (nnheader-replace-chars-in-string group ?
/ ?_
)
481 ;; Translate the first colon into a slash.
482 (when (string-match ":" group
)
483 (setq group
(concat (substring group
0 (match-beginning 0))
484 "/" (substring group
(match-end 0)))))
485 (nnheader-replace-chars-in-string group ?. ?
/)))
487 gnus-cache-directory
))))
489 (defun gnus-cache-update-article (group article
)
490 "If ARTICLE is in the cache, remove it and re-enter it."
491 (gnus-cache-change-buffer group
)
492 (when (gnus-cache-possibly-remove-article article nil nil nil t
)
493 (let ((gnus-use-cache nil
))
494 (gnus-cache-possibly-enter-article
495 gnus-newsgroup-name article
498 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
500 "Possibly remove ARTICLE from the cache."
501 (let ((group gnus-newsgroup-name
)
504 (file-name-coding-system nnmail-pathname-coding-system
))
505 ;; If this is a virtual group, we find the real group.
506 (when (gnus-virtual-group-p group
)
507 (let ((result (nnvirtual-find-group-art
508 (gnus-group-real-name group
) article
)))
509 (setq group
(car result
)
510 number
(cdr result
))))
511 (setq file
(gnus-cache-file-name group number
))
512 (when (and (file-exists-p file
)
514 (gnus-cache-member-of-class
515 gnus-cache-remove-articles ticked dormant unread
)))
517 (gnus-cache-update-file-total-fetched-for group file t
)
520 (set-buffer (cdr gnus-cache-buffer
))
521 (goto-char (point-min))
522 (when (or (looking-at (concat (int-to-string number
) "\t"))
523 (search-forward (concat "\n" (int-to-string number
) "\t")
526 (unless (setq gnus-newsgroup-cached
527 (delq article gnus-newsgroup-cached
))
528 (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb
)
529 (setq gnus-cache-active-altered t
))
530 (gnus-summary-update-secondary-mark article
)
533 (defun gnus-cache-articles-in-group (group)
534 "Return a sorted list of cached articles in GROUP."
535 (let ((dir (file-name-directory (gnus-cache-file-name group
1)))
537 (file-name-coding-system nnmail-pathname-coding-system
))
538 (when (file-exists-p dir
)
540 (sort (mapcar (lambda (name) (string-to-number name
))
541 (directory-files dir nil
"^[0-9]+$" t
))
543 ;; Update the cache active file, just to synch more.
546 (gnus-cache-update-active group
(car articles
) t
)
547 (gnus-cache-update-active group
(car (last articles
))))
548 (when (gnus-gethash group gnus-cache-active-hashtb
)
549 (gnus-sethash group nil gnus-cache-active-hashtb
)
550 (setq gnus-cache-active-altered t
)))
553 (defun gnus-cache-braid-nov (group cached
&optional file
)
554 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
556 (gnus-cache-save-buffers)
558 (set-buffer cache-buf
)
560 (let ((coding-system-for-read gnus-cache-overview-coding-system
)
561 (file-name-coding-system nnmail-pathname-coding-system
))
562 (insert-file-contents
563 (or file
(gnus-cache-file-name group
".overview"))))
564 (goto-char (point-min))
566 (goto-char (point-min)))
567 (set-buffer nntp-server-buffer
)
568 (goto-char (point-min))
570 (while (and (not (eobp))
571 (< (read (current-buffer)) (car cached
)))
574 (set-buffer cache-buf
)
575 (if (search-forward (concat "\n" (int-to-string (car cached
)) "\t")
577 (setq beg
(point-at-bol)
578 end
(progn (end-of-line) (point)))
580 (set-buffer nntp-server-buffer
)
582 (insert-buffer-substring cache-buf beg end
)
584 (setq cached
(cdr cached
)))
585 (kill-buffer cache-buf
)))
587 (defun gnus-cache-braid-heads (group cached
)
588 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
589 (with-current-buffer cache-buf
591 (set-buffer nntp-server-buffer
)
592 (goto-char (point-min))
593 (dolist (entry cached
)
594 (while (and (not (eobp))
595 (looking-at "2.. +\\([0-9]+\\) ")
596 (< (progn (goto-char (match-beginning 1))
597 (read (current-buffer)))
599 (search-forward "\n.\n" nil
'move
))
601 (set-buffer cache-buf
)
603 (let ((coding-system-for-read gnus-cache-coding-system
)
604 (file-name-coding-system nnmail-pathname-coding-system
))
605 (insert-file-contents (gnus-cache-file-name group entry
)))
606 (goto-char (point-min))
608 (princ (car cached
) (current-buffer))
609 (insert " Article retrieved.\n")
610 (search-forward "\n\n" nil
'move
)
611 (delete-region (point) (point-max))
614 (set-buffer nntp-server-buffer
)
615 (insert-buffer-substring cache-buf
))
616 (kill-buffer cache-buf
)))
619 (defun gnus-jog-cache ()
620 "Go through all groups and put the articles into the cache.
623 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
625 (let ((gnus-mark-article-hook nil
)
628 (gnus-use-dribble-file nil
)
629 (gnus-novice-user nil
)
630 (gnus-large-newsgroup nil
))
633 ;; Go through all groups...
634 (gnus-group-mark-buffer)
635 (gnus-group-iterate nil
637 (let (gnus-auto-select-next)
638 (gnus-summary-read-group group nil t
)
639 ;; ... and enter the articles into the cache.
640 (when (eq major-mode
'gnus-summary-mode
)
641 (gnus-uu-mark-buffer)
642 (gnus-cache-enter-article)
643 (kill-buffer (current-buffer))))))))
645 (defun gnus-cache-read-active (&optional force
)
646 "Read the cache active file."
647 (gnus-make-directory gnus-cache-directory
)
648 (if (or (not (file-exists-p gnus-cache-active-file
))
649 (zerop (nth 7 (file-attributes gnus-cache-active-file
)))
651 ;; There is no active file, so we generate one.
652 (gnus-cache-generate-active)
653 ;; We simply read the active file.
655 (gnus-set-work-buffer)
656 (nnheader-insert-file-contents gnus-cache-active-file
)
657 (gnus-active-to-gnus-format
658 nil
(setq gnus-cache-active-hashtb
660 (count-lines (point-min) (point-max)))))
661 (setq gnus-cache-active-altered nil
))))
663 (defun gnus-cache-write-active (&optional force
)
664 "Write the active hashtb to the active file."
666 (and gnus-cache-active-hashtb
667 gnus-cache-active-altered
))
668 (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t
)
669 ;; Mark the active hashtb as unaltered.
670 (setq gnus-cache-active-altered nil
)))
672 (defun gnus-cache-possibly-update-active (group active
)
673 "Update active info bounds of GROUP with ACTIVE if necessary.
674 The update is performed if ACTIVE contains a higher or lower bound
676 (let ((lower t
) (higher t
))
677 (if gnus-cache-active-hashtb
678 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb
)))
680 (unless (< (car active
) (car cache-active
))
682 (unless (> (cdr active
) (cdr cache-active
))
684 (gnus-cache-read-active))
686 (gnus-cache-update-active group
(car active
) t
))
688 (gnus-cache-update-active group
(cdr active
)))))
690 (defun gnus-cache-update-active (group number
&optional low
)
691 "Update the upper bound of the active info of GROUP to NUMBER.
692 If LOW, update the lower bound instead."
693 (let ((active (gnus-gethash group gnus-cache-active-hashtb
)))
695 ;; We just create a new active entry for this group.
696 (gnus-sethash group
(cons number number
) gnus-cache-active-hashtb
)
697 ;; Update the lower or upper bound.
699 (setcar active number
)
700 (setcdr active number
)))
701 ;; Mark the active hashtb as altered.
702 (setq gnus-cache-active-altered t
)))
705 (defun gnus-cache-generate-active (&optional directory
)
706 "Generate the cache active file."
708 (let* ((top (null directory
))
709 (directory (expand-file-name (or directory gnus-cache-directory
)))
710 (file-name-coding-system nnmail-pathname-coding-system
)
711 (files (directory-files directory
'full
))
716 (concat "^" (regexp-quote
717 (file-name-as-directory
718 (expand-file-name gnus-cache-directory
))))
719 (directory-file-name directory
))
720 (nnheader-replace-chars-in-string
721 (substring (directory-file-name directory
) (match-end 0))
725 (gnus-message 5 "Generating the cache active file...")
726 (setq gnus-cache-active-hashtb
(gnus-make-hashtable 123)))
727 (when (string-match "^\\(nn[^_]+\\)_" group
)
728 (setq group
(replace-match "\\1:" t nil group
)))
729 ;; Separate articles from all other files and directories.
731 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files
)))
732 (push (string-to-number (file-name-nondirectory (pop files
))) nums
)
733 (push (pop files
) alphs
)))
734 ;; If we have nums, then this is probably a valid group.
735 (when (setq nums
(sort nums
'<))
736 ;; Use non-decoded group name.
737 ;; FIXME: this is kind of a workaround. The active file should
738 ;; be updated at the time articles are cached. It will make
739 ;; `gnus-cache-unified-group-names' needless.
740 (gnus-sethash (or (cdr (assoc group gnus-cache-unified-group-names
))
742 (cons (car nums
) (gnus-last-element nums
))
743 gnus-cache-active-hashtb
))
744 ;; Go through all the other files.
746 (when (and (file-directory-p file
)
747 (not (string-match "^\\."
748 (file-name-nondirectory file
))))
749 ;; We descend directories.
750 (gnus-cache-generate-active file
)))
751 ;; Write the new active file.
753 (gnus-cache-write-active t
)
754 (gnus-message 5 "Generating the cache active file...done"))))
757 (defun gnus-cache-generate-nov-databases (dir)
758 "Generate NOV files recursively starting in DIR."
759 (interactive (list gnus-cache-directory
))
761 (let ((nnml-generate-active-function 'identity
))
762 (nnml-generate-nov-databases-directory dir
))
764 (setq gnus-cache-total-fetched-hashtb nil
)
768 (defun gnus-cache-move-cache (dir)
769 "Move the cache tree to somewhere else."
770 (interactive "FMove the cache tree to: ")
771 (rename-file gnus-cache-directory dir
))
773 (defun gnus-cache-fully-p (&optional group
)
774 "Returns non-nil if the cache should be fully used.
775 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
776 `gnus-uncacheable-groups'."
778 (not (eq gnus-use-cache
'passive
))
781 (and (or (not gnus-cacheable-groups
)
782 (string-match gnus-cacheable-groups group
))
783 (or (not gnus-uncacheable-groups
)
784 (not (string-match gnus-uncacheable-groups group
)))))))
787 (defun gnus-cache-rename-group (old-group new-group
)
788 "Rename OLD-GROUP as NEW-GROUP.
789 Always updates the cache, even when disabled, as the old cache
790 files would corrupt Gnus when the cache was next enabled. It
791 depends on the caller to determine whether group renaming is
793 (let ((old-dir (gnus-cache-file-name old-group
""))
794 (new-dir (gnus-cache-file-name new-group
""))
795 (file-name-coding-system nnmail-pathname-coding-system
))
796 (gnus-rename-file old-dir new-dir t
))
798 (gnus-cache-rename-group-total-fetched-for old-group new-group
)
800 (let ((no-save gnus-cache-active-hashtb
))
801 (unless gnus-cache-active-hashtb
802 (gnus-cache-read-active))
803 (let* ((old-group-hash-value
804 (gnus-gethash old-group gnus-cache-active-hashtb
))
805 (new-group-hash-value
806 (gnus-gethash new-group gnus-cache-active-hashtb
))
808 (or old-group-hash-value new-group-hash-value
)))
809 (gnus-sethash new-group old-group-hash-value gnus-cache-active-hashtb
)
810 (gnus-sethash old-group nil gnus-cache-active-hashtb
)
813 (setq gnus-cache-active-altered delta
)
814 (gnus-cache-write-active delta
)))))
817 (defun gnus-cache-delete-group (group)
818 "Delete GROUP from the cache.
819 Always updates the cache, even when disabled, as the old cache
820 files would corrupt gnus when the cache was next enabled.
821 Depends upon the caller to determine whether group deletion is
823 (let ((dir (gnus-cache-file-name group
""))
824 (file-name-coding-system nnmail-pathname-coding-system
))
825 (gnus-delete-directory dir
))
827 (gnus-cache-delete-group-total-fetched-for group
)
829 (let ((no-save gnus-cache-active-hashtb
))
830 (unless gnus-cache-active-hashtb
831 (gnus-cache-read-active))
832 (let* ((group-hash-value (gnus-gethash group gnus-cache-active-hashtb
)))
833 (gnus-sethash group nil gnus-cache-active-hashtb
)
836 (setq gnus-cache-active-altered group-hash-value
)
837 (gnus-cache-write-active group-hash-value
)))))
839 (defvar gnus-cache-inhibit-update-total-fetched-for nil
)
840 (defvar gnus-cache-need-update-total-fetched-for nil
)
842 (defmacro gnus-cache-with-refreshed-group
(group &rest body
)
843 `(prog1 (let ((gnus-cache-inhibit-update-total-fetched-for t
))
845 (when (and gnus-cache-need-update-total-fetched-for
846 (not gnus-cache-inhibit-update-total-fetched-for
))
848 (set-buffer gnus-group-buffer
)
849 (setq gnus-cache-need-update-total-fetched-for nil
)
850 (gnus-group-update-group ,group t
)))))
852 (defun gnus-cache-update-file-total-fetched-for (group file
&optional subtract
)
853 (when gnus-cache-total-fetched-hashtb
854 (gnus-cache-with-refreshed-group
856 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb
)
857 (gnus-sethash group
(make-vector 2 0)
858 gnus-cache-total-fetched-hashtb
)))
862 (setq size
(or (nth 7 (file-attributes file
)) 0))
863 (let* ((file-name-coding-system nnmail-pathname-coding-system
)
864 (files (directory-files (gnus-cache-file-name group
"")
868 (while (setq file
(pop files
))
869 (setq attrs
(file-attributes file
))
870 (unless (nth 0 attrs
)
871 (incf size
(float (nth 7 attrs
)))))))
873 (setq gnus-cache-need-update-total-fetched-for t
)
875 (incf (nth 1 entry
) (if subtract
(- size
) size
))))))
877 (defun gnus-cache-update-overview-total-fetched-for (group file
)
878 (when gnus-cache-total-fetched-hashtb
879 (gnus-cache-with-refreshed-group
881 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb
)
882 (gnus-sethash group
(make-list 2 0)
883 gnus-cache-total-fetched-hashtb
)))
884 (file-name-coding-system nnmail-pathname-coding-system
)
885 (size (or (nth 7 (file-attributes
887 (gnus-cache-file-name group
".overview"))))
889 (setq gnus-cache-need-update-total-fetched-for t
)
890 (setf (nth 0 entry
) size
)))))
892 (defun gnus-cache-rename-group-total-fetched-for (old-group new-group
)
893 "Record of disk space used by OLD-GROUP now associated with NEW-GROUP."
894 (when gnus-cache-total-fetched-hashtb
895 (let ((entry (gnus-gethash old-group gnus-cache-total-fetched-hashtb
)))
896 (gnus-sethash new-group entry gnus-cache-total-fetched-hashtb
)
897 (gnus-sethash old-group nil gnus-cache-total-fetched-hashtb
))))
899 (defun gnus-cache-delete-group-total-fetched-for (group)
900 "Delete record of disk space used by GROUP being deleted."
901 (when gnus-cache-total-fetched-hashtb
902 (gnus-sethash group nil gnus-cache-total-fetched-hashtb
)))
904 (defun gnus-cache-total-fetched-for (group &optional no-inhibit
)
905 "Get total disk space used by the cache for the specified GROUP."
906 (unless (equal group
"dummy.group")
907 (unless gnus-cache-total-fetched-hashtb
908 (setq gnus-cache-total-fetched-hashtb
(gnus-make-hashtable 1024)))
910 (let* ((entry (gnus-gethash group gnus-cache-total-fetched-hashtb
)))
913 (let ((gnus-cache-inhibit-update-total-fetched-for (not no-inhibit
)))
915 (gnus-cache-update-overview-total-fetched-for group nil
)
916 (gnus-cache-update-file-total-fetched-for group nil
)))))))
918 (provide 'gnus-cache
)
920 ;; arch-tag: 05a79442-8c58-4e65-bd0a-3cbb1b89a33a
921 ;;; gnus-cache.el ends here