File: sndumpdb.c

package info (click to toggle)
sn 0.3.4a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 784 kB
  • ctags: 826
  • sloc: ansic: 9,023; sh: 339; makefile: 208
file content (142 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright  1998-2000 Harold Tay.
 * Copyright  2000- Patrik Rdman.
 */

/*
 * Dump database.
 */

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include "config.h"
#include "allocate.h"
#include "newsgroup.h"
#include "dhash.h"
#include "parameters.h"
#include <format.h>
#include <out.h>
#include <opt.h>

static const char rcsid[] = "$Id$";

int debug = 0;
int outfd = 1;

int dh_dump (int next)
{
   int i;
   int nr = 0;
   unsigned index;
   struct chain *chp;
   int empties = 0;
   int total = 0;
   int maxlen = 0;
   int bytes = 0;

   for (i = 0; i < DH_SIZE; i++)
   {
      unsigned char *x = dh_table->next + i * 3;

      index = char3toint(x);
      if (!index)
         empties++;
      else
      {
         int len = 0;

         for (; index; index = chp->next)
         {
            char *group;
            int ident;

            chp = allo_deref(index);
            if (NULL == chp)
            {
               LOG("dh_dump:allo_deref returned bad ref");
               return (0 - nr);
            }
            ident = char2toint(chp->newsgroup);
            group = ng_newsgroup(ident);
            if (NULL == group)
            {
               LOG("dh_dump:bad newsgroup for ident %d", ident);
               return (0 - nr);
            }
            if (!next)
               writef(outfd, "%s %d <%s>\n", group, chp->serial, chp->messageid);
            else
               writef(outfd, "[%d,%d]%s %d <%s>\n",
                      index, chp->next, group, chp->serial, chp->messageid);
            nr++;
            len++;
            bytes += strlen(chp->messageid);
         }
         total += len;
         if (len > maxlen)
            maxlen = len;
      }
   }

   log("%d chains of %d empty", empties, DH_SIZE);
   log("Total %d entries", total);
   if (empties < DH_SIZE)
      log("for average chain length of %d/100", (total * 100) / (DH_SIZE - empties));
   log("with max chain length of %d", maxlen);
   if (total)
      log("Average length of ID is %d/100", (bytes * 100) / total);
   return (nr);
}

#define usage() fail(1, "Usage: %s [-i] [-o file]", progname)

int main (int argc, char **argv)
{
   int nr;
   char *cp;
   int withnext = 0;
   int i;

   progname = ((cp = strrchr(argv[0], '/')) ? cp + 1 : argv[0]);

   parameters(0);

   if (-1 == chdir(snroot))
      FAIL(2, "chdir(%s):%m", snroot);

   while ((i = opt_get(argc, argv, "o")) > -1)
      switch (i)
      {
         case 'P': log_with_pid(); break;
         case 'd': debug++; break;
         case 'V': version(); _exit(0);
         case 'i': withnext = 1; break;
         case 'o':
            if (!opt_arg)
               usage();
            outfd = open(opt_arg, O_WRONLY | O_CREAT, 0644);
            if (-1 == outfd)
               fail(2, "open(%s):%m", opt_arg);
            break;
         default: usage();
      }
   if (opt_ind < argc)
      usage();

   if (-1 == dh_open(0, 1))
      _exit(2);

   nr = dh_dump(withnext);
   if (nr < 0)
   {
      LOG("Error (%m?) while reading record number %d", 0 - nr);
      dh_close();
      _exit(2);
   }
   dh_close();
   _exit(0);
}