mkmodules.c revision 177404
151974Smsmith/*
265245Smsmith * Copyright (C) 1986-2008 The Free Software Foundation, Inc.
365245Smsmith *
451974Smsmith * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
551974Smsmith *                                  and others.
651974Smsmith *
751974Smsmith * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
851974Smsmith * Portions Copyright (C) 1989-1992, Brian Berliner
951974Smsmith *
1051974Smsmith * You may distribute under the terms of the GNU General Public License as
1151974Smsmith * specified in the README file that comes with the CVS kit.
1251974Smsmith *
1351974Smsmith * $FreeBSD: head/contrib/cvs/src/mkmodules.c 177404 2008-03-19 15:11:46Z obrien $
1451974Smsmith */
1551974Smsmith
1651974Smsmith#include <assert.h>
1751974Smsmith#include "cvs.h"
1851974Smsmith#include "getline.h"
1951974Smsmith#include "history.h"
2051974Smsmith#include "savecwd.h"
2151974Smsmith
2251974Smsmith#ifndef DBLKSIZ
2351974Smsmith#define	DBLKSIZ	4096			/* since GNU ndbm doesn't define it */
2451974Smsmith#endif
2551974Smsmith
2651974Smsmithstatic int checkout_file PROTO((char *file, char *temp));
2751974Smsmithstatic char *make_tempfile PROTO((void));
2851974Smsmithstatic void rename_rcsfile PROTO((char *temp, char *real));
2951974Smsmith
3065245Smsmith#ifndef MY_NDBM
3165245Smsmithstatic void rename_dbmfile PROTO((char *temp));
3265245Smsmithstatic void write_dbmfile PROTO((char *temp));
3365245Smsmith#endif				/* !MY_NDBM */
3465245Smsmith
3565245Smsmith/* Structure which describes an administrative file.  */
3651974Smsmithstruct admin_file {
3765245Smsmith   /* Name of the file, within the CVSROOT directory.  */
3865245Smsmith   char *filename;
3965245Smsmith
4065245Smsmith   /* This is a one line description of what the file is for.  It is not
4165245Smsmith      currently used, although one wonders whether it should be, somehow.
4265245Smsmith      If NULL, then don't process this file in mkmodules (FIXME?: a bit of
4365245Smsmith      a kludge; probably should replace this with a flags field).  */
4465245Smsmith   char *errormsg;
4565245Smsmith
4665245Smsmith   /* Contents which the file should have in a new repository.  To avoid
4765245Smsmith      problems with brain-dead compilers which choke on long string constants,
4865245Smsmith      this is a pointer to an array of char * terminated by NULL--each of
4965245Smsmith      the strings is concatenated.
5065245Smsmith
5165245Smsmith      If this field is NULL, the file is not created in a new
5265245Smsmith      repository, but it can be added with "cvs add" (just as if one
5365245Smsmith      had created the repository with a version of CVS which didn't
5465245Smsmith      know about the file) and the checked-out copy will be updated
5565245Smsmith      without having to add it to checkoutlist.  */
5665245Smsmith   const char * const *contents;
5765245Smsmith};
5865245Smsmith
5965245Smsmithstatic const char *const loginfo_contents[] = {
6065245Smsmith    "# The \"loginfo\" file controls where \"cvs commit\" log information\n",
6165245Smsmith    "# is sent.  The first entry on a line is a regular expression which must match\n",
6265245Smsmith    "# the directory that the change is being made to, relative to the\n",
6365245Smsmith    "# $CVSROOT.  If a match is found, then the remainder of the line is a filter\n",
6465245Smsmith    "# program that should expect log information on its standard input.\n",
6565245Smsmith    "#\n",
6665245Smsmith    "# If the repository name does not match any of the regular expressions in this\n",
6765245Smsmith    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
6865245Smsmith    "#\n",
6965245Smsmith    "# If the name ALL appears as a regular expression it is always used\n",
7065245Smsmith    "# in addition to the first matching regex or DEFAULT.\n",
7165245Smsmith    "#\n",
7265245Smsmith    "# You may specify a format string as part of the\n",
7365245Smsmith    "# filter.  The string is composed of a `%' followed\n",
7451974Smsmith    "# by a single format character, or followed by a set of format\n",
7551974Smsmith    "# characters surrounded by `{' and `}' as separators.  The format\n",
7665245Smsmith    "# characters are:\n",
7765245Smsmith    "#\n",
7865245Smsmith    "#   s = file name\n",
7965245Smsmith    "#   V = old version number (pre-checkin)\n",
8065245Smsmith    "#   v = new version number (post-checkin)\n",
8165245Smsmith    "#\n",
8265245Smsmith    "# For example:\n",
8351974Smsmith    "#DEFAULT (echo \"\"; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
8465245Smsmith    "# or\n",
8565245Smsmith    "#DEFAULT (echo \"\"; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
8665245Smsmith    NULL
8765245Smsmith};
8865245Smsmith
8951974Smsmithstatic const char *const rcsinfo_contents[] = {
9051974Smsmith    "# The \"rcsinfo\" file is used to control templates with which the editor\n",
9151974Smsmith    "# is invoked on commit and import.\n",
9251974Smsmith    "#\n",
9351974Smsmith    "# The first entry on a line is a regular expression which is tested\n",
9451974Smsmith    "# against the directory that the change is being made to, relative to the\n",
9551974Smsmith    "# $CVSROOT.  For the first match that is found, then the remainder of the\n",
9651974Smsmith    "# line is the name of the file that contains the template.\n",
9751974Smsmith    "#\n",
9851974Smsmith    "# If the repository name does not match any of the regular expressions in this\n",
9951974Smsmith    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
10051974Smsmith    "#\n",
10165245Smsmith    "# If the name \"ALL\" appears as a regular expression it is always used\n",
10251974Smsmith    "# in addition to the first matching regex or \"DEFAULT\".\n",
10365245Smsmith    NULL
10465245Smsmith};
10565245Smsmith
10665245Smsmithstatic const char *const editinfo_contents[] = {
10765245Smsmith    "# The \"editinfo\" file is used to allow verification of logging\n",
10865245Smsmith    "# information.  It works best when a template (as specified in the\n",
10965245Smsmith    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
11065245Smsmith    "# template with locations for, a bug-id number, a list of people who\n",
11165245Smsmith    "# reviewed the code before it can be checked in, and an external\n",
11251974Smsmith    "# process to catalog the differences that were code reviewed, the\n",
11351974Smsmith    "# following test can be applied to the code:\n",
11465245Smsmith    "#\n",
11551974Smsmith    "#   Making sure that the entered bug-id number is correct.\n",
11665245Smsmith    "#   Validating that the code that was reviewed is indeed the code being\n",
11765245Smsmith    "#       checked in (using the bug-id number or a seperate review\n",
11865245Smsmith    "#       number to identify this particular code set.).\n",
11965245Smsmith    "#\n",
12051974Smsmith    "# If any of the above test failed, then the commit would be aborted.\n",
12151974Smsmith    "#\n",
12265245Smsmith    "# Actions such as mailing a copy of the report to each reviewer are\n",
12351974Smsmith    "# better handled by an entry in the loginfo file.\n",
12465245Smsmith    "#\n",
12565245Smsmith    "# One thing that should be noted is the the ALL keyword is not\n",
12665245Smsmith    "# supported.  There can be only one entry that matches a given\n",
12765245Smsmith    "# repository.\n",
12865245Smsmith    NULL
12965245Smsmith};
13065245Smsmith
13165245Smsmithstatic const char *const verifymsg_contents[] = {
13265245Smsmith    "# The \"verifymsg\" file is used to allow verification of logging\n",
13351974Smsmith    "# information.  It works best when a template (as specified in the\n",
13465245Smsmith    "# rcsinfo file) is provided for the logging procedure.  Given a\n",
13565245Smsmith    "# template with locations for, a bug-id number, a list of people who\n",
13665245Smsmith    "# reviewed the code before it can be checked in, and an external\n",
13765245Smsmith    "# process to catalog the differences that were code reviewed, the\n",
13865245Smsmith    "# following test can be applied to the code:\n",
13965245Smsmith    "#\n",
14065245Smsmith    "#   Making sure that the entered bug-id number is correct.\n",
14151974Smsmith    "#   Validating that the code that was reviewed is indeed the code being\n",
14265245Smsmith    "#       checked in (using the bug-id number or a seperate review\n",
14351974Smsmith    "#       number to identify this particular code set.).\n",
14451974Smsmith    "#\n",
14551974Smsmith    "# If any of the above test failed, then the commit would be aborted.\n",
14651974Smsmith    "#\n",
14751974Smsmith    "# Actions such as mailing a copy of the report to each reviewer are\n",
14851974Smsmith    "# better handled by an entry in the loginfo file.\n",
14965245Smsmith    "#\n",
15065245Smsmith    "# One thing that should be noted is the the ALL keyword is not\n",
15165245Smsmith    "# supported.  There can be only one entry that matches a given\n",
15251974Smsmith    "# repository.\n",
15351974Smsmith    NULL
15451974Smsmith};
15551974Smsmith
15651974Smsmithstatic const char *const commitinfo_contents[] = {
15751974Smsmith    "# The \"commitinfo\" file is used to control pre-commit checks.\n",
15851974Smsmith    "# The filter on the right is invoked with the repository and a list \n",
15951974Smsmith    "# of files to check.  A non-zero exit of the filter program will \n",
16051974Smsmith    "# cause the commit to be aborted.\n",
16151974Smsmith    "#\n",
16251974Smsmith    "# The first entry on a line is a regular expression which is tested\n",
16351974Smsmith    "# against the directory that the change is being committed to, relative\n",
16465245Smsmith    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
16565245Smsmith    "# of the line is the name of the filter to run.\n",
16665245Smsmith    "#\n",
16765245Smsmith    "# If the repository name does not match any of the regular expressions in this\n",
16865245Smsmith    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
16965245Smsmith    "#\n",
17065245Smsmith    "# If the name \"ALL\" appears as a regular expression it is always used\n",
171103870Salfred    "# in addition to the first matching regex or \"DEFAULT\".\n",
17251974Smsmith    NULL
17365245Smsmith};
17465245Smsmith
17565245Smsmithstatic const char *const taginfo_contents[] = {
17651974Smsmith    "# The \"taginfo\" file is used to control pre-tag checks.\n",
17751974Smsmith    "# The filter on the right is invoked with the following arguments:\n",
17851974Smsmith    "#\n",
17951974Smsmith    "# $1 -- tagname\n",
18051974Smsmith    "# $2 -- operation \"add\" for tag, \"mov\" for tag -F, and \"del\" for tag -d\n",
18151974Smsmith    "# $3 -- repository\n",
18251974Smsmith    "# $4->  file revision [file revision ...]\n",
183103870Salfred    "#\n",
18451974Smsmith    "# A non-zero exit of the filter program will cause the tag to be aborted.\n",
18565245Smsmith    "#\n",
18665245Smsmith    "# The first entry on a line is a regular expression which is tested\n",
18765245Smsmith    "# against the directory that the change is being committed to, relative\n",
18851974Smsmith    "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
18951974Smsmith    "# of the line is the name of the filter to run.\n",
19065245Smsmith    "#\n",
19165245Smsmith    "# If the repository name does not match any of the regular expressions in this\n",
192103870Salfred    "# file, the \"DEFAULT\" line is used, if it is specified.\n",
19351974Smsmith    "#\n",
19465245Smsmith    "# If the name \"ALL\" appears as a regular expression it is always used\n",
19565245Smsmith    "# in addition to the first matching regex or \"DEFAULT\".\n",
19665245Smsmith    NULL
19765245Smsmith};
19865245Smsmith
19951974Smsmithstatic const char *const checkoutlist_contents[] = {
20051974Smsmith    "# The \"checkoutlist\" file is used to support additional version controlled\n",
20165245Smsmith    "# administrative files in $CVSROOT/CVSROOT, such as template files.\n",
20265245Smsmith    "#\n",
20365245Smsmith    "# The first entry on a line is a filename which will be checked out from\n",
20465245Smsmith    "# the corresponding RCS file in the $CVSROOT/CVSROOT directory.\n",
20565245Smsmith    "# The remainder of the line is an error message to use if the file cannot\n",
20665245Smsmith    "# be checked out.\n",
20765245Smsmith    "#\n",
20865245Smsmith    "# File format:\n",
20965245Smsmith    "#\n",
21065245Smsmith    "#	[<whitespace>]<filename>[<whitespace><error message>]<end-of-line>\n",
21165245Smsmith    "#\n",
21265245Smsmith    "# comment lines begin with '#'\n",
21365245Smsmith    NULL
21465245Smsmith};
21565245Smsmith
21665245Smsmithstatic const char *const cvswrappers_contents[] = {
21765245Smsmith    "# This file affects handling of files based on their names.\n",
21865245Smsmith    "#\n",
219103870Salfred#if 0    /* see comments in wrap_add in wrapper.c */
22051974Smsmith    "# The -t/-f options allow one to treat directories of files\n",
22165245Smsmith    "# as a single file, or to transform a file in other ways on\n",
22265245Smsmith    "# its way in and out of CVS.\n",
22365245Smsmith    "#\n",
22465245Smsmith#endif
22565245Smsmith    "# The -m option specifies whether CVS attempts to merge files.\n",
22665245Smsmith    "#\n",
22765245Smsmith    "# The -k option specifies keyword expansion (e.g. -kb for binary).\n",
22865245Smsmith    "#\n",
22965245Smsmith    "# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)\n",
23065245Smsmith    "#\n",
23165245Smsmith    "#  wildcard	[option value][option value]...\n",
23265245Smsmith    "#\n",
23365245Smsmith    "#  where option is one of\n",
23465245Smsmith    "#  -f		from cvs filter		value: path to filter\n",
23565245Smsmith    "#  -t		to cvs filter		value: path to filter\n",
23665245Smsmith    "#  -m		update methodology	value: MERGE or COPY\n",
23765245Smsmith    "#  -k		expansion mode		value: b, o, kkv, &c\n",
23865245Smsmith    "#\n",
23951974Smsmith    "#  and value is a single-quote delimited value.\n",
24051974Smsmith    "# For example:\n",
24151974Smsmith    "#*.gif -k 'b'\n",
24251974Smsmith    NULL
24351974Smsmith};
24451974Smsmith
24551974Smsmithstatic const char *const notify_contents[] = {
24651974Smsmith    "# The \"notify\" file controls where notifications from watches set by\n",
24751974Smsmith    "# \"cvs watch add\" or \"cvs edit\" are sent.  The first entry on a line is\n",
24851974Smsmith    "# a regular expression which is tested against the directory that the\n",
24951974Smsmith    "# change is being made to, relative to the $CVSROOT.  If it matches,\n",
25051974Smsmith    "# then the remainder of the line is a filter program that should contain\n",
25151974Smsmith    "# one occurrence of %s for the user to notify, and information on its\n",
25251974Smsmith    "# standard input.\n",
25351974Smsmith    "#\n",
25451974Smsmith    "# \"ALL\" or \"DEFAULT\" can be used in place of the regular expression.\n",
25551974Smsmith    "#\n",
25651974Smsmith    "# For example:\n",
257103870Salfred    "#ALL mail -s \"CVS notification\" %s\n",
25851974Smsmith    NULL
25965245Smsmith};
26065245Smsmith
26165245Smsmithstatic const char *const modules_contents[] = {
26265245Smsmith    "# Three different line formats are valid:\n",
26365245Smsmith    "#	key	-a    aliases...\n",
26465245Smsmith    "#	key [options] directory\n",
26565245Smsmith    "#	key [options] directory files...\n",
26665245Smsmith    "#\n",
26765245Smsmith    "# Where \"options\" are composed of:\n",
26865245Smsmith    "#	-o prog		Run \"prog\" on \"cvs checkout\" of module.\n",
26965245Smsmith    "#	-e prog		Run \"prog\" on \"cvs export\" of module.\n",
27065245Smsmith    "#	-t prog		Run \"prog\" on \"cvs rtag\" of module.\n",
27165245Smsmith    "#	-u prog		Run \"prog\" on \"cvs update\" of module.\n",
27265245Smsmith    "#	-d dir		Place module in directory \"dir\" instead of module name.\n",
27365245Smsmith    "#	-l		Top-level directory only -- do not recurse.\n",
27465245Smsmith    "#\n",
27565245Smsmith    "# NOTE:  If you change any of the \"Run\" options above, you'll have to\n",
27665245Smsmith    "# release and re-checkout any working directories of these modules.\n",
27765245Smsmith    "#\n",
27865245Smsmith    "# And \"directory\" is a path to a directory relative to $CVSROOT.\n",
27965245Smsmith    "#\n",
28065245Smsmith    "# The \"-a\" option specifies an alias.  An alias is interpreted as if\n",
28165245Smsmith    "# everything on the right of the \"-a\" had been typed on the command line.\n",
28265245Smsmith    "#\n",
28365245Smsmith    "# You can encode a module within a module by using the special '&'\n",
28465245Smsmith    "# character to interpose another module into the current module.  This\n",
28565245Smsmith    "# can be useful for creating a module that consists of many directories\n",
28665245Smsmith    "# spread out over the entire source repository.\n",
28765245Smsmith    NULL
28865245Smsmith};
28965245Smsmith
29065245Smsmithstatic const char *const config_contents[] = {
29165245Smsmith    "# Set this to \"no\" if pserver shouldn't check system users/passwords\n",
29265245Smsmith    "#SystemAuth=yes\n",
29365245Smsmith    "\n",
29465245Smsmith    "# Set `IgnoreUnknownConfigKeys' to `yes' to ignore unknown config\n",
29565245Smsmith    "# keys which are supported in a future version of CVS.\n",
29665245Smsmith    "# This option is intended to be useful as a transition for read-only\n",
29765245Smsmith    "# mirror sites when sites may need to be updated later than the\n",
29865245Smsmith    "# primary CVS repository.\n",
29965245Smsmith    "#IgnoreUnknownConfigKeys=no\n",
30065245Smsmith    "\n",
30165245Smsmith    "# Put CVS lock files in this directory rather than directly in the repository.\n",
30265245Smsmith    "#LockDir=/var/lock/cvs\n",
30365245Smsmith    "\n",
30465245Smsmith#ifdef PRESERVE_PERMISSIONS_SUPPORT
30565245Smsmith    "# Set `PreservePermissions' to `yes' to save file status information\n",
30665245Smsmith    "# in the repository.\n",
30765245Smsmith    "#PreservePermissions=no\n",
30865245Smsmith    "\n",
30965245Smsmith#endif
31065245Smsmith    "# Set `TopLevelAdmin' to `yes' to create a CVS directory at the top\n",
31165245Smsmith    "# level of the new working directory when using the `cvs checkout'\n",
31265245Smsmith    "# command.\n",
31365245Smsmith    "#TopLevelAdmin=no\n",
31465245Smsmith    "\n",
31565245Smsmith    "# Set `LogHistory' to `all' or `" ALL_HISTORY_REC_TYPES "' to log all transactions to the\n",
31665245Smsmith    "# history file, or a subset as needed (ie `TMAR' logs all write operations)\n",
31765245Smsmith    "#LogHistory=" ALL_HISTORY_REC_TYPES "\n",
31865245Smsmith    "\n",
31965245Smsmith    "# Set `RereadLogAfterVerify' to `always' (the default) to allow the verifymsg\n",
32065245Smsmith    "# script to change the log message.  Set it to `stat' to force CVS to verify\n",
32165245Smsmith    "# that the file has changed before reading it (this can take up to an extra\n",
322103870Salfred    "# second per directory being committed, so it is not recommended for large\n",
32365245Smsmith    "# repositories.  Set it to `never' (the previous CVS behavior) to prevent\n",
32465245Smsmith    "# verifymsg scripts from changing the log message.\n",
32565245Smsmith    "#RereadLogAfterVerify=always\n",
32665245Smsmith    NULL
32765245Smsmith};
32865245Smsmith
32965245Smsmithstatic const struct admin_file filelist[] = {
33065245Smsmith    {CVSROOTADM_LOGINFO,
33165245Smsmith	"no logging of 'cvs commit' messages is done without a %s file",
33265245Smsmith	&loginfo_contents[0]},
33365245Smsmith    {CVSROOTADM_RCSINFO,
33465245Smsmith	"a %s file can be used to configure 'cvs commit' templates",
33565245Smsmith	rcsinfo_contents},
33665245Smsmith    {CVSROOTADM_EDITINFO,
33765245Smsmith	"a %s file can be used to validate log messages",
33865245Smsmith	editinfo_contents},
33965245Smsmith    {CVSROOTADM_VERIFYMSG,
34065245Smsmith	"a %s file can be used to validate log messages",
34165245Smsmith	verifymsg_contents},
34265245Smsmith    {CVSROOTADM_COMMITINFO,
34365245Smsmith	"a %s file can be used to configure 'cvs commit' checking",
34465245Smsmith	commitinfo_contents},
34565245Smsmith    {CVSROOTADM_TAGINFO,
34665245Smsmith	"a %s file can be used to configure 'cvs tag' checking",
34765245Smsmith	taginfo_contents},
34865245Smsmith    {CVSROOTADM_IGNORE,
349103870Salfred	"a %s file can be used to specify files to ignore",
35065245Smsmith	NULL},
35165245Smsmith    {CVSROOTADM_CHECKOUTLIST,
35265245Smsmith	"a %s file can specify extra CVSROOT files to auto-checkout",
35365245Smsmith	checkoutlist_contents},
35465245Smsmith    {CVSROOTADM_WRAPPER,
35565245Smsmith	"a %s file can be used to specify files to treat as wrappers",
35665245Smsmith	cvswrappers_contents},
35765245Smsmith    {CVSROOTADM_NOTIFY,
35851974Smsmith	"a %s file can be used to specify where notifications go",
35951974Smsmith	notify_contents},
36051974Smsmith    {CVSROOTADM_MODULES,
36151974Smsmith	/* modules is special-cased in mkmodules.  */
36251974Smsmith	NULL,
36351974Smsmith	modules_contents},
36451974Smsmith    {CVSROOTADM_READERS,
36551974Smsmith	"a %s file specifies read-only users",
36651974Smsmith	NULL},
36751974Smsmith    {CVSROOTADM_WRITERS,
36851974Smsmith	"a %s file specifies read/write users",
36951974Smsmith	NULL},
37051974Smsmith
37151974Smsmith    /* Some have suggested listing CVSROOTADM_PASSWD here too.  This
37251974Smsmith       would mean that CVS commands which operate on the
37351974Smsmith       CVSROOTADM_PASSWD file would transmit hashed passwords over the
37451974Smsmith       net.  This might seem to be no big deal, as pserver normally
37551974Smsmith       transmits cleartext passwords, but the difference is that
37651974Smsmith       CVSROOTADM_PASSWD contains *all* passwords, not just the ones
377103870Salfred       currently being used.  For example, it could be too easy to
37851974Smsmith       accidentally give someone readonly access to CVSROOTADM_PASSWD
37951974Smsmith       (e.g. via anonymous CVS or cvsweb), and then if there are any
38051974Smsmith       guessable passwords for read/write access (usually there will be)
38151974Smsmith       they get read/write access.
38251974Smsmith
383103870Salfred       Another worry is the implications of storing old passwords--if
38451974Smsmith       someone used a password in the past they might be using it
38551974Smsmith       elsewhere, using a similar password, etc, and so saving old
38651974Smsmith       passwords, even hashed, is probably not a good idea.  */
38751974Smsmith
38851974Smsmith    {CVSROOTADM_CONFIG,
38951974Smsmith	 "a %s file configures various behaviors",
39051974Smsmith	 config_contents},
39165245Smsmith    {NULL, NULL, NULL}
39251974Smsmith};
39351974Smsmith
39451974Smsmith/* Rebuild the checked out administrative files in directory DIR.  */
39565245Smsmithint
39651974Smsmithmkmodules (dir)
39751974Smsmith    char *dir;
39851974Smsmith{
39951974Smsmith    struct saved_cwd cwd;
40051974Smsmith    char *temp;
40165245Smsmith    char *cp, *last, *fname;
402103870Salfred#ifdef MY_NDBM
40351974Smsmith    DBM *db;
40451974Smsmith#endif
40551974Smsmith    FILE *fp;
40651974Smsmith    char *line = NULL;
40751974Smsmith    size_t line_allocated = 0;
408103870Salfred    const struct admin_file *fileptr;
40951974Smsmith
41065245Smsmith    if (noexec)
41165245Smsmith	return 0;
41265245Smsmith
41365245Smsmith    if (save_cwd (&cwd))
41465245Smsmith	error_exit ();
41565245Smsmith
41665245Smsmith    if ( CVS_CHDIR (dir) < 0)
41765245Smsmith	error (1, errno, "cannot chdir to %s", dir);
41865245Smsmith
41965245Smsmith    /*
42065245Smsmith     * First, do the work necessary to update the "modules" database.
42165245Smsmith     */
42265245Smsmith    temp = make_tempfile ();
42365245Smsmith    switch (checkout_file (CVSROOTADM_MODULES, temp))
42465245Smsmith    {
42565245Smsmith
42665245Smsmith	case 0:			/* everything ok */
42765245Smsmith#ifdef MY_NDBM
42865245Smsmith	    /* open it, to generate any duplicate errors */
429103870Salfred	    if ((db = dbm_open (temp, O_RDONLY, 0666)) != NULL)
43051974Smsmith		dbm_close (db);
43165245Smsmith#else
43265245Smsmith	    write_dbmfile (temp);
43365245Smsmith	    rename_dbmfile (temp);
43465245Smsmith#endif
43565245Smsmith	    rename_rcsfile (temp, CVSROOTADM_MODULES);
43665245Smsmith	    break;
43765245Smsmith
43865245Smsmith	default:
43970285Smsmith	    error (0, 0,
44070285Smsmith		"'cvs checkout' is less functional without a %s file",
44165245Smsmith		CVSROOTADM_MODULES);
44265245Smsmith	    break;
44365245Smsmith    }					/* switch on checkout_file() */
44465245Smsmith
44565245Smsmith    if (unlink_file (temp) < 0
44665245Smsmith	&& !existence_error (errno))
44765245Smsmith	error (0, errno, "cannot remove %s", temp);
44865245Smsmith    free (temp);
44965245Smsmith
45065245Smsmith    /* Checkout the files that need it in CVSROOT dir */
45165245Smsmith    for (fileptr = filelist; fileptr && fileptr->filename; fileptr++) {
45265245Smsmith	if (fileptr->errormsg == NULL)
45365245Smsmith	    continue;
45465245Smsmith	temp = make_tempfile ();
45565245Smsmith	if (checkout_file (fileptr->filename, temp) == 0)
45665245Smsmith	    rename_rcsfile (temp, fileptr->filename);
45765245Smsmith#if 0
45865245Smsmith	/*
45965245Smsmith	 * If there was some problem other than the file not existing,
46065245Smsmith	 * checkout_file already printed a real error message.  If the
46165245Smsmith	 * file does not exist, it is harmless--it probably just means
46265245Smsmith	 * that the repository was created with an old version of CVS
46365245Smsmith	 * which didn't have so many files in CVSROOT.
46465245Smsmith	 */
46565245Smsmith	else if (fileptr->errormsg)
46665245Smsmith	    error (0, 0, fileptr->errormsg, fileptr->filename);
46765245Smsmith#endif
46865245Smsmith	if (unlink_file (temp) < 0
46965245Smsmith	    && !existence_error (errno))
47065245Smsmith	    error (0, errno, "cannot remove %s", temp);
47165245Smsmith	free (temp);
47265245Smsmith    }
47365245Smsmith
47465245Smsmith    fp = CVS_FOPEN (CVSROOTADM_CHECKOUTLIST, "r");
47565245Smsmith    if (fp)
47665245Smsmith    {
47765245Smsmith	/*
47865245Smsmith	 * File format:
47965245Smsmith	 *  [<whitespace>]<filename>[<whitespace><error message>]<end-of-line>
48065245Smsmith	 *
48165245Smsmith	 * comment lines begin with '#'
48265245Smsmith	 */
48365245Smsmith	while (getline (&line, &line_allocated, fp) >= 0)
48465245Smsmith	{
48565245Smsmith	    /* skip lines starting with # */
48665245Smsmith	    if (line[0] == '#')
48765245Smsmith		continue;
48865245Smsmith
48965245Smsmith	    if ((last = strrchr (line, '\n')) != NULL)
49065245Smsmith		*last = '\0';			/* strip the newline */
49165245Smsmith
49265245Smsmith	    /* Skip leading white space. */
49365245Smsmith	    for (fname = line;
49465245Smsmith		 *fname && isspace ((unsigned char) *fname);
49565245Smsmith		 fname++)
49665245Smsmith		;
49765245Smsmith
49865245Smsmith	    /* Find end of filename. */
49965245Smsmith	    for (cp = fname; *cp && !isspace ((unsigned char) *cp); cp++)
50065245Smsmith		;
50165245Smsmith	    *cp = '\0';
50265245Smsmith
50365245Smsmith	    temp = make_tempfile ();
50465245Smsmith	    if (checkout_file (fname, temp) == 0)
50565245Smsmith	    {
50665245Smsmith		rename_rcsfile (temp, fname);
50765245Smsmith	    }
50865245Smsmith	    else
50965245Smsmith	    {
51065245Smsmith		/* Skip leading white space before the error message.  */
51165245Smsmith		for (cp++;
51265245Smsmith		     cp < last && *cp && isspace ((unsigned char) *cp);
51365245Smsmith		     cp++)
51465245Smsmith		    ;
51565245Smsmith		if (cp < last && *cp)
51665245Smsmith		    error (0, 0, "%s", cp);
51765245Smsmith	    }
51865245Smsmith	    if (unlink_file (temp) < 0
51965245Smsmith		&& !existence_error (errno))
52065245Smsmith		error (0, errno, "cannot remove %s", temp);
52165245Smsmith	    free (temp);
52265245Smsmith	}
52365245Smsmith	if (line)
52465245Smsmith	    free (line);
52565245Smsmith	if (ferror (fp))
52665245Smsmith	    error (0, errno, "cannot read %s", CVSROOTADM_CHECKOUTLIST);
52765245Smsmith	if (fclose (fp) < 0)
52865245Smsmith	    error (0, errno, "cannot close %s", CVSROOTADM_CHECKOUTLIST);
52965245Smsmith    }
53065245Smsmith    else
53165245Smsmith    {
53265245Smsmith	/* Error from CVS_FOPEN.  */
53365245Smsmith	if (!existence_error (errno))
53465245Smsmith	    error (0, errno, "cannot open %s", CVSROOTADM_CHECKOUTLIST);
53565245Smsmith    }
53665245Smsmith
53765245Smsmith    if (restore_cwd (&cwd, NULL))
53865245Smsmith	error_exit ();
53965245Smsmith    free_cwd (&cwd);
54065245Smsmith
54165245Smsmith    return (0);
54265245Smsmith}
54365245Smsmith
54465245Smsmith/*
54587796Sjhb * Yeah, I know, there are NFS race conditions here.
546 */
547static char *
548make_tempfile ()
549{
550    static int seed = 0;
551    int fd;
552    char *temp;
553
554    if (seed == 0)
555	seed = getpid ();
556    temp = xmalloc (sizeof (BAKPREFIX) + 40);
557    while (1)
558    {
559	(void) sprintf (temp, "%s%d", BAKPREFIX, seed++);
560	if ((fd = CVS_OPEN (temp, O_CREAT|O_EXCL|O_RDWR, 0666)) != -1)
561	    break;
562	if (errno != EEXIST)
563	    error (1, errno, "cannot create temporary file %s", temp);
564    }
565    if (close(fd) < 0)
566	error(1, errno, "cannot close temporary file %s", temp);
567    return temp;
568}
569
570/* Get a file.  If the file does not exist, return 1 silently.  If
571   there is an error, print a message and return 1 (FIXME: probably
572   not a very clean convention).  On success, return 0.  */
573
574static int
575checkout_file (file, temp)
576    char *file;
577    char *temp;
578{
579    char *rcs;
580    RCSNode *rcsnode;
581    int retcode = 0;
582
583    if (noexec)
584	return 0;
585
586    rcs = xmalloc (strlen (file) + 5);
587    strcpy (rcs, file);
588    strcat (rcs, RCSEXT);
589    if (!isfile (rcs))
590    {
591	free (rcs);
592	return (1);
593    }
594
595    rcsnode = RCS_parsercsfile (rcs);
596    if (!rcsnode)
597    {
598	/* Probably not necessary (?); RCS_parsercsfile already printed a
599	   message.  */
600	error (0, 0, "Failed to parse `%s'.", rcs);
601	free (rcs);
602	return 1;
603    }
604
605    retcode = RCS_checkout (rcsnode, NULL, NULL, NULL, NULL, temp,
606			    (RCSCHECKOUTPROC) NULL, (void *) NULL);
607    if (retcode != 0)
608    {
609	/* Probably not necessary (?); RCS_checkout already printed a
610	   message.  */
611	error (0, 0, "failed to check out %s file",
612	       file);
613    }
614    freercsnode (&rcsnode);
615    free (rcs);
616    return (retcode);
617}
618
619#ifndef MY_NDBM
620
621static void
622write_dbmfile (temp)
623    char *temp;
624{
625    char line[DBLKSIZ], value[DBLKSIZ];
626    FILE *fp;
627    DBM *db;
628    char *cp, *vp;
629    datum key, val;
630    int len, cont, err = 0;
631
632    fp = open_file (temp, "r");
633    if ((db = dbm_open (temp, O_RDWR | O_CREAT | O_TRUNC, 0666)) == NULL)
634	error (1, errno, "cannot open dbm file %s for creation", temp);
635    for (cont = 0; fgets (line, sizeof (line), fp) != NULL;)
636    {
637	if ((cp = strrchr (line, '\n')) != NULL)
638	    *cp = '\0';			/* strip the newline */
639
640	/*
641	 * Add the line to the value, at the end if this is a continuation
642	 * line; otherwise at the beginning, but only after any trailing
643	 * backslash is removed.
644	 */
645	vp = value;
646	if (cont)
647	    vp += strlen (value);
648
649	/*
650	 * See if the line we read is a continuation line, and strip the
651	 * backslash if so.
652	 */
653	len = strlen (line);
654	if (len > 0)
655	    cp = &line[len - 1];
656	else
657	    cp = line;
658	if (*cp == '\\')
659	{
660	    cont = 1;
661	    *cp = '\0';
662	}
663	else
664	{
665	    cont = 0;
666	}
667	(void) strcpy (vp, line);
668	if (value[0] == '#')
669	    continue;			/* comment line */
670	vp = value;
671	while (*vp && isspace ((unsigned char) *vp))
672	    vp++;
673	if (*vp == '\0')
674	    continue;			/* empty line */
675
676	/*
677	 * If this was not a continuation line, add the entry to the database
678	 */
679	if (!cont)
680	{
681	    key.dptr = vp;
682	    while (*vp && !isspace ((unsigned char) *vp))
683		vp++;
684	    key.dsize = vp - key.dptr;
685	    *vp++ = '\0';		/* NULL terminate the key */
686	    while (*vp && isspace ((unsigned char) *vp))
687		vp++;			/* skip whitespace to value */
688	    if (*vp == '\0')
689	    {
690		error (0, 0, "warning: NULL value for key `%s'", key.dptr);
691		continue;
692	    }
693	    val.dptr = vp;
694	    val.dsize = strlen (vp);
695	    if (dbm_store (db, key, val, DBM_INSERT) == 1)
696	    {
697		error (0, 0, "duplicate key found for `%s'", key.dptr);
698		err++;
699	    }
700	}
701    }
702    dbm_close (db);
703    if (fclose (fp) < 0)
704	error (0, errno, "cannot close %s", temp);
705    if (err)
706    {
707	/* I think that the size of the buffer needed here is
708	   just determined by sizeof (CVSROOTADM_MODULES), the
709	   filenames created by make_tempfile, and other things that won't
710	   overflow.  */
711	char dotdir[50], dotpag[50], dotdb[50];
712
713	(void) sprintf (dotdir, "%s.dir", temp);
714	(void) sprintf (dotpag, "%s.pag", temp);
715	(void) sprintf (dotdb, "%s.db", temp);
716	if (unlink_file (dotdir) < 0
717	    && !existence_error (errno))
718	    error (0, errno, "cannot remove %s", dotdir);
719	if (unlink_file (dotpag) < 0
720	    && !existence_error (errno))
721	    error (0, errno, "cannot remove %s", dotpag);
722	if (unlink_file (dotdb) < 0
723	    && !existence_error (errno))
724	    error (0, errno, "cannot remove %s", dotdb);
725	error (1, 0, "DBM creation failed; correct above errors");
726    }
727}
728
729static void
730rename_dbmfile (temp)
731    char *temp;
732{
733    /* I think that the size of the buffer needed here is
734       just determined by sizeof (CVSROOTADM_MODULES), the
735       filenames created by make_tempfile, and other things that won't
736       overflow.  */
737    char newdir[50], newpag[50], newdb[50];
738    char dotdir[50], dotpag[50], dotdb[50];
739    char bakdir[50], bakpag[50], bakdb[50];
740
741    int dir1_errno = 0, pag1_errno = 0, db1_errno = 0;
742    int dir2_errno = 0, pag2_errno = 0, db2_errno = 0;
743    int dir3_errno = 0, pag3_errno = 0, db3_errno = 0;
744
745    (void) sprintf (dotdir, "%s.dir", CVSROOTADM_MODULES);
746    (void) sprintf (dotpag, "%s.pag", CVSROOTADM_MODULES);
747    (void) sprintf (dotdb, "%s.db", CVSROOTADM_MODULES);
748    (void) sprintf (bakdir, "%s%s.dir", BAKPREFIX, CVSROOTADM_MODULES);
749    (void) sprintf (bakpag, "%s%s.pag", BAKPREFIX, CVSROOTADM_MODULES);
750    (void) sprintf (bakdb, "%s%s.db", BAKPREFIX, CVSROOTADM_MODULES);
751    (void) sprintf (newdir, "%s.dir", temp);
752    (void) sprintf (newpag, "%s.pag", temp);
753    (void) sprintf (newdb, "%s.db", temp);
754
755    (void) chmod (newdir, 0666);
756    (void) chmod (newpag, 0666);
757    (void) chmod (newdb, 0666);
758
759    /* don't mess with me */
760    SIG_beginCrSect ();
761
762    /* rm .#modules.dir .#modules.pag */
763    if (unlink_file (bakdir) < 0)
764	dir1_errno = errno;
765    if (unlink_file (bakpag) < 0)
766	pag1_errno = errno;
767    if (unlink_file (bakdb) < 0)
768	db1_errno = errno;
769
770    /* mv modules.dir .#modules.dir */
771    if (CVS_RENAME (dotdir, bakdir) < 0)
772	dir2_errno = errno;
773    /* mv modules.pag .#modules.pag */
774    if (CVS_RENAME (dotpag, bakpag) < 0)
775	pag2_errno = errno;
776    /* mv modules.db .#modules.db */
777    if (CVS_RENAME (dotdb, bakdb) < 0)
778	db2_errno = errno;
779
780    /* mv "temp".dir modules.dir */
781    if (CVS_RENAME (newdir, dotdir) < 0)
782	dir3_errno = errno;
783    /* mv "temp".pag modules.pag */
784    if (CVS_RENAME (newpag, dotpag) < 0)
785	pag3_errno = errno;
786    /* mv "temp".db modules.db */
787    if (CVS_RENAME (newdb, dotdb) < 0)
788	db3_errno = errno;
789
790    /* OK -- make my day */
791    SIG_endCrSect ();
792
793    /* I didn't want to call error() when we had signals blocked
794       (unnecessary?), but do it now.  */
795    if (dir1_errno && !existence_error (dir1_errno))
796	error (0, dir1_errno, "cannot remove %s", bakdir);
797    if (pag1_errno && !existence_error (pag1_errno))
798	error (0, pag1_errno, "cannot remove %s", bakpag);
799    if (db1_errno && !existence_error (db1_errno))
800	error (0, db1_errno, "cannot remove %s", bakdb);
801
802    if (dir2_errno && !existence_error (dir2_errno))
803	error (0, dir2_errno, "cannot remove %s", bakdir);
804    if (pag2_errno && !existence_error (pag2_errno))
805	error (0, pag2_errno, "cannot remove %s", bakpag);
806    if (db2_errno && !existence_error (db2_errno))
807	error (0, db2_errno, "cannot remove %s", bakdb);
808
809    if (dir3_errno && !existence_error (dir3_errno))
810	error (0, dir3_errno, "cannot remove %s", bakdir);
811    if (pag3_errno && !existence_error (pag3_errno))
812	error (0, pag3_errno, "cannot remove %s", bakpag);
813    if (db3_errno && !existence_error (db3_errno))
814	error (0, db3_errno, "cannot remove %s", bakdb);
815}
816
817#endif				/* !MY_NDBM */
818
819static void
820rename_rcsfile (temp, real)
821    char *temp;
822    char *real;
823{
824    char *bak;
825    struct stat statbuf;
826    char *rcs;
827
828    /* Set "x" bits if set in original. */
829    rcs = xmalloc (strlen (real) + sizeof (RCSEXT) + 10);
830    (void) sprintf (rcs, "%s%s", real, RCSEXT);
831    statbuf.st_mode = 0; /* in case rcs file doesn't exist, but it should... */
832    if (CVS_STAT (rcs, &statbuf) < 0
833	&& !existence_error (errno))
834	error (0, errno, "cannot stat %s", rcs);
835    free (rcs);
836
837    if (chmod (temp, 0444 | (statbuf.st_mode & 0111)) < 0)
838	error (0, errno, "warning: cannot chmod %s", temp);
839    bak = xmalloc (strlen (real) + sizeof (BAKPREFIX) + 10);
840    (void) sprintf (bak, "%s%s", BAKPREFIX, real);
841
842    /* rm .#loginfo */
843    if (unlink_file (bak) < 0
844	&& !existence_error (errno))
845	error (0, errno, "cannot remove %s", bak);
846
847    /* mv loginfo .#loginfo */
848    if (CVS_RENAME (real, bak) < 0
849	&& !existence_error (errno))
850	error (0, errno, "cannot rename %s to %s", real, bak);
851
852    /* mv "temp" loginfo */
853    if (CVS_RENAME (temp, real) < 0
854	&& !existence_error (errno))
855	error (0, errno, "cannot rename %s to %s", temp, real);
856
857    free (bak);
858}
859
860/*
861 * Walk PATH backwards to the root directory looking for the root of a
862 * repository.
863 */
864static char *
865in_repository (const char *path)
866{
867    char *cp = xstrdup (path);
868
869    for (;;)
870    {
871	if (isdir (cp))
872	{
873	    int foundit;
874	    char *adm = xmalloc (strlen(cp) + strlen(CVSROOTADM) + 2);
875	    sprintf (adm, "%s/%s", cp, CVSROOTADM);
876	    foundit = isdir (adm);
877	    free (adm);
878	    if (foundit) return cp;
879	}
880
881	/* If last_component() returns the empty string, then cp either
882	 * points at the system root or is the empty string itself.
883	 */
884	if (!*last_component (cp) || !strcmp (cp, ".")
885	    || last_component(cp) == cp)
886	    break;
887
888	cp[strlen(cp) - strlen(last_component(cp)) - 1] = '\0';
889    }
890
891    return NULL;
892}
893
894
895const char *const init_usage[] = {
896    "Usage: %s %s\n",
897    "(Specify the --help global option for a list of other help options)\n",
898    NULL
899};
900
901int
902init (argc, argv)
903    int argc;
904    char **argv;
905{
906    /* Name of CVSROOT directory.  */
907    char *adm;
908    /* Name of this administrative file.  */
909    char *info;
910    /* Name of ,v file for this administrative file.  */
911    char *info_v;
912    /* Exit status.  */
913    int err = 0;
914
915    char *root_dir;
916    const struct admin_file *fileptr;
917
918    assert (!server_active);
919
920    umask (cvsumask);
921
922    if (argc == -1 || argc > 1)
923	usage (init_usage);
924
925#ifdef CLIENT_SUPPORT
926    if (current_parsed_root->isremote)
927    {
928	start_server ();
929
930	ign_setup ();
931	send_init_command ();
932	return get_responses_and_close ();
933    }
934#endif /* CLIENT_SUPPORT */
935
936    root_dir = in_repository (current_parsed_root->directory);
937
938    if (root_dir && strcmp (root_dir, current_parsed_root->directory))
939	error (1, 0,
940	       "Cannot initialize repository under existing CVSROOT: `%s'",
941	       root_dir);
942    free (root_dir);
943
944    /* Note: we do *not* create parent directories as needed like the
945       old cvsinit.sh script did.  Few utilities do that, and a
946       non-existent parent directory is as likely to be a typo as something
947       which needs to be created.  */
948    mkdir_if_needed (current_parsed_root->directory);
949
950    adm = xmalloc (strlen (current_parsed_root->directory) + sizeof (CVSROOTADM) + 2);
951    sprintf (adm, "%s/%s", current_parsed_root->directory, CVSROOTADM);
952    mkdir_if_needed (adm);
953
954    /* This is needed because we pass "fileptr->filename" not "info"
955       to add_rcs_file below.  I think this would be easy to change,
956       thus nuking the need for CVS_CHDIR here, but I haven't looked
957       closely (e.g. see wrappers calls within add_rcs_file).  */
958    if ( CVS_CHDIR (adm) < 0)
959	error (1, errno, "cannot change to directory %s", adm);
960
961    /* Make Emptydir so it's there if we need it */
962    mkdir_if_needed (CVSNULLREPOS);
963
964    /* 80 is long enough for all the administrative file names, plus
965       "/" and so on.  */
966    info = xmalloc (strlen (adm) + 80);
967    info_v = xmalloc (strlen (adm) + 80);
968    for (fileptr = filelist; fileptr && fileptr->filename; ++fileptr)
969    {
970	if (fileptr->contents == NULL)
971	    continue;
972	strcpy (info, adm);
973	strcat (info, "/");
974	strcat (info, fileptr->filename);
975	strcpy (info_v, info);
976	strcat (info_v, RCSEXT);
977	if (isfile (info_v))
978	    /* We will check out this file in the mkmodules step.
979	       Nothing else is required.  */
980	    ;
981	else
982	{
983	    int retcode;
984
985	    if (!isfile (info))
986	    {
987		FILE *fp;
988		const char * const *p;
989
990		fp = open_file (info, "w");
991		for (p = fileptr->contents; *p != NULL; ++p)
992		    if (fputs (*p, fp) < 0)
993			error (1, errno, "cannot write %s", info);
994		if (fclose (fp) < 0)
995		    error (1, errno, "cannot close %s", info);
996	    }
997	    /* The message used to say " of " and fileptr->filename after
998	       "initial checkin" but I fail to see the point as we know what
999	       file it is from the name.  */
1000	    retcode = add_rcs_file ("initial checkin", info_v,
1001				    fileptr->filename, "1.1", NULL,
1002
1003				    /* No vendor branch.  */
1004				    NULL, NULL, 0, NULL,
1005
1006				    NULL, 0, NULL);
1007	    if (retcode != 0)
1008		/* add_rcs_file already printed an error message.  */
1009		err = 1;
1010	}
1011    }
1012
1013    /* Turn on history logging by default.  The user can remove the file
1014       to disable it.  */
1015    strcpy (info, adm);
1016    strcat (info, "/");
1017    strcat (info, CVSROOTADM_HISTORY);
1018    if (!isfile (info))
1019    {
1020	FILE *fp;
1021
1022	fp = open_file (info, "w");
1023	if (fclose (fp) < 0)
1024	    error (1, errno, "cannot close %s", info);
1025
1026        /* Make the new history file world-writeable, since every CVS
1027           user will need to be able to write to it.  We use chmod()
1028           because xchmod() is too shy. */
1029        chmod (info, 0666);
1030    }
1031
1032    /* Make an empty val-tags file to prevent problems creating it later.  */
1033    strcpy (info, adm);
1034    strcat (info, "/");
1035    strcat (info, CVSROOTADM_VALTAGS);
1036    if (!isfile (info))
1037    {
1038	FILE *fp;
1039
1040	fp = open_file (info, "w");
1041	if (fclose (fp) < 0)
1042	    error (1, errno, "cannot close %s", info);
1043
1044        /* Make the new val-tags file world-writeable, since every CVS
1045           user will need to be able to write to it.  We use chmod()
1046           because xchmod() is too shy. */
1047        chmod (info, 0666);
1048    }
1049
1050    free (info);
1051    free (info_v);
1052
1053    mkmodules (adm);
1054
1055    free (adm);
1056    return err;
1057}
1058