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