]>
Commit | Line | Data |
---|---|---|
1 | /* $Id$ | |
2 | * | |
3 | * This generates a database extract from the users table for the MIT | |
4 | * Warehouse. | |
5 | * | |
6 | * Copyright (C) 1996-1998 by the Massachusetts Institute of Technology. | |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
9 | */ | |
10 | ||
11 | #include <mit-copyright.h> | |
12 | #include <moira.h> | |
13 | #include <moira_site.h> | |
14 | ||
15 | #include <sys/stat.h> | |
16 | #include <sys/types.h> | |
17 | ||
18 | #include <fcntl.h> | |
19 | #include <ctype.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | ||
25 | #include <krb.h> | |
26 | ||
27 | #include "util.h" | |
28 | ||
29 | EXEC SQL INCLUDE sqlca; | |
30 | ||
31 | RCSID("$Header$"); | |
32 | ||
33 | char *whoami = "warehouse.gen"; | |
34 | char *db = "moira/moira"; | |
35 | ||
36 | #ifndef WAREHOUSE_SUBDIR | |
37 | #define WAREHOUSE_SUBDIR "warehouse" | |
38 | #endif | |
39 | ||
40 | char warehouse_dir[MAXPATHLEN]; | |
41 | ||
42 | int main(int argc, char **argv) | |
43 | { | |
44 | int out = STDOUT_FILENO; | |
45 | char *outf = NULL, outft[MAXPATHLEN]; | |
46 | char control_fn[MAXPATHLEN], cmd[256]; | |
47 | FILE *f; | |
48 | int records = 0; | |
49 | EXEC SQL BEGIN DECLARE SECTION; | |
50 | char login[USERS_LOGIN_SIZE]; | |
51 | char id[USERS_CLEARID_SIZE], kname[MAX_K_NAME_SZ]; | |
52 | char fname[USERS_FIRST_SIZE], lname[USERS_LAST_SIZE]; | |
53 | char middle[USERS_MIDDLE_SIZE]; | |
54 | int timestamp; | |
55 | struct { | |
56 | char login[12]; | |
57 | char id[12]; | |
58 | char fname[20]; | |
59 | char lname[20]; | |
60 | char middle[20]; | |
61 | char sig[256]; | |
62 | } outrec; | |
63 | EXEC SQL END DECLARE SECTION; | |
64 | ||
65 | initialize_sms_error_table(); | |
66 | ||
67 | sprintf(warehouse_dir, "%s/%s", DCM_DIR, WAREHOUSE_SUBDIR); | |
68 | ||
69 | EXEC SQL CONNECT :db; | |
70 | ||
71 | if (argc == 2) | |
72 | { | |
73 | outf = argv[1]; | |
74 | sprintf(outft, "%s/username_id.map", warehouse_dir); | |
75 | if ((out = open(outft, O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0666)) < 0) | |
76 | { | |
77 | fprintf(stderr, "unable to open %s for output\n", outf); | |
78 | exit(MR_OCONFIG); | |
79 | } | |
80 | } | |
81 | else if (argc != 1) | |
82 | { | |
83 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
84 | exit(MR_ARGS); | |
85 | } | |
86 | else | |
87 | outf = NULL; | |
88 | ||
89 | /* The following is declarative, not executed, | |
90 | * and so is dependent on where it is in the file, | |
91 | * not in the order of execution of statements. | |
92 | */ | |
93 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
94 | ||
95 | EXEC SQL DECLARE x CURSOR FOR | |
96 | SELECT login, clearid, first, last, middle | |
97 | FROM users | |
98 | WHERE (status = 1 or status = 2 or status = 5 OR | |
99 | status = 6 or status = 7 or status = 9); | |
100 | EXEC SQL OPEN x; | |
101 | while (1) | |
102 | { | |
103 | EXEC SQL FETCH x INTO :login, :id, :fname, :lname, :middle; | |
104 | if (sqlca.sqlcode) | |
105 | break; | |
106 | if (id == 0) | |
107 | continue; | |
108 | if (!isdigit(id[1])) | |
109 | continue; | |
110 | strtrim(login); | |
111 | strtrim(id); | |
112 | strtrim(fname); | |
113 | strtrim(lname); | |
114 | strtrim(middle); | |
115 | memset(&outrec, 0, sizeof(outrec)); | |
116 | strcpy(outrec.login, login); | |
117 | strcpy(outrec.id, id); | |
118 | strcpy(outrec.fname, fname); | |
119 | strcpy(outrec.lname, lname); | |
120 | strcpy(outrec.middle, middle); | |
121 | write(out, &outrec, sizeof(outrec)); | |
122 | records++; | |
123 | } | |
124 | EXEC SQL CLOSE x; | |
125 | ||
126 | EXEC SQL COMMIT; | |
127 | ||
128 | if (close(out)) | |
129 | { | |
130 | perror("close failed"); | |
131 | exit(MR_CCONFIG); | |
132 | } | |
133 | sprintf(control_fn, "%s/username_id.ctl", warehouse_dir); | |
134 | ||
135 | f = fopen(control_fn, "w"); | |
136 | if (!f) | |
137 | { | |
138 | perror("creating username_id control file"); | |
139 | exit(MR_OCONFIG); | |
140 | } | |
141 | fprintf(f, "username_id.map --- Moira feed for MIT Data Warehouse\n"); | |
142 | fprintf(f, "%d records\n", records); | |
143 | fprintf(f, "%d bytes\n", records * (12 + 12 + 20 + 20 + 20 + 256)); | |
144 | fprintf(f, "Full feed\n"); | |
145 | fprintf(f, "Fixed format -- binary\n"); | |
146 | fclose(f); | |
147 | if (outf) | |
148 | { | |
149 | fprintf(stderr, "Building tar file.\n"); | |
150 | sprintf(cmd, "(cd %s; tar cf - . ) | compress > %s", | |
151 | warehouse_dir, outf); | |
152 | if (system(cmd)) | |
153 | exit(MR_TAR_FAIL); | |
154 | } | |
155 | ||
156 | exit(MR_SUCCESS); | |
157 | ||
158 | sqlerr: | |
159 | db_error(sqlca.sqlcode); | |
160 | exit(MR_DBMS_ERR); | |
161 | } |