]>
Commit | Line | Data |
---|---|---|
6b3a8ea3 | 1 | /* $Id$ |
2 | * | |
3 | * This generates the post office location data. | |
4 | * | |
5 | * tom@mit.edu | |
6 | * | |
7 | * Copyright 1998 by the Massachusetts Institute of Technology. | |
8 | * For copying and distribution information, please see the file | |
9 | * <mit-copyright.h>. | |
10 | */ | |
11 | ||
12 | #include <mit-copyright.h> | |
13 | #include <moira.h> | |
14 | #include <moira_site.h> | |
15 | #include <sys/stat.h> | |
16 | #include <stdio.h> | |
17 | #include <string.h> | |
18 | #include <time.h> | |
19 | #include <errno.h> | |
20 | #include "util.h" | |
21 | ||
22 | EXEC SQL INCLUDE sqlca; | |
23 | ||
24 | RCSID("$Header$"); | |
25 | ||
26 | ||
27 | #ifndef PO_SUBDIR | |
28 | #define PO_SUBDIR "postoffice" | |
29 | #endif | |
30 | ||
31 | #define MAX_RECSIZE 1000 | |
32 | ||
33 | char *whoami = "postoffice.gen"; | |
34 | char *db = "moira/moira"; | |
35 | ||
36 | void pobox(char *); | |
37 | ||
38 | ||
39 | int main(int argc, char **argv) | |
40 | { | |
41 | char cmd[BUFSIZ]; | |
42 | char po_dir[MAXPATHLEN], wpo_dir[MAXPATHLEN]; | |
43 | struct stat sb; | |
44 | char *c; | |
45 | int flag; | |
46 | ||
47 | initialize_sms_error_table(); | |
48 | ||
49 | if (argc == 1) | |
50 | sprintf(po_dir, "%s/%s", DCM_DIR, PO_SUBDIR); | |
51 | else | |
52 | { | |
53 | sprintf(po_dir, "%s", argv[1]); | |
54 | /* fake out dcm - we'll append .out later */ | |
55 | if (c = strrchr(po_dir, '.')) | |
56 | *c = '\0'; | |
57 | } | |
58 | sprintf(wpo_dir, "%s~", po_dir); | |
59 | ||
60 | if (argc > 2) | |
61 | { | |
62 | fprintf(stderr, "usage: %s [outdir]\n", argv[0]); | |
63 | exit(MR_ARGS); | |
64 | } | |
65 | ||
66 | if (stat(wpo_dir, &sb) < 0) | |
67 | { | |
68 | if (errno == ENOENT) | |
69 | { | |
70 | if (mkdir(wpo_dir, 0700) < 0) | |
71 | { | |
72 | fprintf(stderr, "%s: unable to make directory %s (%s))\n", | |
73 | whoami, wpo_dir, strerror(errno)); | |
74 | exit(MR_CCONFIG); | |
75 | } | |
76 | } | |
77 | else | |
78 | { | |
79 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, wpo_dir, | |
80 | strerror(errno)); | |
81 | exit(MR_CCONFIG); | |
82 | } | |
83 | } | |
84 | else if (!S_ISDIR(sb.st_mode)) | |
85 | { | |
86 | fprintf(stderr, "%s: %s not a directory\n" , whoami, wpo_dir); | |
87 | exit(MR_CCONFIG); | |
88 | } | |
89 | ||
90 | EXEC SQL CONNECT :db; | |
91 | ||
92 | flag = 0; | |
93 | if (stat(po_dir, &sb) == 0) | |
94 | { | |
95 | if (ModDiff (&flag, "filesys", sb.st_mtime)) | |
96 | exit(MR_DATE); | |
97 | } | |
98 | if (flag < 0) | |
99 | { | |
100 | fprintf(stderr, "%s: %s does not need to be rebuilt.\n", whoami, po_dir); | |
101 | unlink(wpo_dir); | |
102 | } | |
103 | else | |
104 | { | |
105 | fprintf(stderr, "%s: building po databases...\n", whoami); | |
106 | pobox(wpo_dir); | |
107 | ||
108 | if (stat(po_dir, &sb) != -1) | |
109 | { | |
110 | fprintf(stderr, "%s: removing %s\n", whoami, po_dir); | |
111 | sprintf(cmd, "rm -r %s", po_dir); | |
112 | if (strncmp(po_dir, wpo_dir, strlen(po_dir))) | |
113 | exit(MR_CCONFIG); | |
114 | if (system(cmd)) | |
115 | { | |
116 | fprintf(stderr, "%s: unable to remove %s (%s)", whoami, po_dir, | |
117 | strerror(errno)); | |
118 | exit(MR_CCONFIG); | |
119 | } | |
120 | } | |
121 | ||
122 | rename(wpo_dir, po_dir); | |
123 | } | |
124 | ||
125 | fprintf(stderr, "%s: building tar file...\n", whoami); | |
126 | sprintf(cmd, "(cd %s; tar cf - . ) > %s.out", po_dir, po_dir); | |
127 | if (system(cmd)) | |
128 | exit(MR_TAR_FAIL); | |
129 | ||
130 | exit(MR_SUCCESS); | |
131 | } | |
132 | ||
133 | void pobox(char *path) | |
134 | { | |
135 | char *c; | |
136 | char mdir[MAXPATHLEN]; | |
137 | char mout[MAXPATHLEN], qout[MAXPATHLEN]; | |
138 | struct stat sb; | |
139 | FILE *moutp, *qoutp; | |
140 | ||
141 | EXEC SQL BEGIN DECLARE SECTION; | |
142 | char machine[MACHINE_NAME_SIZE]; | |
143 | char label[FILESYS_LABEL_SIZE], type[FILESYS_TYPE_SIZE]; | |
144 | char partition[NFSPHYS_DIR_SIZE]; | |
145 | int mid, quota; | |
146 | EXEC SQL END DECLARE SECTION; | |
147 | ||
148 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
149 | ||
150 | EXEC SQL DECLARE po_curses CURSOR FOR | |
151 | SELECT m.name, m.mach_id from machine m, serverhosts s | |
152 | WHERE s.service='POSTOFFICE' AND s.mach_id=m.mach_id | |
153 | ORDER by m.name; | |
154 | EXEC SQL OPEN po_curses; | |
155 | ||
156 | while (1) | |
157 | { | |
158 | EXEC SQL FETCH po_curses INTO :machine, :mid; | |
159 | if (sqlca.sqlcode) | |
160 | break; | |
161 | if (!*strtrim(machine)) | |
162 | continue; | |
163 | ||
164 | sprintf(mdir, "%s/%s", path, machine); | |
165 | if (stat(mdir, &sb) < 0) | |
166 | { | |
167 | if (errno == ENOENT) | |
168 | { | |
169 | if (mkdir(mdir, 0700) < 0) | |
170 | { | |
171 | fprintf(stderr, "%s: unable to make directory %s (%s)\n", | |
172 | whoami, mdir, strerror(errno)); | |
173 | exit(MR_CCONFIG); | |
174 | } | |
175 | } | |
176 | else | |
177 | { | |
178 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, mdir, | |
179 | strerror(errno)); | |
180 | exit(MR_CCONFIG); | |
181 | } | |
182 | } | |
183 | sprintf(mout, "%s/mailboxes", mdir); | |
184 | sprintf(qout, "%s/quota", mdir); | |
185 | if (!(moutp = fopen(mout, "w"))) | |
186 | { | |
187 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, | |
188 | mout, strerror(errno)); | |
189 | exit(MR_OCONFIG); | |
190 | } | |
191 | ||
192 | if (!(qoutp = fopen(qout, "w"))) | |
193 | { | |
194 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, | |
195 | qout, strerror(errno)); | |
196 | exit(MR_OCONFIG); | |
197 | } | |
198 | ||
199 | EXEC SQL DECLARE filsys_cursor CURSOR FOR | |
200 | SELECT f.label, f.lockertype, p.dir, q.quota | |
201 | FROM filesys f, nfsphys p, quota q | |
202 | WHERE f.mach_id=:mid AND f.type='IMAP' AND f.createflg!=0 | |
203 | AND f.filsys_id=q.filsys_id | |
204 | AND f.mach_id=p.mach_id AND f.phys_id=p.nfsphys_id | |
205 | ORDER BY f.label, p.dir, q.quota; | |
206 | EXEC SQL OPEN filsys_cursor; | |
207 | ||
208 | while (1) | |
209 | { | |
210 | EXEC SQL FETCH filsys_cursor INTO :label, :type, :partition, :quota; | |
211 | if (sqlca.sqlcode) | |
212 | break; | |
213 | if (!*strtrim(label)) | |
214 | continue; | |
215 | if (!*strtrim(type)) | |
216 | continue; | |
217 | if (!*strtrim(partition)) | |
218 | continue; | |
219 | ||
220 | if (!strncmp(&label[strlen(label)-3], ".po", 3)) | |
221 | label[strlen(label)-3] = '\0'; | |
222 | lowercase(label); | |
223 | lowercase(type); | |
224 | lowercase(partition); | |
225 | ||
226 | fprintf(moutp, "%s.%s\t%s\t%s\t%s\n", type, label, partition, | |
227 | label, "lrswipcda"); | |
228 | fprintf(qoutp, "%s.%s\t%d\n", type, label, quota); | |
229 | } | |
230 | ||
231 | EXEC SQL CLOSE filsys_cursor; | |
232 | EXEC SQL COMMIT; | |
233 | ||
234 | if (fclose(moutp)) | |
235 | { | |
236 | fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami, | |
237 | mout, strerror(errno)); | |
238 | exit(MR_CCONFIG); | |
239 | } | |
240 | ||
241 | if (fclose(qoutp)) | |
242 | { | |
243 | fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami, | |
244 | qout, strerror(errno)); | |
245 | exit(MR_CCONFIG); | |
246 | } | |
247 | } | |
248 | ||
249 | EXEC SQL CLOSE po_curses; | |
250 | EXEC SQL COMMIT; | |
251 | return; | |
252 | ||
253 | sqlerr: | |
254 | db_error(sqlca.sqlcode); | |
255 | exit(MR_DBMS_ERR); | |
256 | } |