]> andersk Git - moira.git/blob - gen/ca.dc
New database and column names for Moira2.
[moira.git] / gen / ca.dc
1 /* $Header$
2  *
3  * This generates a list of users and their home directories for the
4  * Centra America daemon.
5  *
6  *  (c) Copyright 1991 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 <stdio.h>
13 #include <moira.h>
14 #include <moira_site.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <strings.h>
19 EXEC SQL INCLUDE sqlca;
20
21 extern int errno;
22 char *whoami = "ca.gen";
23
24 struct prec {
25     int phys_id;
26     char mach[64];
27     char dir[128];
28 };
29
30
31 main(argc, argv)
32 int argc;
33 char **argv;
34 {
35     FILE *out = stdout;
36     char *outf = NULL, outft[64], *p, last[256];
37     struct stat sb;
38     struct save_queue *sq;
39     struct prec *prec;
40     int flag1, flag2, first;
41     EXEC SQL BEGIN DECLARE SECTION;
42     char login[257], name[257], dir[257];
43     int id;
44     EXEC SQL END DECLARE SECTION;
45
46     initialize_sms_error_table ();
47
48 #ifsql INGRES
49     EXEC SQL CONNECT moira;
50 #endsql
51 #ifsql INFORMIX
52     EXEC SQL DATABASE moira;
53 #endsql
54
55     if (argc == 2) {
56         if (stat(argv[1], &sb) == 0) {
57             if (ModDiff (&flag1, "users", sb.st_mtime) == 0 &&
58                 ModDiff (&flag2, "filesys", sb.st_mtime) == 0 &&
59                 flag1 < 0 && flag2 < 0) {
60                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
61                         argv[1]);
62                 exit(MR_NO_CHANGE);
63             }
64         }
65         outf = argv[1];
66         sprintf(outft, "%s~", outf);
67         if ((out = fopen(outft, "w")) == NULL) {
68             fprintf(stderr, "unable to open %s for output\n", outf);
69             exit(MR_OCONFIG);
70         }
71     } else if (argc != 1) {
72         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
73         exit(MR_ARGS);
74     } else {
75         outf = NULL;
76     }
77
78     /* The following WHENEVER is declarative, not executed,
79      * and applies for the remainder of this file only.
80      */
81     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
82     sq = sq_create();
83
84     EXEC SQL DECLARE y CURSOR FOR
85       SELECT p.nfsphys_id, p.dir, m.name
86       FROM nfsphys p, machine m
87         WHERE m.mach_id = p.mach_id;
88     EXEC SQL OPEN y;
89     while (1) {
90         EXEC SQL FETCH y INTO :id, :dir, :name;
91         if (sqlca.sqlcode != 0) break;
92         if (id == 0) continue;
93         strtrim(dir);
94         strtrim(name);
95         prec = (struct prec *) malloc(sizeof(struct prec));
96         prec->phys_id = id;
97         strcpy(prec->mach, strtrim(name));
98         strcpy(prec->dir, strtrim(dir));
99         sq_save_data(sq, prec);
100     }
101     EXEC SQL CLOSE y;
102
103     while (sq_get_data(sq, &prec)) {
104         id = prec->phys_id;
105 #ifdef DEBUG
106         printf("got ID %d\n", id);
107         printf("mach %s dir %s\n", prec->mach, prec->dir);
108 #endif
109         EXEC SQL DECLARE x CURSOR FOR 
110           SELECT u.login, u.fullname
111           FROM users u, filesys f
112             WHERE f.label = u.login and u.status = 1 and f.phys_id = :id;
113         EXEC SQL OPEN x;
114         first = 1;
115         while (1) {
116             EXEC SQL FETCH x INTO :login, :name;
117             if (sqlca.sqlcode != 0) break;
118             if (first) {
119                 first = 0;
120                 fprintf(out, "*%s:%s\n", prec->mach, prec->dir);
121             }
122             strtrim(login);
123             strtrim(name);
124             fprintf(out, "%s,%s\n", login, name);
125         }
126         EXEC SQL CLOSE x;
127     }
128
129     EXEC SQL DECLARE z CURSOR FOR 
130       SELECT DISTINCT u.login, u.fullname, f.name
131       FROM users u, filesys f
132         WHERE f.label = u.login and u.status = 1 and f.type = 'AFS'
133         ORDER BY name;
134     EXEC SQL OPEN z;
135     while (1) {
136         EXEC SQL FETCH z INTO :login, :name, :dir;
137         if (sqlca.sqlcode != 0) break;
138         p = rindex(dir, '/');
139         if (p) *p = 0;
140         if (strcmp(last, dir)) {
141             strcpy(last, dir);
142             fprintf(out, "*%s:%s\n", "AFS", dir);
143         }
144         strtrim(login);
145         strtrim(name);
146         fprintf(out, "%s,%s\n", login, name);
147     }
148     EXEC SQL CLOSE z;
149
150 #ifsql INGRES
151     EXEC SQL DISCONNECT;
152 #endsql
153 #ifsql INFORMIX
154     EXEC SQL CLOSE DATABASE;
155 #endsql
156
157     if (fclose(out)) {
158         perror("close failed");
159         exit(MR_CCONFIG);
160     }
161     if (outf)
162       fix_file(outf);
163     exit(MR_SUCCESS);
164
165  sqlerr:
166     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
167 #ifsql INGRES
168     if (sqlca.sqlcode == 17700)
169       exit(MR_DEADLOCK);
170 #endsql
171     critical_alert("DCM", "Central America build encountered INGRES ERROR %d",
172                    sqlca.sqlcode);
173     exit(MR_INGRES_ERR);
174 }
This page took 0.049102 seconds and 5 git commands to generate.