3 * $Id: sw_info.c,v 1.4 1999/06/24 15:57:07 mdw Exp $
5 * Maintenance of `.sw-info' files
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of sw-tools.
14 * sw-tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * sw-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with sw-tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 1999/06/24 15:57:07 mdw
33 * Stupid bug fix. Code wouldn't even compile. Tut.
35 * Revision 1.3 1999/06/24 15:51:59 mdw
36 * Add support for the `sw-precommit' script.
38 * Revision 1.2 1999/06/18 18:58:45 mdw
41 * Revision 1.1.1.1 1999/06/02 16:53:35 mdw
46 /*----- Header files ------------------------------------------------------*/
59 #include <sys/types.h>
65 extern char **environ;
68 #include <mLib/alloc.h>
69 #include <mLib/dstr.h>
70 #include <mLib/lock.h>
71 #include <mLib/quis.h>
72 #include <mLib/report.h>
78 /*----- Static variables --------------------------------------------------*/
80 static struct swfield {
85 { "package", offsetof(swinfo, package), "Package" },
86 { "version", offsetof(swinfo, version), "Version" },
87 { "maintainer", offsetof(swinfo, maintainer), "Maintained by" },
88 { "date", offsetof(swinfo, date), "Last modified" },
89 { "only-arch", offsetof(swinfo, only_arch), "Only build for" },
90 { "arch", offsetof(swinfo, arch), "Successfully built" },
93 typedef struct swfield swfield;
95 #define SWINFO(sw, off) (*(char **)((char *)sw + off))
97 /*----- Main code ---------------------------------------------------------*/
99 /* --- @swinfo_clear@ --- *
101 * Arguments: @swinfo *sw@ = pointe to info block
105 * Use: Clears an info block so that it doesn't contain anything.
106 * This is mainly useful when building skeletons to apply using
110 void swinfo_clear(swinfo *sw)
113 for (f = swfields; f->name; f++)
114 SWINFO(sw, f->off) = 0;
117 /* --- @swinfo_fetch@ --- *
119 * Arguments: @swinfo *sw@ = pointer to info block to fill in
121 * Returns: Zero if OK, else nonzero.
123 * Use: Fills in the info block if it can.
126 int swinfo_fetch(swinfo *sw)
133 /* --- Initialize the various fields --- */
137 /* --- Open the data file --- */
139 if ((fp = fopen(".sw-info", "r")) == 0)
142 /* --- Read the lines from the file --- */
144 for (; dstr_putline(&d, fp) != EOF; DRESET(&d)) {
149 /* --- Find the field name --- */
151 while (isspace((unsigned char)*p))
153 if (*p == 0 || *p == '#')
156 /* --- Find the end of the field name --- */
159 while (*p && *p != '=' && !isspace((unsigned char)*p))
162 /* --- Skip an optional `=' sign --- */
168 while (isspace((unsigned char)*p))
170 if (*p == '=' && ch != '=') {
172 while (isspace((unsigned char)*p))
179 /* --- Look up the key in the table --- */
181 for (f = swfields; f->name; f++) {
182 if (strcmp(key, f->name) == 0)
185 moan("unknown key `%s' at line %i in `.sw-info'", key, line);
188 /* --- Put the value in --- */
191 SWINFO(sw, f->off) = xstrdup(value);
199 /* --- @swinfo_sanity@ --- *
201 * Arguments: @swinfo *sw@ = pointer to an info block
203 * Returns: Yes, if the block is OK.
205 * Use: Objects if the information in the info block is bad.
208 void swinfo_sanity(swinfo *sw)
211 die(1, "unknown package name. Try `%s setup PACKAGE VERSION'.", QUIS);
213 die(1, "unknown package version. Try `%s setup PACKAGE VERSION'.",
216 die(1, "unknown maintainer. Try `%s setup PACKAGE VERSION'.", QUIS);
218 die(1, "unknown date. Try `%s setup PACKAGE VERSION'.", QUIS);
221 /* --- @swinfo_put@ --- *
223 * Arguments: @swinfo *sw@ = pointer to an info block
225 * Returns: Zero if it worked, nonzero if not.
227 * Use: Writes an info block to the appropriate file.
230 int swinfo_put(swinfo *sw)
236 /* --- Quick sanity check --- */
240 /* --- Write the new data out --- */
242 if ((fp = fopen(".sw-info.new", "w")) == 0)
244 for (f = swfields; f->name; f++) {
245 if (!SWINFO(sw, f->off))
247 if (fprintf(fp, "%s = %s\n", f->name, SWINFO(sw, f->off)) == EOF) {
253 if (fclose(fp) == EOF) {
258 /* --- Carefully! replace the old one --- *
260 * Keep an old one around just in case, unless the renames fail because
264 remove(".sw-info.older"); /* Don't care if this fails */
265 if (rename(".sw-info.old", ".sw-info.older") && errno != ENOENT)
266 { e = errno; goto tidy_0; }
267 if (rename(".sw-info", ".sw-info.old") && errno != ENOENT)
268 { e = errno; goto tidy_1; }
269 if (rename(".sw-info.new", ".sw-info"))
270 { e = errno; goto tidy_2; }
271 remove(".sw-info.older"); /* Don't care if this fails */
274 /* --- Tidy up if anything went tits-up --- */
277 rename(".sw-info.old", ".sw-info");
279 rename(".sw-info.older", ".sw-info.old");
281 remove(".sw-info.new");
286 /* --- @swinfo_destroy@ --- *
288 * Arguments: @swinfo *sw@ = pointer to info block
292 * Use: Destroys an info block when it's not useful any more.
295 void swinfo_destroy(swinfo *sw)
299 for (f = swfields; f->name; f++) {
300 if (SWINFO(sw, f->off))
301 free(SWINFO(sw, f->off));
305 /* --- @swinfo_update@ --- *
307 * Arguments: @swinfo *sw@ = pointer to info block
308 * @swinfo *skel@ = pointer to skeleton values
312 * Use: Updates the fields in an information block, except for the
313 * architecture names stuff. (I'll leave that for later,
314 * because it's rather more involved.
317 void swinfo_update(swinfo *sw, swinfo *skel)
323 /* --- Set up a default maintainer --- */
325 if (!skel->maintainer && !sw->maintainer) {
326 char *u = getenv("USER");
329 u = getenv("LOGNAME");
331 struct passwd *pw = getpwuid(getuid());
333 moan("you don't seem to exist");
334 sprintf(ubuf, "uid %i", (int)getuid());
339 skel->maintainer = u;
342 /* --- Set up a default date --- */
346 struct tm *tm = localtime(&t);
347 strftime(dbuf, sizeof(dbuf), "%Y-%m-%d", tm);
351 /* --- Set a default architecture list --- */
353 if (!skel->arch && !sw->arch)
356 /* --- Grind through all the fields --- */
358 for (f = swfields; f->name; f++) {
359 if (!SWINFO(skel, f->off) || strcmp(SWINFO(skel, f->off), "-") == 0)
361 if (SWINFO(sw, f->off))
362 free(SWINFO(sw, f->off));
363 SWINFO(sw, f->off) = xstrdup(SWINFO(skel, f->off));
367 /*----- Subcommands -------------------------------------------------------*/
369 /* --- @sw_setup@ --- */
371 int sw_setup(int argc, char *argv[])
374 if (argc < 3 || argc > 4)
375 die(1, "Usage: setup PACKAGE VERSION [MAINTAINER]");
378 skel.package = argv[1];
379 skel.version = argv[2];
381 skel.maintainer = argv[3];
382 swinfo_update(&sw, &skel);
387 /* --- @sw_status@ --- */
389 int sw_status(int argc, char *argv[])
393 if (swinfo_fetch(&sw)) {
394 die(1, "couldn't read build status: %s (try running setup)",
397 for (f = swfields; f->name; f++) {
398 if (SWINFO(&sw, f->off))
399 printf("%s: %s\n", f->desc, SWINFO(&sw, f->off));
404 /* --- @sw_commit@ --- */
406 int sw_commit(int argc, char *argv[])
411 die(1, "Usage: commit");
412 if (swinfo_fetch(&sw)) {
413 die(1, "couldn't read build status: %s (try running setup)",
417 /* --- Make sure everything has been built properly --- */
421 archcons *all = arch_readtab();
424 for (a = aa = arch_filter(all, sw.arch, 0, 0); a; a = a->cdr)
425 a->car->flags |= archFlag_built;
429 aa = arch_filter(all, sw.only_arch, archFlag_built, 0);
431 const char *sep = "";
432 fprintf(stderr, "%s: not built for ", QUIS);
433 for (a = aa; a; a = a->cdr) {
434 fprintf(stderr, "%s%s", sep, a->car->arch);
443 /* --- Run the local precommit check script --- */
447 struct sigaction sa, sa_int, sa_quit;
450 /* --- Make sure I can trap the child's death --- */
452 sa.sa_handler = SIG_IGN;
453 sigemptyset(&sa.sa_mask);
455 if (sigaction(SIGINT, &sa, &sa_int) || sigaction(SIGQUIT, &sa, &sa_quit))
456 die(1, "couldn't set signal dispositions: %s", strerror(errno));
458 /* --- Spawn off a child process --- */
463 die(1, "error from fork: %s", strerror(errno));
467 /* --- Re-enable signals --- */
469 sigaction(SIGINT, &sa_int, 0);
470 sigaction(SIGQUIT, &sa_quit, 0);
472 /* --- Set up the environment --- */
475 env_import(&t, environ);
476 env_put(&t, "SW_PACKAGE", sw.package);
477 env_put(&t, "SW_VERSION", sw.version);
478 env_put(&t, "SW_MAINTAINER", sw.maintainer);
479 env_put(&t, "SW_DATE", sw.date);
480 env_put(&t, "SW_ARCHLIST", sw.arch);
481 env_put(&t, "SW_PREFIX", PREFIX);
482 environ = env_export(&t);
484 /* --- Run the commit check script --- */
486 execl(PREFIX "/share/sw-precommit", "sw-precommit",
487 sw.package, (void *)0);
490 die(1, "couldn't run " PREFIX "/share/sw-precommit: %s",
494 /* --- Wait for the child to finish --- */
496 if (waitpid(kid, &status, 0) < 0)
497 die(1, "error waiting for child: %s", strerror(errno));
498 if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0))
499 die(1, "sw-precommit failed");
500 sigaction(SIGINT, &sa_int, 0);
501 sigaction(SIGQUIT, &sa_quit, 0);
504 /* --- Write to the index file --- */
507 FILE *fp = fopen(PREFIX "/sw-index", "a");
509 const char *sep = "";
512 die(1, "couldn't open index file: %s", strerror(errno));
513 if (lock_file(fileno(fp), LOCK_EXCL))
514 die(1, "couldn't obtain lock on index file: %s", strerror(errno));
516 for (f = swfields; f->name; f++) {
517 if (SWINFO(&sw, f->off)) {
518 fprintf(fp, "%s%s = %s", sep, f->name, SWINFO(&sw, f->off));
529 /*----- That's all, folks -------------------------------------------------*/