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