mkmodules.c revision 102843
1/*
2 * Copyright (c) 1992, Brian Berliner and Jeff Polk
3 * Copyright (c) 1989-1992, Brian Berliner
4 *
5 * You may distribute under the terms of the GNU General Public License as
6 * specified in the README file that comes with the CVS kit.
7 *
8 * $FreeBSD: head/contrib/cvs/src/mkmodules.c 102843 2002-09-02 05:57:14Z peter $
9 */
10
11#include "cvs.h"
12#include "savecwd.h"
13#include "getline.h"
14
15#ifndef DBLKSIZ
16#define	DBLKSIZ	4096			/* since GNU ndbm doesn't define it */
17#endif
18
19static int checkout_file PROTO((char *file, char *temp));
20static char *make_tempfile PROTO((void));
21static void rename_rcsfile PROTO((char *temp, char *real));
22
23#ifndef MY_NDBM
24static void rename_dbmfile PROTO((char *temp));
25static void write_dbmfile PROTO((char *temp));
26#endif				/* !MY_NDBM */
27
28/* Structure which describes an administrative file.  */
29struct admin_file {
30   /* Name of the file, within the CVSROOT directory.  */
31   char *filename;
32
33   /* This is a one line description of what the file is for.  It is not
34      currently used, although one wonders whether it should be, somehow.
35      If NULL, then don't process this file in mkmodules (FIXME?: a bit of
36      a kludge; probably should replace this with a flags field).  */
37   char *errormsg;
38
39   /* Contents which the file should have in a new repository.  To avoid
40      problems with brain-dead compilers which choke on long string constants,
41      this is a pointer to an array of char * terminated by NULL--each of
42      the strings is concatenated.
43
44      If this field is NULL, the file is not created in a new
45      repository, but it can be added with "cvs add" (just as if one
46      had created the repository with a version of CVS which didn't
47      know about the file) and the checked-out copy will be updated
48      without having to add it to checkoutlist.  */
49   const char * const *contents;
50};
51
52static const char *const loginfo_contents[] = {
53    "# The \"loginfo\" file controls where \"cvs commit\" log information\n",
54    "# is sent.  The first entry on a line is a regular expression which must match\n",
55    "# the directory that the change is being made to, relative to the\n",
56    "# $CVSROOT.  If a match is found, then the remainder of the line is a filter\n",
57    "# program that should expect log information on its standard input.\n",
58    "#\n",
59    "# If the repository name does not match any of the regular expressions in this\n",
60    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
61    "#\n",
62    "# If the name ALL appears as a regular expression it is always used\n",
63    "# in addition to the first matching regex or DEFAULT.\n",
64    "#\n",
65    "# You may specify a format string as part of the\n",
66    "# filter.  The string is composed of a `%' followed\n",
67    "# by a single format character, or followed by a set of format\n",
68    "# characters surrounded by `{' and `}' as separators.  The format\n",
69    "# characters are:\n",
70    "#\n",
71    "#   s = file name\n",
72    "#   V = old version number (pre-checkin)\n",
73    "#   v = new version number (post-checkin)\n",
74    "#\n",
75    "# For example:\n",
76    "#DEFAULT (echo \"\"; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
77    "# or\n",
78    "#DEFAULT (echo \"\"; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
79    NULL
80};
81
82static const char *const rcsinfo_contents[] = {
83    "# The \"rcsinfo\" file is used to control templates with which the editor\n",
84    "# is invoked on commit and import.\n",
85    "#\n",
86    "# The first entry on a line is a regular expression which is tested\n",
87    "# against the directory that the change is being made to, relative to the\n",
88    "# $CVSROOT.  For the first match that is found, then the remainder of the\n",
89    "# line is the name of the file that contains the template.\n",
90    "#\n",
91    "# If the repository name does not match any of the regular expressions in this\n",
92    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
93    "#\n",
94    "# If the name \"ALL\" appears as a regular expression it is always used\n",
95    "# in addition to the first matching regex or \"DEFAULT\".\n",
96    NULL
97};
98
99static const char *const editinfo_contents[] = {
100    "# The \"editinfo\" file is used to allow verification of logging\n",
101    "# information.  It works best when a template (as specified in the\n",
102    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
103    "# template with locations for, a bug-id number, a list of people who\n",
104    "# reviewed the code before it can be checked in, and an external\n",
105    "# process to catalog the differences that were code reviewed, the\n",
106    "# following test can be applied to the code:\n",
107    "#\n",
108    "#   Making sure that the entered bug-id number is correct.\n",
109    "#   Validating that the code that was reviewed is indeed the code being\n",
110    "#       checked in (using the bug-id number or a seperate review\n",
111    "#       number to identify this particular code set.).\n",
112    "#\n",
113    "# If any of the above test failed, then the commit would be aborted.\n",
114    "#\n",
115    "# Actions such as mailing a copy of the report to each reviewer are\n",
116    "# better handled by an entry in the loginfo file.\n",
117    "#\n",
118    "# One thing that should be noted is the the ALL keyword is not\n",
119    "# supported.  There can be only one entry that matches a given\n",
120    "# repository.\n",
121    NULL
122};
123
124static const char *const verifymsg_contents[] = {
125    "# The \"verifymsg\" file is used to allow verification of logging\n",
126    "# information.  It works best when a template (as specified in the\n",
127    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
128    "# template with locations for, a bug-id number, a list of people who\n",
129    "# reviewed the code before it can be checked in, and an external\n",
130    "# process to catalog the differences that were code reviewed, the\n",
131    "# following test can be applied to the code:\n",
132    "#\n",
133    "#   Making sure that the entered bug-id number is correct.\n",
134    "#   Validating that the code that was reviewed is indeed the code being\n",
135    "#       checked in (using the bug-id number or a seperate review\n",
136    "#       number to identify this particular code set.).\n",
137    "#\n",
138    "# If any of the above test failed, then the commit would be aborted.\n",
139    "#\n",
140    "# Actions such as mailing a copy of the report to each reviewer are\n",
141    "# better handled by an entry in the loginfo file.\n",
142    "#\n",
143    "# One thing that should be noted is the the ALL keyword is not\n",
144    "# supported.  There can be only one entry that matches a given\n",
145    "# repository.\n",
146    NULL
147};
148
149static const char *const commitinfo_contents[] = {
150    "# The \"commitinfo\" file is used to control pre-commit checks.\n",
151    "# The filter on the right is invoked with the repository and a list \n",
152    "# of files to check.  A non-zero exit of the filter program will \n",
153    "# cause the commit to be aborted.\n",
154    "#\n",
155    "# The first entry on a line is a regular expression which is tested\n",
156    "# against the directory that the change is being committed to, relative\n",
157    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
158    "# of the line is the name of the filter to run.\n",
159    "#\n",
160    "# If the repository name does not match any of the regular expressions in this\n",
161    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
162    "#\n",
163    "# If the name \"ALL\" appears as a regular expression it is always used\n",
164    "# in addition to the first matching regex or \"DEFAULT\".\n",
165    NULL
166};
167
168static const char *const taginfo_contents[] = {
169    "# The \"taginfo\" file is used to control pre-tag checks.\n",
170    "# The filter on the right is invoked with the following arguments:\n",
171    "#\n",
172    "# $1 -- tagname\n",
173    "# $2 -- operation \"add\" for tag, \"mov\" for tag -F, and \"del\" for tag -d\n",
174    "# $3 -- repository\n",
175    "# $4->  file revision [file revision ...]\n",
176    "#\n",
177    "# A non-zero exit of the filter program will cause the tag to be aborted.\n",
178    "#\n",
179    "# The first entry on a line is a regular expression which is tested\n",
180    "# against the directory that the change is being committed to, relative\n",
181    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
182    "# of the line is the name of the filter to run.\n",
183    "#\n",
184    "# If the repository name does not match any of the regular expressions in this\n",
185    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
186    "#\n",
187    "# If the name \"ALL\" appears as a regular expression it is always used\n",
188    "# in addition to the first matching regex or \"DEFAULT\".\n",
189    NULL
190};
191
192static const char *const checkoutlist_contents[] = {
193    "# The \"checkoutlist\" file is used to support additional version controlled\n",
194    "# administrative files in $CVSROOT/CVSROOT, such as template files.\n",
195    "#\n",
196    "# The first entry on a line is a filename which will be checked out from\n",
197    "# the corresponding RCS file in the $CVSROOT/CVSROOT directory.\n",
198    "# The remainder of the line is an error message to use if the file cannot\n",
199    "# be checked out.\n",
200    "#\n",
201    "# File format:\n",
202    "#\n",
203    "#	[<whitespace>]<filename><whitespace><error message><end-of-line>\n",
204    "#\n",
205    "# comment lines begin with '#'\n",
206    NULL
207};
208
209static const char *const cvswrappers_contents[] = {
210    "# This file affects handling of files based on their names.\n",
211    "#\n",
212#if 0    /* see comments in wrap_add in wrapper.c */
213    "# The -t/-f options allow one to treat directories of files\n",
214    "# as a single file, or to transform a file in other ways on\n",
215    "# its way in and out of CVS.\n",
216    "#\n",
217#endif
218    "# The -m option specifies whether CVS attempts to merge files.\n",
219    "#\n",
220    "# The -k option specifies keyword expansion (e.g. -kb for binary).\n",
221    "#\n",
222    "# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)\n",
223    "#\n",
224    "#  wildcard	[option value][option value]...\n",
225    "#\n",
226    "#  where option is one of\n",
227    "#  -f		from cvs filter		value: path to filter\n",
228    "#  -t		to cvs filter		value: path to filter\n",
229    "#  -m		update methodology	value: MERGE or COPY\n",
230    "#  -k		expansion mode		value: b, o, kkv, &c\n",
231    "#\n",
232    "#  and value is a single-quote delimited value.\n",
233    "# For example:\n",
234    "#*.gif -k 'b'\n",
235    NULL
236};
237
238static const char *const notify_contents[] = {
239    "# The \"notify\" file controls where notifications from watches set by\n",
240    "# \"cvs watch add\" or \"cvs edit\" are sent.  The first entry on a line is\n",
241    "# a regular expression which is tested against the directory that the\n",
242    "# change is being made to, relative to the $CVSROOT.  If it matches,\n",
243    "# then the remainder of the line is a filter program that should contain\n",
244    "# one occurrence of %s for the user to notify, and information on its\n",
245    "# standard input.\n",
246    "#\n",
247    "# \"ALL\" or \"DEFAULT\" can be used in place of the regular expression.\n",
248    "#\n",
249    "# For example:\n",
250    "#ALL mail -s \"CVS notification\" %s\n",
251    NULL
252};
253
254static const char *const modules_contents[] = {
255    "# Three different line formats are valid:\n",
256    "#	key	-a    aliases...\n",
257    "#	key [options] directory\n",
258    "#	key [options] directory files...\n",
259    "#\n",
260    "# Where \"options\" are composed of:\n",
261    "#	-i prog		Run \"prog\" on \"cvs commit\" from top-level of module.\n",
262    "#	-o prog		Run \"prog\" on \"cvs checkout\" of module.\n",
263    "#	-e prog		Run \"prog\" on \"cvs export\" of module.\n",
264    "#	-t prog		Run \"prog\" on \"cvs rtag\" of module.\n",
265    "#	-u prog		Run \"prog\" on \"cvs update\" of module.\n",
266    "#	-d dir		Place module in directory \"dir\" instead of module name.\n",
267    "#	-l		Top-level directory only -- do not recurse.\n",
268    "#\n",
269    "# NOTE:  If you change any of the \"Run\" options above, you'll have to\n",
270    "# release and re-checkout any working directories of these modules.\n",
271    "#\n",
272    "# And \"directory\" is a path to a directory relative to $CVSROOT.\n",
273    "#\n",
274    "# The \"-a\" option specifies an alias.  An alias is interpreted as if\n",
275    "# everything on the right of the \"-a\" had been typed on the command line.\n",
276    "#\n",
277    "# You can encode a module within a module by using the special '&'\n",
278    "# character to interpose another module into the current module.  This\n",
279    "# can be useful for creating a module that consists of many directories\n",
280    "# spread out over the entire source repository.\n",
281    NULL
282};
283
284static const char *const config_contents[] = {
285    "# Set this to \"no\" if pserver shouldn't check system users/passwords\n",
286    "#SystemAuth=no\n",
287    "\n",
288    "# Put CVS lock files in this directory rather than directly in the repository.\n",
289    "#LockDir=/var/lock/cvs\n",
290    "\n",
291#ifdef PRESERVE_PERMISSIONS_SUPPORT
292    "# Set `PreservePermissions' to `yes' to save file status information\n",
293    "# in the repository.\n",
294    "#PreservePermissions=no\n",
295    "\n",
296#endif
297    "# Set `TopLevelAdmin' to `yes' to create a CVS directory at the top\n",
298    "# level of the new working directory when using the `cvs checkout'\n",
299    "# command.\n",
300    "#TopLevelAdmin=no\n",
301    "\n",
302    "# Set `LogHistory' to `all' or `TOFEWGCMAR' to log all transactions to the\n",
303    "# history file, or a subset as needed (ie `TMAR' logs all write operations)\n",
304    "#LogHistory=TOFEWGCMAR\n",
305    "\n",
306    "# Set `RereadLogAfterVerify' to `always' (the default) to allow the verifymsg\n",
307    "# script to change the log message.  Set it to `stat' to force CVS to verify",
308    "# that the file has changed before reading it (this can take up to an extra\n",
309    "# second per directory being committed, so it is not recommended for large\n",
310    "# repositories.  Set it to `never' (the previous CVS behavior) to prevent\n",
311    "# verifymsg scripts from changing the log message.\n",
312    "#RereadLogAfterVerify=always\n",
313    NULL
314};
315
316static const struct admin_file filelist[] = {
317    {CVSROOTADM_LOGINFO,
318	"no logging of 'cvs commit' messages is done without a %s file",
319	&loginfo_contents[0]},
320    {CVSROOTADM_RCSINFO,
321	"a %s file can be used to configure 'cvs commit' templates",
322	rcsinfo_contents},
323    {CVSROOTADM_EDITINFO,
324	"a %s file can be used to validate log messages",
325	editinfo_contents},
326    {CVSROOTADM_VERIFYMSG,
327	"a %s file can be used to validate log messages",
328	verifymsg_contents},
329    {CVSROOTADM_COMMITINFO,
330	"a %s file can be used to configure 'cvs commit' checking",
331	commitinfo_contents},
332    {CVSROOTADM_TAGINFO,
333	"a %s file can be used to configure 'cvs tag' checking",
334	taginfo_contents},
335    {CVSROOTADM_IGNORE,
336	"a %s file can be used to specify files to ignore",
337	NULL},
338    {CVSROOTADM_CHECKOUTLIST,
339	"a %s file can specify extra CVSROOT files to auto-checkout",
340	checkoutlist_contents},
341    {CVSROOTADM_WRAPPER,
342	"a %s file can be used to specify files to treat as wrappers",
343	cvswrappers_contents},
344    {CVSROOTADM_NOTIFY,
345	"a %s file can be used to specify where notifications go",
346	notify_contents},
347    {CVSROOTADM_MODULES,
348	/* modules is special-cased in mkmodules.  */
349	NULL,
350	modules_contents},
351    {CVSROOTADM_READERS,
352	"a %s file specifies read-only users",
353	NULL},
354    {CVSROOTADM_WRITERS,
355	"a %s file specifies read/write users",
356	NULL},
357
358    /* Some have suggested listing CVSROOTADM_PASSWD here too.  This
359       would mean that CVS commands which operate on the
360       CVSROOTADM_PASSWD file would transmit hashed passwords over the
361       net.  This might seem to be no big deal, as pserver normally
362       transmits cleartext passwords, but the difference is that
363       CVSROOTADM_PASSWD contains *all* passwords, not just the ones
364       currently being used.  For example, it could be too easy to
365       accidentally give someone readonly access to CVSROOTADM_PASSWD
366       (e.g. via anonymous CVS or cvsweb), and then if there are any
367       guessable passwords for read/write access (usually there will be)
368       they get read/write access.
369
370       Another worry is the implications of storing old passwords--if
371       someone used a password in the past they might be using it
372       elsewhere, using a similar password, etc, and so saving old
373       passwords, even hashed, is probably not a good idea.  */
374
375    {CVSROOTADM_CONFIG,
376	 "a %s file configures various behaviors",
377	 config_contents},
378    {NULL, NULL, NULL}
379};
380
381/* Rebuild the checked out administrative files in directory DIR.  */
382int
383mkmodules (dir)
384    char *dir;
385{
386    struct saved_cwd cwd;
387    char *temp;
388    char *cp, *last, *fname;
389#ifdef MY_NDBM
390    DBM *db;
391#endif
392    FILE *fp;
393    char *line = NULL;
394    size_t line_allocated = 0;
395    const struct admin_file *fileptr;
396
397    if (noexec)
398	return 0;
399
400    if (save_cwd (&cwd))
401	error_exit ();
402
403    if ( CVS_CHDIR (dir) < 0)
404	error (1, errno, "cannot chdir to %s", dir);
405
406    /*
407     * First, do the work necessary to update the "modules" database.
408     */
409    temp = make_tempfile ();
410    switch (checkout_file (CVSROOTADM_MODULES, temp))
411    {
412
413	case 0:			/* everything ok */
414#ifdef MY_NDBM
415	    /* open it, to generate any duplicate errors */
416	    if ((db = dbm_open (temp, O_RDONLY, 0666)) != NULL)
417		dbm_close (db);
418#else
419	    write_dbmfile (temp);
420	    rename_dbmfile (temp);
421#endif
422	    rename_rcsfile (temp, CVSROOTADM_MODULES);
423	    break;
424
425	default:
426	    error (0, 0,
427		"'cvs checkout' is less functional without a %s file",
428		CVSROOTADM_MODULES);
429	    break;
430    }					/* switch on checkout_file() */
431
432    if (unlink_file (temp) < 0
433	&& !existence_error (errno))
434	error (0, errno, "cannot remove %s", temp);
435    free (temp);
436
437    /* Checkout the files that need it in CVSROOT dir */
438    for (fileptr = filelist; fileptr && fileptr->filename; fileptr++) {
439	if (fileptr->errormsg == NULL)
440	    continue;
441	temp = make_tempfile ();
442	if (checkout_file (fileptr->filename, temp) == 0)
443	    rename_rcsfile (temp, fileptr->filename);
444#if 0
445	/*
446	 * If there was some problem other than the file not existing,
447	 * checkout_file already printed a real error message.  If the
448	 * file does not exist, it is harmless--it probably just means
449	 * that the repository was created with an old version of CVS
450	 * which didn't have so many files in CVSROOT.
451	 */
452	else if (fileptr->errormsg)
453	    error (0, 0, fileptr->errormsg, fileptr->filename);
454#endif
455	if (unlink_file (temp) < 0
456	    && !existence_error (errno))
457	    error (0, errno, "cannot remove %s", temp);
458	free (temp);
459    }
460
461    fp = CVS_FOPEN (CVSROOTADM_CHECKOUTLIST, "r");
462    if (fp)
463    {
464	/*
465	 * File format:
466	 *  [<whitespace>]<filename><whitespace><error message><end-of-line>
467	 *
468	 * comment lines begin with '#'
469	 */
470	while (getline (&line, &line_allocated, fp) >= 0)
471	{
472	    /* skip lines starting with # */
473	    if (line[0] == '#')
474		continue;
475
476	    if ((last = strrchr (line, '\n')) != NULL)
477		*last = '\0';			/* strip the newline */
478
479	    /* Skip leading white space. */
480	    for (fname = line;
481		 *fname && isspace ((unsigned char) *fname);
482		 fname++)
483		;
484
485	    /* Find end of filename. */
486	    for (cp = fname; *cp && !isspace ((unsigned char) *cp); cp++)
487		;
488	    *cp = '\0';
489
490	    temp = make_tempfile ();
491	    if (checkout_file (fname, temp) == 0)
492	    {
493		rename_rcsfile (temp, fname);
494	    }
495	    else
496	    {
497		for (cp++;
498		     cp < last && *last && isspace ((unsigned char) *last);
499		     cp++)
500		    ;
501		if (cp < last && *cp)
502		    error (0, 0, cp, fname);
503	    }
504	    if (unlink_file (temp) < 0
505		&& !existence_error (errno))
506		error (0, errno, "cannot remove %s", temp);
507	    free (temp);
508	}
509	if (line)
510	    free (line);
511	if (ferror (fp))
512	    error (0, errno, "cannot read %s", CVSROOTADM_CHECKOUTLIST);
513	if (fclose (fp) < 0)
514	    error (0, errno, "cannot close %s", CVSROOTADM_CHECKOUTLIST);
515    }
516    else
517    {
518	/* Error from CVS_FOPEN.  */
519	if (!existence_error (errno))
520	    error (0, errno, "cannot open %s", CVSROOTADM_CHECKOUTLIST);
521    }
522
523    if (restore_cwd (&cwd, NULL))
524	error_exit ();
525    free_cwd (&cwd);
526
527    return (0);
528}
529
530/*
531 * Yeah, I know, there are NFS race conditions here.
532 */
533static char *
534make_tempfile ()
535{
536    static int seed = 0;
537    int fd;
538    char *temp;
539
540    if (seed == 0)
541	seed = getpid ();
542    temp = xmalloc (sizeof (BAKPREFIX) + 40);
543    while (1)
544    {
545	(void) sprintf (temp, "%s%d", BAKPREFIX, seed++);
546	if ((fd = CVS_OPEN (temp, O_CREAT|O_EXCL|O_RDWR, 0666)) != -1)
547	    break;
548	if (errno != EEXIST)
549	    error (1, errno, "cannot create temporary file %s", temp);
550    }
551    if (close(fd) < 0)
552	error(1, errno, "cannot close temporary file %s", temp);
553    return temp;
554}
555
556/* Get a file.  If the file does not exist, return 1 silently.  If
557   there is an error, print a message and return 1 (FIXME: probably
558   not a very clean convention).  On success, return 0.  */
559
560static int
561checkout_file (file, temp)
562    char *file;
563    char *temp;
564{
565    char *rcs;
566    RCSNode *rcsnode;
567    int retcode = 0;
568
569    if (noexec)
570	return 0;
571
572    rcs = xmalloc (strlen (file) + 5);
573    strcpy (rcs, file);
574    strcat (rcs, RCSEXT);
575    if (!isfile (rcs))
576    {
577	free (rcs);
578	return (1);
579    }
580    rcsnode = RCS_parsercsfile (rcs);
581    retcode = RCS_checkout (rcsnode, NULL, NULL, NULL, NULL, temp,
582			    (RCSCHECKOUTPROC) NULL, (void *) NULL);
583    if (retcode != 0)
584    {
585	/* Probably not necessary (?); RCS_checkout already printed a
586	   message.  */
587	error (0, 0, "failed to check out %s file",
588	       file);
589    }
590    freercsnode (&rcsnode);
591    free (rcs);
592    return (retcode);
593}
594
595#ifndef MY_NDBM
596
597static void
598write_dbmfile (temp)
599    char *temp;
600{
601    char line[DBLKSIZ], value[DBLKSIZ];
602    FILE *fp;
603    DBM *db;
604    char *cp, *vp;
605    datum key, val;
606    int len, cont, err = 0;
607
608    fp = open_file (temp, "r");
609    if ((db = dbm_open (temp, O_RDWR | O_CREAT | O_TRUNC, 0666)) == NULL)
610	error (1, errno, "cannot open dbm file %s for creation", temp);
611    for (cont = 0; fgets (line, sizeof (line), fp) != NULL;)
612    {
613	if ((cp = strrchr (line, '\n')) != NULL)
614	    *cp = '\0';			/* strip the newline */
615
616	/*
617	 * Add the line to the value, at the end if this is a continuation
618	 * line; otherwise at the beginning, but only after any trailing
619	 * backslash is removed.
620	 */
621	vp = value;
622	if (cont)
623	    vp += strlen (value);
624
625	/*
626	 * See if the line we read is a continuation line, and strip the
627	 * backslash if so.
628	 */
629	len = strlen (line);
630	if (len > 0)
631	    cp = &line[len - 1];
632	else
633	    cp = line;
634	if (*cp == '\\')
635	{
636	    cont = 1;
637	    *cp = '\0';
638	}
639	else
640	{
641	    cont = 0;
642	}
643	(void) strcpy (vp, line);
644	if (value[0] == '#')
645	    continue;			/* comment line */
646	vp = value;
647	while (*vp && isspace ((unsigned char) *vp))
648	    vp++;
649	if (*vp == '\0')
650	    continue;			/* empty line */
651
652	/*
653	 * If this was not a continuation line, add the entry to the database
654	 */
655	if (!cont)
656	{
657	    key.dptr = vp;
658	    while (*vp && !isspace ((unsigned char) *vp))
659		vp++;
660	    key.dsize = vp - key.dptr;
661	    *vp++ = '\0';		/* NULL terminate the key */
662	    while (*vp && isspace ((unsigned char) *vp))
663		vp++;			/* skip whitespace to value */
664	    if (*vp == '\0')
665	    {
666		error (0, 0, "warning: NULL value for key `%s'", key.dptr);
667		continue;
668	    }
669	    val.dptr = vp;
670	    val.dsize = strlen (vp);
671	    if (dbm_store (db, key, val, DBM_INSERT) == 1)
672	    {
673		error (0, 0, "duplicate key found for `%s'", key.dptr);
674		err++;
675	    }
676	}
677    }
678    dbm_close (db);
679    if (fclose (fp) < 0)
680	error (0, errno, "cannot close %s", temp);
681    if (err)
682    {
683	/* I think that the size of the buffer needed here is
684	   just determined by sizeof (CVSROOTADM_MODULES), the
685	   filenames created by make_tempfile, and other things that won't
686	   overflow.  */
687	char dotdir[50], dotpag[50], dotdb[50];
688
689	(void) sprintf (dotdir, "%s.dir", temp);
690	(void) sprintf (dotpag, "%s.pag", temp);
691	(void) sprintf (dotdb, "%s.db", temp);
692	if (unlink_file (dotdir) < 0
693	    && !existence_error (errno))
694	    error (0, errno, "cannot remove %s", dotdir);
695	if (unlink_file (dotpag) < 0
696	    && !existence_error (errno))
697	    error (0, errno, "cannot remove %s", dotpag);
698	if (unlink_file (dotdb) < 0
699	    && !existence_error (errno))
700	    error (0, errno, "cannot remove %s", dotdb);
701	error (1, 0, "DBM creation failed; correct above errors");
702    }
703}
704
705static void
706rename_dbmfile (temp)
707    char *temp;
708{
709    /* I think that the size of the buffer needed here is
710       just determined by sizeof (CVSROOTADM_MODULES), the
711       filenames created by make_tempfile, and other things that won't
712       overflow.  */
713    char newdir[50], newpag[50], newdb[50];
714    char dotdir[50], dotpag[50], dotdb[50];
715    char bakdir[50], bakpag[50], bakdb[50];
716
717    int dir1_errno = 0, pag1_errno = 0, db1_errno = 0;
718    int dir2_errno = 0, pag2_errno = 0, db2_errno = 0;
719    int dir3_errno = 0, pag3_errno = 0, db3_errno = 0;
720
721    (void) sprintf (dotdir, "%s.dir", CVSROOTADM_MODULES);
722    (void) sprintf (dotpag, "%s.pag", CVSROOTADM_MODULES);
723    (void) sprintf (dotdb, "%s.db", CVSROOTADM_MODULES);
724    (void) sprintf (bakdir, "%s%s.dir", BAKPREFIX, CVSROOTADM_MODULES);
725    (void) sprintf (bakpag, "%s%s.pag", BAKPREFIX, CVSROOTADM_MODULES);
726    (void) sprintf (bakdb, "%s%s.db", BAKPREFIX, CVSROOTADM_MODULES);
727    (void) sprintf (newdir, "%s.dir", temp);
728    (void) sprintf (newpag, "%s.pag", temp);
729    (void) sprintf (newdb, "%s.db", temp);
730
731    (void) chmod (newdir, 0666);
732    (void) chmod (newpag, 0666);
733    (void) chmod (newdb, 0666);
734
735    /* don't mess with me */
736    SIG_beginCrSect ();
737
738    /* rm .#modules.dir .#modules.pag */
739    if (unlink_file (bakdir) < 0)
740	dir1_errno = errno;
741    if (unlink_file (bakpag) < 0)
742	pag1_errno = errno;
743    if (unlink_file (bakdb) < 0)
744	db1_errno = errno;
745
746    /* mv modules.dir .#modules.dir */
747    if (CVS_RENAME (dotdir, bakdir) < 0)
748	dir2_errno = errno;
749    /* mv modules.pag .#modules.pag */
750    if (CVS_RENAME (dotpag, bakpag) < 0)
751	pag2_errno = errno;
752    /* mv modules.db .#modules.db */
753    if (CVS_RENAME (dotdb, bakdb) < 0)
754	db2_errno = errno;
755
756    /* mv "temp".dir modules.dir */
757    if (CVS_RENAME (newdir, dotdir) < 0)
758	dir3_errno = errno;
759    /* mv "temp".pag modules.pag */
760    if (CVS_RENAME (newpag, dotpag) < 0)
761	pag3_errno = errno;
762    /* mv "temp".db modules.db */
763    if (CVS_RENAME (newdb, dotdb) < 0)
764	db3_errno = errno;
765
766    /* OK -- make my day */
767    SIG_endCrSect ();
768
769    /* I didn't want to call error() when we had signals blocked
770       (unnecessary?), but do it now.  */
771    if (dir1_errno && !existence_error (dir1_errno))
772	error (0, dir1_errno, "cannot remove %s", bakdir);
773    if (pag1_errno && !existence_error (pag1_errno))
774	error (0, pag1_errno, "cannot remove %s", bakpag);
775    if (db1_errno && !existence_error (db1_errno))
776	error (0, db1_errno, "cannot remove %s", bakdb);
777
778    if (dir2_errno && !existence_error (dir2_errno))
779	error (0, dir2_errno, "cannot remove %s", bakdir);
780    if (pag2_errno && !existence_error (pag2_errno))
781	error (0, pag2_errno, "cannot remove %s", bakpag);
782    if (db2_errno && !existence_error (db2_errno))
783	error (0, db2_errno, "cannot remove %s", bakdb);
784
785    if (dir3_errno && !existence_error (dir3_errno))
786	error (0, dir3_errno, "cannot remove %s", bakdir);
787    if (pag3_errno && !existence_error (pag3_errno))
788	error (0, pag3_errno, "cannot remove %s", bakpag);
789    if (db3_errno && !existence_error (db3_errno))
790	error (0, db3_errno, "cannot remove %s", bakdb);
791}
792
793#endif				/* !MY_NDBM */
794
795static void
796rename_rcsfile (temp, real)
797    char *temp;
798    char *real;
799{
800    char *bak;
801    struct stat statbuf;
802    char *rcs;
803
804    /* Set "x" bits if set in original. */
805    rcs = xmalloc (strlen (real) + sizeof (RCSEXT) + 10);
806    (void) sprintf (rcs, "%s%s", real, RCSEXT);
807    statbuf.st_mode = 0; /* in case rcs file doesn't exist, but it should... */
808    if (CVS_STAT (rcs, &statbuf) < 0
809	&& !existence_error (errno))
810	error (0, errno, "cannot stat %s", rcs);
811    free (rcs);
812
813    if (chmod (temp, 0444 | (statbuf.st_mode & 0111)) < 0)
814	error (0, errno, "warning: cannot chmod %s", temp);
815    bak = xmalloc (strlen (real) + sizeof (BAKPREFIX) + 10);
816    (void) sprintf (bak, "%s%s", BAKPREFIX, real);
817
818    /* rm .#loginfo */
819    if (unlink_file (bak) < 0
820	&& !existence_error (errno))
821	error (0, errno, "cannot remove %s", bak);
822
823    /* mv loginfo .#loginfo */
824    if (CVS_RENAME (real, bak) < 0
825	&& !existence_error (errno))
826	error (0, errno, "cannot rename %s to %s", real, bak);
827
828    /* mv "temp" loginfo */
829    if (CVS_RENAME (temp, real) < 0
830	&& !existence_error (errno))
831	error (0, errno, "cannot rename %s to %s", temp, real);
832
833    free (bak);
834}
835
836const char *const init_usage[] = {
837    "Usage: %s %s\n",
838    "(Specify the --help global option for a list of other help options)\n",
839    NULL
840};
841
842int
843init (argc, argv)
844    int argc;
845    char **argv;
846{
847    /* Name of CVSROOT directory.  */
848    char *adm;
849    /* Name of this administrative file.  */
850    char *info;
851    /* Name of ,v file for this administrative file.  */
852    char *info_v;
853    /* Exit status.  */
854    int err;
855
856    const struct admin_file *fileptr;
857
858    umask (cvsumask);
859
860    if (argc == -1 || argc > 1)
861	usage (init_usage);
862
863#ifdef CLIENT_SUPPORT
864    if (current_parsed_root->isremote)
865    {
866	start_server ();
867
868	ign_setup ();
869	send_init_command ();
870	return get_responses_and_close ();
871    }
872#endif /* CLIENT_SUPPORT */
873
874    /* Note: we do *not* create parent directories as needed like the
875       old cvsinit.sh script did.  Few utilities do that, and a
876       non-existent parent directory is as likely to be a typo as something
877       which needs to be created.  */
878    mkdir_if_needed (current_parsed_root->directory);
879
880    adm = xmalloc (strlen (current_parsed_root->directory) + sizeof (CVSROOTADM) + 2);
881    sprintf (adm, "%s/%s", current_parsed_root->directory, CVSROOTADM);
882    mkdir_if_needed (adm);
883
884    /* This is needed because we pass "fileptr->filename" not "info"
885       to add_rcs_file below.  I think this would be easy to change,
886       thus nuking the need for CVS_CHDIR here, but I haven't looked
887       closely (e.g. see wrappers calls within add_rcs_file).  */
888    if ( CVS_CHDIR (adm) < 0)
889	error (1, errno, "cannot change to directory %s", adm);
890
891    /* Make Emptydir so it's there if we need it */
892    mkdir_if_needed (CVSNULLREPOS);
893
894    /* 80 is long enough for all the administrative file names, plus
895       "/" and so on.  */
896    info = xmalloc (strlen (adm) + 80);
897    info_v = xmalloc (strlen (adm) + 80);
898    for (fileptr = filelist; fileptr && fileptr->filename; ++fileptr)
899    {
900	if (fileptr->contents == NULL)
901	    continue;
902	strcpy (info, adm);
903	strcat (info, "/");
904	strcat (info, fileptr->filename);
905	strcpy (info_v, info);
906	strcat (info_v, RCSEXT);
907	if (isfile (info_v))
908	    /* We will check out this file in the mkmodules step.
909	       Nothing else is required.  */
910	    ;
911	else
912	{
913	    int retcode;
914
915	    if (!isfile (info))
916	    {
917		FILE *fp;
918		const char * const *p;
919
920		fp = open_file (info, "w");
921		for (p = fileptr->contents; *p != NULL; ++p)
922		    if (fputs (*p, fp) < 0)
923			error (1, errno, "cannot write %s", info);
924		if (fclose (fp) < 0)
925		    error (1, errno, "cannot close %s", info);
926	    }
927	    /* The message used to say " of " and fileptr->filename after
928	       "initial checkin" but I fail to see the point as we know what
929	       file it is from the name.  */
930	    retcode = add_rcs_file ("initial checkin", info_v,
931				    fileptr->filename, "1.1", NULL,
932
933				    /* No vendor branch.  */
934				    NULL, NULL, 0, NULL,
935
936				    NULL, 0, NULL);
937	    if (retcode != 0)
938		/* add_rcs_file already printed an error message.  */
939		err = 1;
940	}
941    }
942
943    /* Turn on history logging by default.  The user can remove the file
944       to disable it.  */
945    strcpy (info, adm);
946    strcat (info, "/");
947    strcat (info, CVSROOTADM_HISTORY);
948    if (!isfile (info))
949    {
950	FILE *fp;
951
952	fp = open_file (info, "w");
953	if (fclose (fp) < 0)
954	    error (1, errno, "cannot close %s", info);
955
956        /* Make the new history file world-writeable, since every CVS
957           user will need to be able to write to it.  We use chmod()
958           because xchmod() is too shy. */
959        chmod (info, 0666);
960    }
961
962    /* Make an empty val-tags file to prevent problems creating it later.  */
963    strcpy (info, adm);
964    strcat (info, "/");
965    strcat (info, CVSROOTADM_VALTAGS);
966    if (!isfile (info))
967    {
968	FILE *fp;
969
970	fp = open_file (info, "w");
971	if (fclose (fp) < 0)
972	    error (1, errno, "cannot close %s", info);
973
974        /* Make the new val-tags file world-writeable, since every CVS
975           user will need to be able to write to it.  We use chmod()
976           because xchmod() is too shy. */
977        chmod (info, 0666);
978    }
979
980    free (info);
981    free (info_v);
982
983    mkmodules (adm);
984
985    free (adm);
986    return 0;
987}
988