]>
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; | |
6b3a8ea3 | 45 | |
46 | initialize_sms_error_table(); | |
47 | ||
48 | if (argc == 1) | |
49 | sprintf(po_dir, "%s/%s", DCM_DIR, PO_SUBDIR); | |
50 | else | |
51 | { | |
52 | sprintf(po_dir, "%s", argv[1]); | |
53 | /* fake out dcm - we'll append .out later */ | |
54 | if (c = strrchr(po_dir, '.')) | |
55 | *c = '\0'; | |
56 | } | |
57 | sprintf(wpo_dir, "%s~", po_dir); | |
58 | ||
59 | if (argc > 2) | |
60 | { | |
61 | fprintf(stderr, "usage: %s [outdir]\n", argv[0]); | |
62 | exit(MR_ARGS); | |
63 | } | |
64 | ||
65 | if (stat(wpo_dir, &sb) < 0) | |
66 | { | |
67 | if (errno == ENOENT) | |
68 | { | |
69 | if (mkdir(wpo_dir, 0700) < 0) | |
70 | { | |
71 | fprintf(stderr, "%s: unable to make directory %s (%s))\n", | |
72 | whoami, wpo_dir, strerror(errno)); | |
73 | exit(MR_CCONFIG); | |
74 | } | |
75 | } | |
76 | else | |
77 | { | |
78 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, wpo_dir, | |
79 | strerror(errno)); | |
80 | exit(MR_CCONFIG); | |
81 | } | |
82 | } | |
83 | else if (!S_ISDIR(sb.st_mode)) | |
84 | { | |
85 | fprintf(stderr, "%s: %s not a directory\n" , whoami, wpo_dir); | |
86 | exit(MR_CCONFIG); | |
87 | } | |
88 | ||
89 | EXEC SQL CONNECT :db; | |
90 | ||
a589d269 | 91 | fprintf(stderr, "%s: building po databases...\n", whoami); |
92 | pobox(wpo_dir); | |
6b3a8ea3 | 93 | |
a589d269 | 94 | if (stat(po_dir, &sb) != -1) |
95 | { | |
96 | fprintf(stderr, "%s: removing %s\n", whoami, po_dir); | |
97 | sprintf(cmd, "rm -r %s", po_dir); | |
98 | if (strncmp(po_dir, wpo_dir, strlen(po_dir))) | |
99 | exit(MR_CCONFIG); | |
100 | if (system(cmd)) | |
6b3a8ea3 | 101 | { |
a589d269 | 102 | fprintf(stderr, "%s: unable to remove %s (%s)", whoami, po_dir, |
103 | strerror(errno)); | |
104 | exit(MR_CCONFIG); | |
6b3a8ea3 | 105 | } |
6b3a8ea3 | 106 | } |
107 | ||
a589d269 | 108 | rename(wpo_dir, po_dir); |
109 | ||
6b3a8ea3 | 110 | fprintf(stderr, "%s: building tar file...\n", whoami); |
111 | sprintf(cmd, "(cd %s; tar cf - . ) > %s.out", po_dir, po_dir); | |
112 | if (system(cmd)) | |
113 | exit(MR_TAR_FAIL); | |
114 | ||
115 | exit(MR_SUCCESS); | |
116 | } | |
117 | ||
118 | void pobox(char *path) | |
119 | { | |
120 | char *c; | |
121 | char mdir[MAXPATHLEN]; | |
122 | char mout[MAXPATHLEN], qout[MAXPATHLEN]; | |
123 | struct stat sb; | |
124 | FILE *moutp, *qoutp; | |
125 | ||
126 | EXEC SQL BEGIN DECLARE SECTION; | |
127 | char machine[MACHINE_NAME_SIZE]; | |
128 | char label[FILESYS_LABEL_SIZE], type[FILESYS_TYPE_SIZE]; | |
129 | char partition[NFSPHYS_DIR_SIZE]; | |
130 | int mid, quota; | |
131 | EXEC SQL END DECLARE SECTION; | |
132 | ||
133 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
134 | ||
135 | EXEC SQL DECLARE po_curses CURSOR FOR | |
136 | SELECT m.name, m.mach_id from machine m, serverhosts s | |
137 | WHERE s.service='POSTOFFICE' AND s.mach_id=m.mach_id | |
138 | ORDER by m.name; | |
139 | EXEC SQL OPEN po_curses; | |
140 | ||
141 | while (1) | |
142 | { | |
143 | EXEC SQL FETCH po_curses INTO :machine, :mid; | |
144 | if (sqlca.sqlcode) | |
145 | break; | |
146 | if (!*strtrim(machine)) | |
147 | continue; | |
148 | ||
149 | sprintf(mdir, "%s/%s", path, machine); | |
150 | if (stat(mdir, &sb) < 0) | |
151 | { | |
152 | if (errno == ENOENT) | |
153 | { | |
154 | if (mkdir(mdir, 0700) < 0) | |
155 | { | |
156 | fprintf(stderr, "%s: unable to make directory %s (%s)\n", | |
157 | whoami, mdir, strerror(errno)); | |
158 | exit(MR_CCONFIG); | |
159 | } | |
160 | } | |
161 | else | |
162 | { | |
163 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, mdir, | |
164 | strerror(errno)); | |
165 | exit(MR_CCONFIG); | |
166 | } | |
167 | } | |
168 | sprintf(mout, "%s/mailboxes", mdir); | |
169 | sprintf(qout, "%s/quota", mdir); | |
170 | if (!(moutp = fopen(mout, "w"))) | |
171 | { | |
172 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, | |
173 | mout, strerror(errno)); | |
174 | exit(MR_OCONFIG); | |
175 | } | |
176 | ||
177 | if (!(qoutp = fopen(qout, "w"))) | |
178 | { | |
179 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, | |
180 | qout, strerror(errno)); | |
181 | exit(MR_OCONFIG); | |
182 | } | |
183 | ||
184 | EXEC SQL DECLARE filsys_cursor CURSOR FOR | |
185 | SELECT f.label, f.lockertype, p.dir, q.quota | |
186 | FROM filesys f, nfsphys p, quota q | |
187 | WHERE f.mach_id=:mid AND f.type='IMAP' AND f.createflg!=0 | |
188 | AND f.filsys_id=q.filsys_id | |
189 | AND f.mach_id=p.mach_id AND f.phys_id=p.nfsphys_id | |
190 | ORDER BY f.label, p.dir, q.quota; | |
191 | EXEC SQL OPEN filsys_cursor; | |
192 | ||
193 | while (1) | |
194 | { | |
195 | EXEC SQL FETCH filsys_cursor INTO :label, :type, :partition, :quota; | |
196 | if (sqlca.sqlcode) | |
197 | break; | |
198 | if (!*strtrim(label)) | |
199 | continue; | |
200 | if (!*strtrim(type)) | |
201 | continue; | |
202 | if (!*strtrim(partition)) | |
203 | continue; | |
204 | ||
205 | if (!strncmp(&label[strlen(label)-3], ".po", 3)) | |
206 | label[strlen(label)-3] = '\0'; | |
207 | lowercase(label); | |
208 | lowercase(type); | |
209 | lowercase(partition); | |
210 | ||
211 | fprintf(moutp, "%s.%s\t%s\t%s\t%s\n", type, label, partition, | |
212 | label, "lrswipcda"); | |
213 | fprintf(qoutp, "%s.%s\t%d\n", type, label, quota); | |
214 | } | |
215 | ||
216 | EXEC SQL CLOSE filsys_cursor; | |
217 | EXEC SQL COMMIT; | |
218 | ||
219 | if (fclose(moutp)) | |
220 | { | |
221 | fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami, | |
222 | mout, strerror(errno)); | |
223 | exit(MR_CCONFIG); | |
224 | } | |
225 | ||
226 | if (fclose(qoutp)) | |
227 | { | |
228 | fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami, | |
229 | qout, strerror(errno)); | |
230 | exit(MR_CCONFIG); | |
231 | } | |
232 | } | |
233 | ||
234 | EXEC SQL CLOSE po_curses; | |
235 | EXEC SQL COMMIT; | |
236 | return; | |
237 | ||
238 | sqlerr: | |
239 | db_error(sqlca.sqlcode); | |
240 | exit(MR_DBMS_ERR); | |
241 | } |