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