]> andersk Git - moira.git/blame - gen/warehouse.pc
Add support for get_host_by_account_number query.
[moira.git] / gen / warehouse.pc
CommitLineData
7ac48069 1/* $Id$
21c13470 2 *
3 * This generates a database extract from the users table for the MIT
4 * Warehouse.
5 *
7ac48069 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>.
21c13470 9 */
10
11#include <mit-copyright.h>
21c13470 12#include <moira.h>
13#include <moira_site.h>
7ac48069 14
21c13470 15#include <sys/stat.h>
7ac48069 16#include <sys/types.h>
17
21c13470 18#include <fcntl.h>
7ac48069 19#include <ctype.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
5eaef520 23#include <unistd.h>
7ac48069 24
25#include <krb.h>
7ac48069 26
27#include "util.h"
28
21c13470 29EXEC SQL INCLUDE sqlca;
30
7ac48069 31RCSID("$Header$");
32
d4bdcedb 33char *whoami = "warehouse.gen";
21c13470 34char *db = "moira/moira";
35
97cd0053 36#ifndef WAREHOUSE_SUBDIR
37#define WAREHOUSE_SUBDIR "warehouse"
38#endif
39
dfaf9b68 40char warehouse_dir[MAXPATHLEN];
97cd0053 41
5eaef520 42int main(int argc, char **argv)
21c13470 43{
5eaef520 44 int out = STDOUT_FILENO;
dfaf9b68 45 char *outf = NULL, outft[MAXPATHLEN];
46 char control_fn[MAXPATHLEN], cmd[256];
5eaef520 47 FILE *f;
5eaef520 48 int records = 0;
49 EXEC SQL BEGIN DECLARE SECTION;
d55429b2 50 char login[USERS_LOGIN_SIZE];
dfaf9b68 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];
d55429b2 54 int timestamp;
5eaef520 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 {
5eaef520 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);
21c13470 79 }
21c13470 80 }
5eaef520 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;
21c13470 94
5eaef520 95 EXEC SQL DECLARE x CURSOR FOR
d55429b2 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);
5eaef520 100 EXEC SQL OPEN x;
101 while (1)
102 {
d55429b2 103 EXEC SQL FETCH x INTO :login, :id, :fname, :lname, :middle;
5eaef520 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);
5eaef520 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);
5eaef520 121 write(out, &outrec, sizeof(outrec));
122 records++;
21c13470 123 }
5eaef520 124 EXEC SQL CLOSE x;
21c13470 125
5eaef520 126 EXEC SQL COMMIT;
21c13470 127
5eaef520 128 if (close(out))
129 {
130 perror("close failed");
131 exit(MR_CCONFIG);
21c13470 132 }
5eaef520 133 sprintf(control_fn, "%s/username_id.ctl", warehouse_dir);
97cd0053 134
5eaef520 135 f = fopen(control_fn, "w");
136 if (!f)
137 {
138 perror("creating username_id control file");
139 exit(MR_OCONFIG);
97cd0053 140 }
5eaef520 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);
97cd0053 154 }
21c13470 155
5eaef520 156 exit(MR_SUCCESS);
157
158sqlerr:
159 db_error(sqlca.sqlcode);
160 exit(MR_DBMS_ERR);
21c13470 161}
This page took 0.089952 seconds and 5 git commands to generate.