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