5f6fdf6eed8487cb5afe6ef3a5a6d90d861319f5
[elisp/gnus.git-] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-int)
32 (require 'gnus-range)
33 (require 'gnus-start)
34 (eval-when-compile
35   (require 'gnus-sum))
36
37 (defcustom gnus-cache-active-file
38   (concat (file-name-as-directory gnus-cache-directory) "active")
39   "*The cache active file."
40   :group 'gnus-cache
41   :type 'file)
42
43 (defcustom gnus-cache-enter-articles '(ticked dormant)
44   "Classes of articles to enter into the cache."
45   :group 'gnus-cache
46   :type '(set (const ticked) (const dormant) (const unread) (const read)))
47
48 (defcustom gnus-cache-remove-articles '(read)
49   "Classes of articles to remove from the cache."
50   :group 'gnus-cache
51   :type '(set (const ticked) (const dormant) (const unread) (const read)))
52
53 (defcustom gnus-uncacheable-groups nil
54   "*Groups that match this regexp will not be cached.
55
56 If you want to avoid caching your nnml groups, you could set this
57 variable to \"^nnml\"."
58   :group 'gnus-cache
59   :type '(choice (const :tag "off" nil)
60                  regexp))
61
62 \f
63
64 ;;; Internal variables.
65
66 (defvar gnus-cache-removable-articles nil)
67 (defvar gnus-cache-buffer nil)
68 (defvar gnus-cache-active-hashtb nil)
69 (defvar gnus-cache-active-altered nil)
70
71 (eval-and-compile
72   (autoload 'nnml-generate-nov-databases-1 "nnml")
73   (autoload 'nnvirtual-find-group-art "nnvirtual"))
74
75 \f
76
77 ;;; Functions called from Gnus.
78
79 (defun gnus-cache-open ()
80   "Initialize the cache."
81   (when (or (file-exists-p gnus-cache-directory)
82             (and gnus-use-cache
83                  (not (eq gnus-use-cache 'passive))))
84     (gnus-cache-read-active)))
85
86 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
87 (ignore-errors
88   (gnus-add-shutdown 'gnus-cache-close 'gnus))
89
90 (defun gnus-cache-close ()
91   "Shut down the cache."
92   (gnus-cache-write-active)
93   (gnus-cache-save-buffers)
94   (setq gnus-cache-active-hashtb nil))
95
96 (defun gnus-cache-save-buffers ()
97   ;; save the overview buffer if it exists and has been modified
98   ;; delete empty cache subdirectories
99   (when gnus-cache-buffer
100     (let ((buffer (cdr gnus-cache-buffer))
101           (overview-file (gnus-cache-file-name
102                           (car gnus-cache-buffer) ".overview")))
103       ;; write the overview only if it was modified
104       (when (buffer-modified-p buffer)
105         (save-excursion
106           (set-buffer buffer)
107           (if (> (buffer-size) 0)
108               ;; Non-empty overview, write it to a file.
109               (gnus-write-buffer overview-file)
110             ;; Empty overview file, remove it
111             (when (file-exists-p overview-file)
112               (delete-file overview-file))
113             ;; If possible, remove group's cache subdirectory.
114             (condition-case nil
115                 ;; FIXME: we can detect the error type and warn the user
116                 ;; of any inconsistencies (articles w/o nov entries?).
117                 ;; for now, just be conservative...delete only if safe -- sj
118                 (delete-directory (file-name-directory overview-file))
119               (error nil)))))
120       ;; Kill the buffer -- it's either unmodified or saved.
121       (gnus-kill-buffer buffer)
122       (setq gnus-cache-buffer nil))))
123
124 (defun gnus-cache-possibly-enter-article
125   (group article headers ticked dormant unread &optional force)
126   (when (and (or force (not (eq gnus-use-cache 'passive)))
127              (numberp article)
128              (> article 0)
129              (vectorp headers))         ; This might be a dummy article.
130     ;; If this is a virtual group, we find the real group.
131     (when (gnus-virtual-group-p group)
132       (let ((result (nnvirtual-find-group-art
133                      (gnus-group-real-name group) article)))
134         (setq group (car result)
135               headers (copy-sequence headers))
136         (mail-header-set-number headers (cdr result))))
137     (let ((number (mail-header-number headers))
138           file dir)
139       (when (and number
140                  (> number 0)           ; Reffed article.
141                  (or force
142                      (and (or (not gnus-uncacheable-groups)
143                               (not (string-match
144                                     gnus-uncacheable-groups group)))
145                           (gnus-cache-member-of-class
146                            gnus-cache-enter-articles ticked dormant unread)))
147                  (not (file-exists-p (setq file (gnus-cache-file-name
148                                                  group number)))))
149         ;; Possibly create the cache directory.
150         (gnus-make-directory (setq dir (file-name-directory file)))
151         ;; Save the article in the cache.
152         (if (file-exists-p file)
153             t                           ; The article already is saved.
154           (save-excursion
155             (set-buffer nntp-server-buffer)
156             (let ((gnus-use-cache nil))
157               (gnus-request-article-this-buffer number group))
158             (when (> (buffer-size) 0)
159               (gnus-write-buffer file)
160               (gnus-cache-change-buffer group)
161               (set-buffer (cdr gnus-cache-buffer))
162               (goto-char (point-max))
163               (forward-line -1)
164               (while (condition-case ()
165                          (when (not (bobp))
166                            (> (read (current-buffer)) number))
167                        (error
168                         ;; The line was malformed, so we just remove it!!
169                         (gnus-delete-line)
170                         t))
171                 (forward-line -1))
172               (if (bobp)
173                   (if (not (eobp))
174                       (progn
175                         (beginning-of-line)
176                         (when (< (read (current-buffer)) number)
177                           (forward-line 1)))
178                     (beginning-of-line))
179                 (forward-line 1))
180               (beginning-of-line)
181               ;; [number subject from date id references chars lines xref]
182               (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
183                               (mail-header-number headers)
184                               (let ((subject (mail-header-subject headers)))
185                                 (or (get-text-property 0 'raw-text subject)
186                                     subject))
187                               (let ((from (mail-header-from headers)))
188                                 (or (get-text-property 0 'raw-text from)
189                                     from))
190                               (mail-header-date headers)
191                               (mail-header-id headers)
192                               (or (mail-header-references headers) "")
193                               (or (mail-header-chars headers) "")
194                               (or (mail-header-lines headers) "")
195                               (or (mail-header-xref headers) "")))
196               ;; Update the active info.
197               (set-buffer gnus-summary-buffer)
198               (gnus-cache-update-active group number)
199               (push article gnus-newsgroup-cached)
200               (gnus-summary-update-secondary-mark article))
201             t))))))
202
203 (defun gnus-cache-enter-remove-article (article)
204   "Mark ARTICLE for later possible removal."
205   (when article
206     (push article gnus-cache-removable-articles)))
207
208 (defun gnus-cache-possibly-remove-articles ()
209   "Possibly remove some of the removable articles."
210   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
211       (gnus-cache-possibly-remove-articles-1)
212     (let ((arts gnus-cache-removable-articles)
213           ga)
214       (while arts
215         (when (setq ga (nnvirtual-find-group-art
216                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
217           (let ((gnus-cache-removable-articles (list (cdr ga)))
218                 (gnus-newsgroup-name (car ga)))
219             (gnus-cache-possibly-remove-articles-1)))))
220     (setq gnus-cache-removable-articles nil)))
221
222 (defun gnus-cache-possibly-remove-articles-1 ()
223   "Possibly remove some of the removable articles."
224   (unless (eq gnus-use-cache 'passive)
225     (let ((articles gnus-cache-removable-articles)
226           (cache-articles gnus-newsgroup-cached)
227           article)
228       (gnus-cache-change-buffer gnus-newsgroup-name)
229       (while articles
230         (when (memq (setq article (pop articles)) cache-articles)
231           ;; The article was in the cache, so we see whether we are
232           ;; supposed to remove it from the cache.
233           (gnus-cache-possibly-remove-article
234            article (memq article gnus-newsgroup-marked)
235            (memq article gnus-newsgroup-dormant)
236            (or (memq article gnus-newsgroup-unreads)
237                (memq article gnus-newsgroup-unselected))))))
238     ;; The overview file might have been modified, save it
239     ;; safe because we're only called at group exit anyway.
240     (gnus-cache-save-buffers)))
241
242 (defun gnus-cache-request-article (article group)
243   "Retrieve ARTICLE in GROUP from the cache."
244   (let ((file (gnus-cache-file-name group article))
245         (buffer-read-only nil))
246     (when (file-exists-p file)
247       (erase-buffer)
248       (gnus-kill-all-overlays)
249       (nnheader-insert-file-contents file)
250       t)))
251
252 (defun gnus-cache-possibly-alter-active (group active)
253   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
254   (when gnus-cache-active-hashtb
255     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
256       (when cache-active
257         (when (< (car cache-active) (car active))
258           (setcar active (car cache-active)))
259         (when (> (cdr cache-active) (cdr active))
260           (setcdr active (cdr cache-active)))))))
261
262 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
263   "Retrieve the headers for ARTICLES in GROUP."
264   (let ((cached
265          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
266     (if (not cached)
267         ;; No cached articles here, so we just retrieve them
268         ;; the normal way.
269         (let ((gnus-use-cache nil))
270           (gnus-retrieve-headers articles group fetch-old))
271       (let ((uncached-articles (gnus-sorted-intersection
272                                 (gnus-sorted-complement articles cached)
273                                 articles))
274             (cache-file (gnus-cache-file-name group ".overview"))
275             type)
276         ;; We first retrieve all the headers that we don't have in
277         ;; the cache.
278         (let ((gnus-use-cache nil))
279           (when uncached-articles
280             (setq type (and articles
281                             (gnus-retrieve-headers
282                              uncached-articles group fetch-old)))))
283         (gnus-cache-save-buffers)
284         ;; Then we insert the cached headers.
285         (save-excursion
286           (cond
287            ((not (file-exists-p cache-file))
288             ;; There are no cached headers.
289             type)
290            ((null type)
291             ;; There were no uncached headers (or retrieval was
292             ;; unsuccessful), so we use the cached headers exclusively.
293             (set-buffer nntp-server-buffer)
294             (erase-buffer)
295             (nnheader-insert-file-contents cache-file)
296             'nov)
297            ((eq type 'nov)
298             ;; We have both cached and uncached NOV headers, so we
299             ;; braid them.
300             (gnus-cache-braid-nov group cached)
301             type)
302            (t
303             ;; We braid HEADs.
304             (gnus-cache-braid-heads group (gnus-sorted-intersection
305                                            cached articles))
306             type)))))))
307
308 (defun gnus-cache-enter-article (&optional n)
309   "Enter the next N articles into the cache.
310 If not given a prefix, use the process marked articles instead.
311 Returns the list of articles entered."
312   (interactive "P")
313   (let ((articles (gnus-summary-work-articles n))
314         article out)
315     (while (setq article (pop articles))
316       (gnus-summary-remove-process-mark article)
317       (if (natnump article)
318           (when (gnus-cache-possibly-enter-article
319                  gnus-newsgroup-name article
320                  (gnus-summary-article-header article)
321                  nil nil nil t)
322             (push article out))
323         (gnus-message 2 "Can't cache article %d" article))
324       (gnus-summary-update-secondary-mark article))
325     (gnus-summary-next-subject 1)
326     (gnus-summary-position-point)
327     (nreverse out)))
328
329 (defun gnus-cache-remove-article (n)
330   "Remove the next N articles from the cache.
331 If not given a prefix, use the process marked articles instead.
332 Returns the list of articles removed."
333   (interactive "P")
334   (gnus-cache-change-buffer gnus-newsgroup-name)
335   (let ((articles (gnus-summary-work-articles n))
336         article out)
337     (while articles
338       (setq article (pop articles))
339       (gnus-summary-remove-process-mark article)
340       (when (gnus-cache-possibly-remove-article article nil nil nil t)
341         (push article out))
342       (gnus-summary-update-secondary-mark article))
343     (gnus-summary-next-subject 1)
344     (gnus-summary-position-point)
345     (nreverse out)))
346
347 (defun gnus-cached-article-p (article)
348   "Say whether ARTICLE is cached in the current group."
349   (memq article gnus-newsgroup-cached))
350
351 (defun gnus-summary-insert-cached-articles ()
352   "Insert all the articles cached for this group into the current buffer."
353   (interactive)
354   (let ((cached gnus-newsgroup-cached)
355         (gnus-verbose (max 6 gnus-verbose)))
356     (unless cached
357       (gnus-message 3 "No cached articles for this group"))
358     (while cached
359       (gnus-summary-goto-subject (pop cached) t))))
360
361 (defalias 'gnus-summary-limit-include-cached
362   'gnus-summary-insert-cached-articles)
363
364 ;;; Internal functions.
365
366 (defun gnus-cache-change-buffer (group)
367   (and gnus-cache-buffer
368        ;; See if the current group's overview cache has been loaded.
369        (or (string= group (car gnus-cache-buffer))
370            ;; Another overview cache is current, save it.
371            (gnus-cache-save-buffers)))
372   ;; if gnus-cache buffer is nil, create it
373   (unless gnus-cache-buffer
374     ;; Create cache buffer
375     (save-excursion
376       (setq gnus-cache-buffer
377             (cons group
378                   (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
379       (buffer-disable-undo (current-buffer))
380       ;; Insert the contents of this group's cache overview.
381       (erase-buffer)
382       (let ((file (gnus-cache-file-name group ".overview")))
383         (when (file-exists-p file)
384           (nnheader-insert-file-contents file)))
385       ;; We have a fresh (empty/just loaded) buffer,
386       ;; mark it as unmodified to save a redundant write later.
387       (set-buffer-modified-p nil))))
388
389 ;; Return whether an article is a member of a class.
390 (defun gnus-cache-member-of-class (class ticked dormant unread)
391   (or (and ticked (memq 'ticked class))
392       (and dormant (memq 'dormant class))
393       (and unread (memq 'unread class))
394       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
395
396 (defun gnus-cache-file-name (group article)
397   (concat (file-name-as-directory gnus-cache-directory)
398           (file-name-as-directory
399            (nnheader-translate-file-chars
400             (if (gnus-use-long-file-name 'not-cache)
401                 group
402               (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
403                 ;; Translate the first colon into a slash.
404                 (when (string-match ":" group)
405                   (aset group (match-beginning 0) ?/))
406                 (nnheader-replace-chars-in-string group ?. ?/)))
407             t))
408           (if (stringp article) article (int-to-string article))))
409
410 (defun gnus-cache-update-article (group article)
411   "If ARTICLE is in the cache, remove it and re-enter it."
412   (gnus-cache-change-buffer group)
413   (when (gnus-cache-possibly-remove-article article nil nil nil t)    
414     (let ((gnus-use-cache nil))
415       (gnus-cache-possibly-enter-article
416        gnus-newsgroup-name article (gnus-summary-article-header article)
417        nil nil nil t))))
418
419 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
420                                                    &optional force)
421   "Possibly remove ARTICLE from the cache."
422   (let ((group gnus-newsgroup-name)
423         (number article)
424         file)
425     ;; If this is a virtual group, we find the real group.
426     (when (gnus-virtual-group-p group)
427       (let ((result (nnvirtual-find-group-art
428                      (gnus-group-real-name group) article)))
429         (setq group (car result)
430               number (cdr result))))
431     (setq file (gnus-cache-file-name group number))
432     (when (and (file-exists-p file)
433                (or force
434                    (gnus-cache-member-of-class
435                     gnus-cache-remove-articles ticked dormant unread)))
436       (save-excursion
437         (delete-file file)
438         (set-buffer (cdr gnus-cache-buffer))
439         (goto-char (point-min))
440         (when (or (looking-at (concat (int-to-string number) "\t"))
441                   (search-forward (concat "\n" (int-to-string number) "\t")
442                                   (point-max) t))
443           (delete-region (progn (beginning-of-line) (point))
444                          (progn (forward-line 1) (point)))))
445       (setq gnus-newsgroup-cached
446             (delq article gnus-newsgroup-cached))
447       (gnus-summary-update-secondary-mark article)
448       t)))
449
450 (defun gnus-cache-articles-in-group (group)
451   "Return a sorted list of cached articles in GROUP."
452   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
453         articles)
454     (when (file-exists-p dir)
455       (setq articles
456             (sort (mapcar (lambda (name) (string-to-int name))
457                           (directory-files dir nil "^[0-9]+$" t))
458                   '<))
459       ;; Update the cache active file, just to synch more.
460       (when articles
461         (gnus-cache-update-active group (car articles) t)
462         (gnus-cache-update-active group (car (last articles))))
463       articles)))
464
465 (defun gnus-cache-braid-nov (group cached &optional file)
466   (let ((cache-buf (get-buffer-create " *gnus-cache*"))
467         beg end)
468     (gnus-cache-save-buffers)
469     (save-excursion
470       (set-buffer cache-buf)
471       (buffer-disable-undo (current-buffer))
472       (erase-buffer)
473       (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
474       (goto-char (point-min))
475       (insert "\n")
476       (goto-char (point-min)))
477     (set-buffer nntp-server-buffer)
478     (goto-char (point-min))
479     (while cached
480       (while (and (not (eobp))
481                   (< (read (current-buffer)) (car cached)))
482         (forward-line 1))
483       (beginning-of-line)
484       (save-excursion
485         (set-buffer cache-buf)
486         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
487                             nil t)
488             (setq beg (progn (beginning-of-line) (point))
489                   end (progn (end-of-line) (point)))
490           (setq beg nil)))
491       (when beg
492         (insert-buffer-substring cache-buf beg end)
493         (insert "\n"))
494       (setq cached (cdr cached)))
495     (kill-buffer cache-buf)))
496
497 (defun gnus-cache-braid-heads (group cached)
498   (let ((cache-buf (get-buffer-create " *gnus-cache*")))
499     (save-excursion
500       (set-buffer cache-buf)
501       (buffer-disable-undo (current-buffer))
502       (erase-buffer))
503     (set-buffer nntp-server-buffer)
504     (goto-char (point-min))
505     (while cached
506       (while (and (not (eobp))
507                   (looking-at "2.. +\\([0-9]+\\) ")
508                   (< (progn (goto-char (match-beginning 1))
509                             (read (current-buffer)))
510                      (car cached)))
511         (search-forward "\n.\n" nil 'move))
512       (beginning-of-line)
513       (save-excursion
514         (set-buffer cache-buf)
515         (erase-buffer)
516         (nnheader-insert-file-contents (gnus-cache-file-name group (car cached)))
517         (goto-char (point-min))
518         (insert "220 ")
519         (princ (car cached) (current-buffer))
520         (insert " Article retrieved.\n")
521         (search-forward "\n\n" nil 'move)
522         (delete-region (point) (point-max))
523         (forward-char -1)
524         (insert "."))
525       (insert-buffer-substring cache-buf)
526       (setq cached (cdr cached)))
527     (kill-buffer cache-buf)))
528
529 ;;;###autoload
530 (defun gnus-jog-cache ()
531   "Go through all groups and put the articles into the cache.
532
533 Usage:
534 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
535   (interactive)
536   (let ((gnus-mark-article-hook nil)
537         (gnus-expert-user t)
538         (nnmail-spool-file nil)
539         (gnus-use-dribble-file nil)
540         (gnus-novice-user nil)
541         (gnus-large-newsgroup nil))
542     ;; Start Gnus.
543     (gnus)
544     ;; Go through all groups...
545     (gnus-group-mark-buffer)
546     (gnus-group-iterate nil
547       (lambda (group)
548         (let (gnus-auto-select-next)
549           (gnus-summary-read-group group nil t)
550           ;; ... and enter the articles into the cache.
551           (when (eq major-mode 'gnus-summary-mode)
552             (gnus-uu-mark-buffer)
553             (gnus-cache-enter-article)
554             (kill-buffer (current-buffer))))))))
555
556 (defun gnus-cache-read-active (&optional force)
557   "Read the cache active file."
558   (gnus-make-directory gnus-cache-directory)
559   (if (or (not (file-exists-p gnus-cache-active-file))
560           (not (zerop (nth 7 (file-attributes gnus-cache-active-file))))
561           force)
562       ;; There is no active file, so we generate one.
563       (gnus-cache-generate-active)
564     ;; We simply read the active file.
565     (save-excursion
566       (gnus-set-work-buffer)
567       (nnheader-insert-file-contents gnus-cache-active-file)
568       (gnus-active-to-gnus-format
569        nil (setq gnus-cache-active-hashtb
570                  (gnus-make-hashtable
571                   (count-lines (point-min) (point-max)))))
572       (setq gnus-cache-active-altered nil))))
573
574 (defun gnus-cache-write-active (&optional force)
575   "Write the active hashtb to the active file."
576   (when (or force
577             (and gnus-cache-active-hashtb
578                  gnus-cache-active-altered))
579     (nnheader-temp-write gnus-cache-active-file
580       (mapatoms
581        (lambda (sym)
582          (when (and sym (boundp sym))
583            (insert (format "%s %d %d y\n"
584                            (symbol-name sym) (cdr (symbol-value sym))
585                            (car (symbol-value sym))))))
586        gnus-cache-active-hashtb))
587     ;; Mark the active hashtb as unaltered.
588     (setq gnus-cache-active-altered nil)))
589
590 (defun gnus-cache-update-active (group number &optional low)
591   "Update the upper bound of the active info of GROUP to NUMBER.
592 If LOW, update the lower bound instead."
593   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
594     (if (null active)
595         ;; We just create a new active entry for this group.
596         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
597       ;; Update the lower or upper bound.
598       (if low
599           (setcar active number)
600         (setcdr active number)))
601     ;; Mark the active hashtb as altered.
602     (setq gnus-cache-active-altered t)))
603
604 ;;;###autoload
605 (defun gnus-cache-generate-active (&optional directory)
606   "Generate the cache active file."
607   (interactive)
608   (let* ((top (null directory))
609          (directory (expand-file-name (or directory gnus-cache-directory)))
610          (files (directory-files directory 'full))
611          (group
612           (if top
613               ""
614             (string-match
615              (concat "^" (regexp-quote
616                           (file-name-as-directory
617                            (expand-file-name gnus-cache-directory))))
618              (directory-file-name directory))
619             (nnheader-replace-chars-in-string
620              (substring (directory-file-name directory) (match-end 0))
621              ?/ ?.)))
622          nums alphs)
623     (when top
624       (gnus-message 5 "Generating the cache active file...")
625       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
626     ;; Separate articles from all other files and directories.
627     (while files
628       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
629           (push (string-to-int (file-name-nondirectory (pop files))) nums)
630         (push (pop files) alphs)))
631     ;; If we have nums, then this is probably a valid group.
632     (when (setq nums (sort nums '<))
633       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
634                     gnus-cache-active-hashtb))
635     ;; Go through all the other files.
636     (while alphs
637       (when (and (file-directory-p (car alphs))
638                  (not (string-match "^\\.\\.?$"
639                                     (file-name-nondirectory (car alphs)))))
640         ;; We descend directories.
641         (gnus-cache-generate-active (car alphs)))
642       (setq alphs (cdr alphs)))
643     ;; Write the new active file.
644     (when top
645       (gnus-cache-write-active t)
646       (gnus-message 5 "Generating the cache active file...done"))))
647
648 ;;;###autoload
649 (defun gnus-cache-generate-nov-databases (dir)
650   "Generate NOV files recursively starting in DIR."
651   (interactive (list gnus-cache-directory))
652   (gnus-cache-close)
653   (let ((nnml-generate-active-function 'identity))
654     (nnml-generate-nov-databases-1 dir)))
655
656 (defun gnus-cache-move-cache (dir)
657   "Move the cache tree to somewhere else."
658   (interactive "FMove the cache tree to: ")
659   (rename-file gnus-cache-directory dir))
660
661 (provide 'gnus-cache)
662
663 ;;; gnus-cache.el ends here