3 * $Id: sw_info.c,v 1.6 2004/04/08 01:52:19 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 /*----- Header files ------------------------------------------------------*/
42 #include <sys/types.h>
48 extern char **environ;
51 #include <mLib/alloc.h>
52 #include <mLib/dstr.h>
53 #include <mLib/lock.h>
54 #include <mLib/quis.h>
55 #include <mLib/report.h>
61 /*----- Static variables --------------------------------------------------*/
63 static struct swfield {
68 { "package", offsetof(swinfo, package), "Package" },
69 { "version", offsetof(swinfo, version), "Version" },
70 { "maintainer", offsetof(swinfo, maintainer), "Maintained by" },
71 { "date", offsetof(swinfo, date), "Last modified" },
72 { "only-arch", offsetof(swinfo, only_arch), "Only build for" },
73 { "arch", offsetof(swinfo, arch), "Successfully built" },
76 typedef struct swfield swfield;
78 #define SWINFO(sw, off) (*(char **)((char *)sw + off))
80 /*----- Main code ---------------------------------------------------------*/
82 /* --- @swinfo_clear@ --- *
84 * Arguments: @swinfo *sw@ = pointe to info block
88 * Use: Clears an info block so that it doesn't contain anything.
89 * This is mainly useful when building skeletons to apply using
93 void swinfo_clear(swinfo *sw)
96 for (f = swfields; f->name; f++)
97 SWINFO(sw, f->off) = 0;
100 /* --- @swinfo_fetch@ --- *
102 * Arguments: @swinfo *sw@ = pointer to info block to fill in
104 * Returns: Zero if OK, else nonzero.
106 * Use: Fills in the info block if it can.
109 int swinfo_fetch(swinfo *sw)
116 /* --- Initialize the various fields --- */
120 /* --- Open the data file --- */
122 if ((fp = fopen(".sw-info", "r")) == 0)
125 /* --- Read the lines from the file --- */
127 for (; dstr_putline(&d, fp) != EOF; DRESET(&d)) {
132 /* --- Find the field name --- */
134 while (isspace((unsigned char)*p))
136 if (*p == 0 || *p == '#')
139 /* --- Find the end of the field name --- */
142 while (*p && *p != '=' && !isspace((unsigned char)*p))
145 /* --- Skip an optional `=' sign --- */
151 while (isspace((unsigned char)*p))
153 if (*p == '=' && ch != '=') {
155 while (isspace((unsigned char)*p))
162 /* --- Look up the key in the table --- */
164 for (f = swfields; f->name; f++) {
165 if (strcmp(key, f->name) == 0)
168 moan("unknown key `%s' at line %i in `.sw-info'", key, line);
171 /* --- Put the value in --- */
174 SWINFO(sw, f->off) = xstrdup(value);
182 /* --- @swinfo_sanity@ --- *
184 * Arguments: @swinfo *sw@ = pointer to an info block
186 * Returns: Yes, if the block is OK.
188 * Use: Objects if the information in the info block is bad.
191 void swinfo_sanity(swinfo *sw)
194 die(1, "unknown package name. Try `%s setup PACKAGE VERSION'.", QUIS);
196 die(1, "unknown package version. Try `%s setup PACKAGE VERSION'.",
199 die(1, "unknown maintainer. Try `%s setup PACKAGE VERSION'.", QUIS);
201 die(1, "unknown date. Try `%s setup PACKAGE VERSION'.", QUIS);
204 /* --- @swinfo_put@ --- *
206 * Arguments: @swinfo *sw@ = pointer to an info block
208 * Returns: Zero if it worked, nonzero if not.
210 * Use: Writes an info block to the appropriate file.
213 int swinfo_put(swinfo *sw)
219 /* --- Quick sanity check --- */
223 /* --- Write the new data out --- */
225 if ((fp = fopen(".sw-info.new", "w")) == 0)
227 for (f = swfields; f->name; f++) {
228 if (!SWINFO(sw, f->off))
230 if (fprintf(fp, "%s = %s\n", f->name, SWINFO(sw, f->off)) == EOF) {
236 if (fclose(fp) == EOF) {
241 /* --- Carefully! replace the old one --- *
243 * Keep an old one around just in case, unless the renames fail because
247 remove(".sw-info.older"); /* Don't care if this fails */
248 if (rename(".sw-info.old", ".sw-info.older") && errno != ENOENT)
249 { e = errno; goto tidy_0; }
250 if (rename(".sw-info", ".sw-info.old") && errno != ENOENT)
251 { e = errno; goto tidy_1; }
252 if (rename(".sw-info.new", ".sw-info"))
253 { e = errno; goto tidy_2; }
254 remove(".sw-info.older"); /* Don't care if this fails */
257 /* --- Tidy up if anything went tits-up --- */
260 rename(".sw-info.old", ".sw-info");
262 rename(".sw-info.older", ".sw-info.old");
264 remove(".sw-info.new");
269 /* --- @swinfo_destroy@ --- *
271 * Arguments: @swinfo *sw@ = pointer to info block
275 * Use: Destroys an info block when it's not useful any more.
278 void swinfo_destroy(swinfo *sw)
282 for (f = swfields; f->name; f++) {
283 if (SWINFO(sw, f->off))
284 free(SWINFO(sw, f->off));
288 /* --- @swinfo_update@ --- *
290 * Arguments: @swinfo *sw@ = pointer to info block
291 * @swinfo *skel@ = pointer to skeleton values
295 * Use: Updates the fields in an information block, except for the
296 * architecture names stuff. (I'll leave that for later,
297 * because it's rather more involved.
300 void swinfo_update(swinfo *sw, swinfo *skel)
306 /* --- Set up a default maintainer --- */
308 if (!skel->maintainer && !sw->maintainer) {
309 char *u = getenv("USER");
312 u = getenv("LOGNAME");
314 struct passwd *pw = getpwuid(getuid());
316 moan("you don't seem to exist");
317 sprintf(ubuf, "uid %i", (int)getuid());
322 skel->maintainer = u;
325 /* --- Set up a default date --- */
329 struct tm *tm = localtime(&t);
330 strftime(dbuf, sizeof(dbuf), "%Y-%m-%d", tm);
334 /* --- Set a default architecture list --- */
336 if (!skel->arch && !sw->arch)
339 /* --- Grind through all the fields --- */
341 for (f = swfields; f->name; f++) {
342 if (!SWINFO(skel, f->off) || strcmp(SWINFO(skel, f->off), "-") == 0)
344 if (SWINFO(sw, f->off))
345 free(SWINFO(sw, f->off));
346 SWINFO(sw, f->off) = xstrdup(SWINFO(skel, f->off));
350 /*----- Subcommands -------------------------------------------------------*/
352 /* --- @sw_setup@ --- */
354 int sw_setup(int argc, char *argv[])
357 if (argc < 3 || argc > 4)
358 die(1, "Usage: setup PACKAGE VERSION [MAINTAINER]");
361 skel.package = argv[1];
362 skel.version = argv[2];
364 skel.maintainer = argv[3];
365 swinfo_update(&sw, &skel);
370 /* --- @sw_status@ --- */
372 int sw_status(int argc, char *argv[])
376 if (swinfo_fetch(&sw)) {
377 die(1, "couldn't read build status: %s (try running setup)",
380 for (f = swfields; f->name; f++) {
381 if (SWINFO(&sw, f->off))
382 printf("%s: %s\n", f->desc, SWINFO(&sw, f->off));
387 /* --- @sw_commit@ --- */
389 int sw_commit(int argc, char *argv[])
394 die(1, "Usage: commit");
395 if (swinfo_fetch(&sw)) {
396 die(1, "couldn't read build status: %s (try running setup)",
400 /* --- Make sure everything has been built properly --- */
404 archcons *all = arch_readtab();
407 for (a = aa = arch_filter(all, sw.arch, 0, 0); a; a = a->cdr)
408 a->car->flags |= archFlag_built;
412 aa = arch_filter(all, sw.only_arch, archFlag_built, 0);
414 const char *sep = "";
415 fprintf(stderr, "%s: not built for ", QUIS);
416 for (a = aa; a; a = a->cdr) {
417 fprintf(stderr, "%s%s", sep, a->car->arch);
426 /* --- Run the local precommit check script --- */
430 struct sigaction sa, sa_int, sa_quit;
433 /* --- Make sure I can trap the child's death --- */
435 sa.sa_handler = SIG_IGN;
436 sigemptyset(&sa.sa_mask);
438 if (sigaction(SIGINT, &sa, &sa_int) || sigaction(SIGQUIT, &sa, &sa_quit))
439 die(1, "couldn't set signal dispositions: %s", strerror(errno));
441 /* --- Spawn off a child process --- */
446 die(1, "error from fork: %s", strerror(errno));
450 /* --- Re-enable signals --- */
452 sigaction(SIGINT, &sa_int, 0);
453 sigaction(SIGQUIT, &sa_quit, 0);
455 /* --- Set up the environment --- */
458 env_import(&t, environ);
459 env_put(&t, "SW_PACKAGE", sw.package);
460 env_put(&t, "SW_VERSION", sw.version);
461 env_put(&t, "SW_MAINTAINER", sw.maintainer);
462 env_put(&t, "SW_DATE", sw.date);
463 env_put(&t, "SW_ARCHLIST", sw.arch);
464 env_put(&t, "SW_PREFIX", PREFIX);
465 environ = env_export(&t);
467 /* --- Run the commit check script --- */
469 execl(PREFIX "/share/sw-precommit", "sw-precommit",
470 sw.package, (char *)0);
473 die(1, "couldn't run " PREFIX "/share/sw-precommit: %s",
477 /* --- Wait for the child to finish --- */
479 if (waitpid(kid, &status, 0) < 0)
480 die(1, "error waiting for child: %s", strerror(errno));
481 if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0))
482 die(1, "sw-precommit failed");
483 sigaction(SIGINT, &sa_int, 0);
484 sigaction(SIGQUIT, &sa_quit, 0);
487 /* --- Write to the index file --- */
490 FILE *fp = fopen(PREFIX "/sw-index", "a");
492 const char *sep = "";
495 die(1, "couldn't open index file: %s", strerror(errno));
496 if (lock_file(fileno(fp), LOCK_EXCL))
497 die(1, "couldn't obtain lock on index file: %s", strerror(errno));
499 for (f = swfields; f->name; f++) {
500 if (SWINFO(&sw, f->off)) {
501 fprintf(fp, "%s%s = %s", sep, f->name, SWINFO(&sw, f->off));
512 /*----- That's all, folks -------------------------------------------------*/