]> andersk Git - moira.git/blob - dbck/phase4.pc
Code style cleanup. (No functional changes)
[moira.git] / dbck / phase4.pc
1 /* $Header$
2  *
3  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
4  *  For copying and distribution information, please see the file
5  *  <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include <stdlib.h>
10 #include <moira.h>
11 #include "dbck.h"
12 EXEC SQL INCLUDE sqlca;
13
14 static char phase4_qc_rcsid[] = "$Header$";
15
16 EXEC SQL WHENEVER SQLERROR DO dbmserr();
17
18 count_boxes(int id, struct user *u, struct hash *boxes)
19 {
20   int i;
21
22   if (u->potype == 'P')
23     {
24       if ((i = (int) hash_lookup(boxes, u->pobox_id)))
25         {
26           if (hash_store(boxes, u->pobox_id, i + 1) == -1)
27             out_of_mem("storing poboxes in hash table");
28         }
29       else
30         {
31           printf("User %s(%s) has pobox on non-POP server %d\n",
32                  u->fullname, u->login, u->pobox_id);
33           printf("Not fixing this error\n");
34         }
35     }
36 }
37
38
39 check_box_counts(int id, int cnt, struct hash *counts)
40 {
41   EXEC SQL BEGIN DECLARE SECTION;
42   int oldval, rowcount, iid = id, icnt = cnt;
43   EXEC SQL END DECLARE SECTION;
44
45   oldval = (int) hash_lookup(counts, id);
46   cnt--;
47   if (oldval != cnt)
48     {
49       printf("Count wrong on POBox machine %s; is %d in db, counted %d\n",
50              ((struct machine *) hash_lookup(machines, id))->name,
51              oldval, cnt);
52       if (single_fix("Update", 1))
53         {
54           EXEC SQL UPDATE serverhosts SET value1 = :icnt
55             WHERE service = 'POP' AND mach_id = :iid;
56           rowcount = sqlca.sqlerrd[2];
57           if (rowcount > 0)
58             printf("%d entr%s fixed\n", rowcount, rowcount == 1 ? "y" : "ies");
59           else
60             printf("Not fixed\n");
61           modified("serverhosts");
62         }
63     }
64 }
65
66
67 check_nfs_counts(int id, struct nfsphys *n, int hint)
68 {
69   EXEC SQL BEGIN DECLARE SECTION;
70   int iid = id, val, rowcount;
71   EXEC SQL END DECLARE SECTION;
72
73   val = n->count;
74   if (n->allocated != val)
75     {
76       printf("Count wrong on NFSphys %s:%s; is %d in db, counted %d\n",
77              ((struct machine *) hash_lookup(machines, n->mach_id))->name,
78              n->dir, n->allocated, val);
79       if (single_fix("Update", 1))
80         {
81           EXEC SQL UPDATE nfsphys SET allocated = :val
82             WHERE nfsphys_id = :iid;
83           rowcount = sqlca.sqlerrd[2];
84           if (rowcount > 0)
85             printf("%d entr%s fixed\n", rowcount, rowcount == 1 ? "y" : "ies");
86           else
87             printf("Not fixed\n");
88           modified("nfsphys");
89         }
90     }
91 }
92
93
94 phase4(void)
95 {
96   struct hash *boxes, *counts;
97   int id, cnt;
98
99   printf("Phase 4 - Checking counts\n");
100
101   dprintf("Doing POBoxes...\n");
102   boxes = create_hash(10);
103   counts = create_hash(10);
104   EXEC SQL DECLARE csr401 CURSOR FOR
105     SELECT mach_id, value1 FROM serverhosts
106     WHERE service = 'POP';
107   EXEC SQL OPEN csr401;
108   while (1)
109     {
110       EXEC SQL FETCH csr401 INTO :id, :cnt;
111       if (sqlca.sqlcode)
112         break;
113
114       if (hash_store(boxes, id, 1) == -1)
115         out_of_mem("storing poboxes");
116       if (hash_store(counts, id, cnt) == -1)
117         out_of_mem("storing pobox counts in hash table");
118     }
119   EXEC SQL CLOSE csr401;
120   hash_step(users, count_boxes, boxes);
121   hash_step(boxes, check_box_counts, counts);
122
123   dprintf("Doing NFSphys...\n");
124   hash_step(nfsphys, check_nfs_counts, 0);
125 }
126
127
128 count_only_setup(void)
129 {
130   EXEC SQL BEGIN DECLARE SECTION;
131   int id, status, id2, id3;
132   char name[33], last[17], first[17], buf[257];
133   EXEC SQL END DECLARE SECTION;
134   struct save_queue *sq;
135   struct user *u;
136   struct nfsphys *n;
137   struct machine *m;
138
139   dprintf("Loading users...\n");
140   users = create_hash(30000);
141   EXEC SQL DECLARE csr402 CURSOR FOR
142     SELECT users_id, login, last, first, status,
143     potype, pop_id, box_id FROM users
144     WHERE potype = 'POP';
145   EXEC SQL OPEN csr402;
146   while (1)
147     {
148       EXEC SQL FETCH csr402 INTO :id, :name, :last, :first, :status,
149         :buf, :id2, :id3;
150       if (sqlca.sqlcode)
151         break;
152
153       u = malloc(sizeof(struct user));
154       if (!u)
155         out_of_mem("storing users");
156       strcpy(u->login, strtrim(name));
157       u->potype = buf[0];
158       sprintf(buf, "%s, %s", strtrim(last), strtrim(first));
159       u->fullname = strsave(buf);
160       u->status = status;
161       u->users_id = id;
162       switch (u->potype)
163         {
164         case 'P':
165           u->pobox_id = id2;
166           break;
167         case 'S':
168           u->pobox_id = id3;
169           break;
170         default:
171           u->pobox_id = 0;
172         }
173       if (hash_store(users, id, u) == -1)
174         out_of_mem("storing users in hash table");
175     }
176   EXEC SQL CLOSE csr402;
177
178   dprintf("Loading machines...\n");
179   machines = create_hash(20000);
180   EXEC SQL DECLARE csr403 CURSOR FOR
181     SELECT mach_id, name FROM machine;
182   EXEC SQL OPEN csr403;
183   while (1)
184     {
185       EXEC SQL FETCH csr403 INTO :id, :name;
186       if (sqlca.sqlcode)
187         break;
188
189         m = malloc(sizeof(struct machine));
190         if (!m)
191           out_of_mem("storing machines");
192         strcpy(m->name, strtrim(name));
193         m->mach_id = id;
194         if (hash_store(machines, id, m) == -1)
195           out_of_mem("storing users in hash table");
196     }
197   EXEC SQL CLOSE csr403;
198
199   dprintf("Loading nfsphys...\n");
200   nfsphys = create_hash(500);
201   EXEC SQL DECLARE csr404 CURSOR FOR
202     SELECT nfsphys_id, dir, mach_id, allocated FROM nfsphys;
203   EXEC SQL OPEN csr404;
204   while (1)
205     {
206       EXEC SQL FETCH csr404 INTO :id, :name, :id2, :id3;
207       if (sqlca.sqlcode)
208         break;
209
210       n = malloc(sizeof(struct nfsphys));
211       if (!n)
212         out_of_mem("storing nfsphys");
213       strcpy(n->dir, strtrim(name));
214       n->mach_id = id2;
215       n->nfsphys_id = id;
216       n->allocated = id3;
217       n->count = 0;
218       if (hash_store(nfsphys, id, n) == -1)
219         out_of_mem("storing nfsphys in hash table");
220     }
221   EXEC SQL CLOSE csr404;
222
223   dprintf("Counting quotas...\n");
224   EXEC SQL DECLARE csr405 CURSOR FOR
225     SELECT phys_id, quota FROM quota;
226   EXEC SQL OPEN csr405;
227   while (1)
228     {
229       EXEC SQL FETCH csr405 INTO :id, :id2;
230       if (sqlca.sqlcode)
231         break;
232
233       if (n = (struct nfsphys  *) hash_lookup(nfsphys, id))
234         n->count += id2;
235     }
236   EXEC SQL CLOSE csr405;
237 }
This page took 0.065341 seconds and 5 git commands to generate.