]> andersk Git - moira.git/blob - gen/warehouse.pc
Code style cleanup. (No functional changes)
[moira.git] / gen / warehouse.pc
1 /* $Header$
2  *
3  * This generates a database extract from the users table for the MIT
4  * Warehouse.
5  *
6  *  (c) Copyright 1992 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 <errno.h>
13 #include <stdio.h>
14 #include <moira.h>
15 #include <moira_site.h>
16 #include <des.h>
17 #include <krb.h>
18 #include <gdss.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <sys/file.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 EXEC SQL INCLUDE sqlca;
28
29 char *whoami = "warehouse.gen";
30 char *db = "moira/moira";
31
32 #ifndef WAREHOUSE_SUBDIR
33 #define WAREHOUSE_SUBDIR "warehouse"
34 #endif
35
36 char warehouse_dir[128];
37
38 int main(int argc, char **argv)
39 {
40   int out = STDOUT_FILENO;
41   char *outf = NULL, outft[64];
42   char control_fn[80], cmd[256];
43   FILE *f;
44   struct stat sb;
45   int flag1;
46   SigInfo si;
47   struct timeval now;
48   int records = 0;
49   EXEC SQL BEGIN DECLARE SECTION;
50   char login[9], sig[257], id[17], kname[257];
51   char fname[17], lname[17], middle[17];
52   EXEC SQL VAR sig IS STRING(257);
53   int timestamp, sigwho;
54   struct {
55     char login[12];
56     char id[12];
57     char fname[20];
58     char lname[20];
59     char middle[20];
60     char sig[256];
61   } outrec;
62   EXEC SQL END DECLARE SECTION;
63
64   initialize_sms_error_table();
65
66   sprintf(warehouse_dir, "%s/%s", DCM_DIR, WAREHOUSE_SUBDIR);
67
68   EXEC SQL CONNECT :db;
69
70   if (argc == 2)
71     {
72       if (stat(argv[1], &sb) == 0)
73         {
74           if (ModDiff(&flag1, "users", sb.st_mtime) == 0 && flag1 < 0)
75             {
76               fprintf(stderr, "File %s does not need to be rebuilt.\n",
77                       argv[1]);
78               exit(MR_NO_CHANGE);
79             }
80         }
81       outf = argv[1];
82       sprintf(outft, "%s/username_id.map", warehouse_dir);
83       if ((out = open(outft, O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0666)) < 0)
84         {
85           fprintf(stderr, "unable to open %s for output\n", outf);
86           exit(MR_OCONFIG);
87         }
88     }
89   else if (argc != 1)
90     {
91       fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
92       exit(MR_ARGS);
93     }
94   else
95     outf = NULL;
96
97   /* The following is declarative, not executed,
98    * and so is dependent on where it is in the file,
99    * not in the order of execution of statements.
100    */
101   EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
102
103   EXEC SQL DECLARE x CURSOR FOR
104     SELECT u.login, u.clearid, u.signature, u.sigdate, s.string, u.sigwho,
105     u.first, u.last, u.middle
106     FROM users u, strings s
107     WHERE (u.status = 1 or u.status = 2 or u.status = 5 OR
108            u.status = 6 or u.status = 7)
109     AND u.sigwho = s.string_id;
110   EXEC SQL OPEN x;
111   while (1)
112     {
113       EXEC SQL FETCH x INTO :login, :id, :sig, :timestamp, :kname, :sigwho,
114         :fname, :lname, :middle;
115       if (sqlca.sqlcode)
116         break;
117       if (id == 0)
118         continue;
119       if (!isdigit(id[1]))
120         continue;
121       strtrim(login);
122       strtrim(id);
123       strtrim(kname);
124       strtrim(fname);
125       strtrim(lname);
126       strtrim(middle);
127       memset(&outrec, 0, sizeof(outrec));
128       strcpy(outrec.login, login);
129       strcpy(outrec.id, id);
130       strcpy(outrec.fname, fname);
131       strcpy(outrec.lname, lname);
132       strcpy(outrec.middle, middle);
133       if (sigwho)
134         {
135           si.timestamp = timestamp;
136           si.SigInfoVersion = 0; /* XXXXXX this isn't used */
137           kname_parse(si.pname, si.pinst, si.prealm, kname);
138           si.rawsig = (unsigned char *)sig;
139           GDSS_Recompose(&si, outrec.sig);
140         }
141       write(out, &outrec, sizeof(outrec));
142       records++;
143     }
144   EXEC SQL CLOSE x;
145
146   EXEC SQL COMMIT;
147
148   if (close(out))
149     {
150       perror("close failed");
151       exit(MR_CCONFIG);
152     }
153   sprintf(control_fn, "%s/username_id.ctl", warehouse_dir);
154
155   f = fopen(control_fn, "w");
156   if (!f)
157     {
158       perror("creating username_id control file");
159       exit(MR_OCONFIG);
160     }
161   fprintf(f, "username_id.map --- Moira feed for MIT Data Warehouse\n");
162   fprintf(f, "%d records\n", records);
163   fprintf(f, "%d bytes\n", records * (12 + 12 + 20 + 20 + 20 + 256));
164   fprintf(f, "Full feed\n");
165   fprintf(f, "Fixed format -- binary\n");
166   fclose(f);
167   if (outf)
168     {
169       fprintf(stderr, "Building tar file.\n");
170       sprintf(cmd, "(cd %s; tar cf - . ) | compress > %s",
171               warehouse_dir, outf);
172       if (system(cmd))
173         exit(MR_TAR_FAIL);
174     }
175
176   exit(MR_SUCCESS);
177
178 sqlerr:
179   db_error(sqlca.sqlcode);
180   exit(MR_DBMS_ERR);
181 }
This page took 0.050775 seconds and 5 git commands to generate.