3 * This generates the post office location data.
7 * Copyright 1998 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, please see the file
12 #include <mit-copyright.h>
14 #include <moira_site.h>
22 EXEC SQL INCLUDE sqlca;
28 #define PO_SUBDIR "postoffice"
31 #define MAX_RECSIZE 1000
33 char *whoami = "postoffice.gen";
34 char *db = "moira/moira";
39 int main(int argc, char **argv)
42 char po_dir[MAXPATHLEN], wpo_dir[MAXPATHLEN];
46 initialize_sms_error_table();
49 sprintf(po_dir, "%s/%s", DCM_DIR, PO_SUBDIR);
52 sprintf(po_dir, "%s", argv[1]);
53 /* fake out dcm - we'll append .out later */
54 if (c = strrchr(po_dir, '.'))
57 sprintf(wpo_dir, "%s~", po_dir);
61 fprintf(stderr, "usage: %s [outdir]\n", argv[0]);
65 if (stat(wpo_dir, &sb) < 0)
69 if (mkdir(wpo_dir, 0700) < 0)
71 fprintf(stderr, "%s: unable to make directory %s (%s))\n",
72 whoami, wpo_dir, strerror(errno));
78 fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, wpo_dir,
83 else if (!S_ISDIR(sb.st_mode))
85 fprintf(stderr, "%s: %s not a directory\n" , whoami, wpo_dir);
91 fprintf(stderr, "%s: building po databases...\n", whoami);
94 if (stat(po_dir, &sb) != -1)
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)))
102 fprintf(stderr, "%s: unable to remove %s (%s)", whoami, po_dir,
108 rename(wpo_dir, po_dir);
110 fprintf(stderr, "%s: building tar file...\n", whoami);
111 sprintf(cmd, "(cd %s; tar cf - . ) > %s.out", po_dir, po_dir);
118 void pobox(char *path)
121 char mdir[MAXPATHLEN];
122 char mout[MAXPATHLEN], qout[MAXPATHLEN];
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];
131 EXEC SQL END DECLARE SECTION;
133 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
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
139 EXEC SQL OPEN po_curses;
143 EXEC SQL FETCH po_curses INTO :machine, :mid;
146 if (!*strtrim(machine))
149 sprintf(mdir, "%s/%s", path, machine);
150 if (stat(mdir, &sb) < 0)
154 if (mkdir(mdir, 0700) < 0)
156 fprintf(stderr, "%s: unable to make directory %s (%s)\n",
157 whoami, mdir, strerror(errno));
163 fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, mdir,
168 sprintf(mout, "%s/mailboxes", mdir);
169 sprintf(qout, "%s/quota", mdir);
170 if (!(moutp = fopen(mout, "w")))
172 fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami,
173 mout, strerror(errno));
177 if (!(qoutp = fopen(qout, "w")))
179 fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami,
180 qout, strerror(errno));
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;
195 EXEC SQL FETCH filsys_cursor INTO :label, :type, :partition, :quota;
198 if (!*strtrim(label))
202 if (!*strtrim(partition))
205 if (!strncmp(&label[strlen(label)-3], ".po", 3))
206 label[strlen(label)-3] = '\0';
209 lowercase(partition);
211 fprintf(moutp, "%s.%s\t%s\t%s\t%s\n", type, label, partition,
213 fprintf(qoutp, "%s.%s\t%d\n", type, label, quota);
216 EXEC SQL CLOSE filsys_cursor;
221 fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami,
222 mout, strerror(errno));
228 fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami,
229 qout, strerror(errno));
234 EXEC SQL CLOSE po_curses;
239 db_error(sqlca.sqlcode);