1 /*
2  * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
3  *
4  * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
5  *                                  and others.
6  *
7  * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8  * Portions Copyright (C) 1989-1992, Brian Berliner
9  *
10  * You may distribute under the terms of the GNU General Public License as
11  * specified in the README file that comes with the CVS kit.  */
12 
13 #include "cvs.h"
14 #include "getline.h"
15 #include "history.h"
16 #include "save-cwd.h"
17 
18 __RCSID("$MirOS: src/gnu/usr.bin/cvs/src/mkmodules.c,v 1.12 2011/06/10 22:08:46 tg Exp $");
19 
20 #ifndef DBLKSIZ
21 #define	DBLKSIZ	4096			/* since GNU ndbm doesn't define it */
22 #endif
23 
24 static int checkout_file (char *file, char *temp);
25 static char *make_tempfile (void);
26 static void rename_rcsfile (char *temp, char *real);
27 
28 #ifndef MY_NDBM
29 static void rename_dbmfile (char *temp);
30 static void write_dbmfile (char *temp);
31 #endif				/* !MY_NDBM */
32 
33 /* Structure which describes an administrative file.  */
34 struct admin_file {
35    /* Name of the file, within the CVSROOT directory.  */
36    char *filename;
37 
38    /* This is a one line description of what the file is for.  It is not
39       currently used, although one wonders whether it should be, somehow.
40       If NULL, then don't process this file in mkmodules (FIXME?: a bit of
41       a kludge; probably should replace this with a flags field).  */
42    char *errormsg;
43 
44    /* Contents which the file should have in a new repository.  To avoid
45       problems with brain-dead compilers which choke on long string constants,
46       this is a pointer to an array of char * terminated by NULL--each of
47       the strings is concatenated.
48 
49       If this field is NULL, the file is not created in a new
50       repository, but it can be added with "cvs add" (just as if one
51       had created the repository with a version of CVS which didn't
52       know about the file) and the checked-out copy will be updated
53       without having to add it to checkoutlist.  */
54    const char * const *contents;
55 };
56 
57 static const char *const loginfo_contents[] = {
58     "# The \"loginfo\" file controls where \"cvs commit\" log information is\n",
59     "# sent. The first entry on a line is a regular expression which must\n",
60     "# match the directory that the change is being made to, relative to the\n",
61     "# $CVSROOT.  If a match is found, then the remainder of the line is a\n",
62     "# filter program that should expect log information on its standard input.\n",
63     "#\n",
64     "# If the repository name does not match any of the regular expressions in this\n",
65     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
66     "#\n",
67     "# If the name ALL appears as a regular expression it is always used\n",
68     "# in addition to the first matching regex or DEFAULT.\n",
69     "#\n",
70     "# If any format strings are present in the filter, they will be replaced\n",
71     "# as follows:\n",
72     "#    %c = canonical name of the command being executed\n",
73     "#    %I = unique (randomly generated) commit ID\n",
74 #ifdef PROXY_SUPPORT
75     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
76 #endif
77     "#    %p = path relative to repository\n",
78     "#    %r = repository (path portion of $CVSROOT)\n",
79     "#    %{sVv} = attribute list = file name, old version number (pre-checkin),\n",
80     "#           new version number (post-checkin).  When either old or new revision\n",
81     "#           is unknown, doesn't exist, or isn't applicable, the string \"NONE\"\n",
82     "#           will be placed on the command line instead.\n",
83     "#\n",
84     "# Note that %{sVv} is a list operator and not all elements are necessary.\n",
85     "# Thus %{sv} is a legal format string, but will only be replaced with\n",
86     "# file name and new revision.\n",
87     "# It also generates multiple arguments for each file being operated upon.\n",
88     "# That is, if two files, file1 & file2, are being committed from 1.1 to\n",
89     "# version 1.1.2.1 and from 1.1.2.2 to 1.1.2.3, respectively, %{sVv} will\n",
90     "# generate the following six arguments in this order:\n",
91     "# file1, 1.1, 1.1.2.1, file2, 1.1.2.2, 1.1.2.3.\n",
92     "#\n",
93     "# For example:\n",
94     "#DEFAULT (echo \"\"; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
95     "# or\n",
96     "#DEFAULT (echo \"\"; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog\n",
97     NULL
98 };
99 
100 static const char *const rcsinfo_contents[] = {
101     "# The \"rcsinfo\" file is used to control templates with which the editor\n",
102     "# is invoked on commit and import.\n",
103     "#\n",
104     "# The first entry on a line is a regular expression which is tested\n",
105     "# against the directory that the change is being made to, relative to the\n",
106     "# $CVSROOT.  For the first match that is found, then the remainder of the\n",
107     "# line is the name of the file that contains the template.\n",
108     "#\n",
109     "# If the repository name does not match any of the regular expressions in this\n",
110     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
111     "#\n",
112     "# If the name \"ALL\" appears as a regular expression it is always used\n",
113     "# in addition to the first matching regex or \"DEFAULT\".\n",
114     NULL
115 };
116 
117 
118 
119 static const char *const verifymsg_contents[] = {
120     "# The \"verifymsg\" file is used to allow verification of logging\n",
121     "# information.  It works best when a template (as specified in the\n",
122     "# rcsinfo file) is provided for the logging procedure.  Given a\n",
123     "# template with locations for, a bug-id number, a list of people who\n",
124     "# reviewed the code before it can be checked in, and an external\n",
125     "# process to catalog the differences that were code reviewed, the\n",
126     "# following test can be applied to the code:\n",
127     "#\n",
128     "#   Making sure that the entered bug-id number is correct.\n",
129     "#   Validating that the code that was reviewed is indeed the code being\n",
130     "#       checked in (using the bug-id number or a separate review\n",
131     "#       number to identify this particular code set.).\n",
132     "#\n",
133     "# If any of the above test failed, then the commit would be aborted.\n",
134     "#\n",
135     "# Format strings present in the filter will be replaced as follows:\n",
136     "#    %c = canonical name of the command being executed\n",
137     "#    %I = unique (randomly generated) commit ID\n",
138 #ifdef PROXY_SUPPORT
139     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
140 #endif
141     "#    %p = path relative to repository\n",
142     "#    %r = repository (path portion of $CVSROOT)\n",
143     "#    %l = name of log file to be verified.\n",
144     "#\n",
145     "# If no format strings are present in the filter, a default \" %l\" will\n",
146     "# be appended to the filter, but this usage is deprecated.\n",
147     "#\n",
148     "# Actions such as mailing a copy of the report to each reviewer are\n",
149     "# better handled by an entry in the loginfo file.\n",
150     "#\n",
151     "# One thing that should be noted is the the ALL keyword is not\n",
152     "# supported.  There can be only one entry that matches a given\n",
153     "# repository.\n",
154     NULL
155 };
156 
157 static const char *const commitinfo_contents[] = {
158     "# The \"commitinfo\" file is used to control pre-commit checks.\n",
159     "# The filter on the right is invoked with the repository and a list \n",
160     "# of files to check.  A non-zero exit of the filter program will \n",
161     "# cause the commit to be aborted.\n",
162     "#\n",
163     "# The first entry on a line is a regular expression which is tested\n",
164     "# against the directory that the change is being committed to, relative\n",
165     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
166     "# of the line is the name of the filter to run.\n",
167     "#\n",
168     "# Format strings present in the filter will be replaced as follows:\n",
169     "#    %c = canonical name of the command being executed\n",
170     "#    %I = unique (randomly generated) commit ID\n",
171 #ifdef PROXY_SUPPORT
172     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
173 #endif
174     "#    %p = path relative to repository\n",
175     "#    %r = repository (path portion of $CVSROOT)\n",
176     "#    %{s} = file name, file name, ...\n",
177     "#\n",
178     "# If no format strings are present in the filter string, a default of\n",
179     "# \" %r %s\" will be appended to the filter string, but this usage is\n",
180     "# deprecated.\n",
181     "#\n",
182     "# If the repository name does not match any of the regular expressions in this\n",
183     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
184     "#\n",
185     "# If the name \"ALL\" appears as a regular expression it is always used\n",
186     "# in addition to the first matching regex or \"DEFAULT\".\n",
187     NULL
188 };
189 
190 static const char *const taginfo_contents[] = {
191     "# The \"taginfo\" file is used to control pre-tag checks.\n",
192     "# The filter on the right is invoked with the following arguments\n",
193     "# if no format strings are present:\n",
194     "#\n",
195     "# $1 -- tagname\n",
196     "# $2 -- operation \"add\" for tag, \"mov\" for tag -F, and \"del\" for tag -d\n",
197     "# $3 -- tagtype \"?\" on delete, \"T\" for branch, \"N\" for static\n",
198     "# $4 -- repository\n",
199     "# $5->  file revision [file revision ...]\n",
200     "#\n",
201     "# If any format strings are present in the filter, they will be replaced\n",
202     "# as follows:\n",
203     "#    %b = branch mode = \"?\" (delete ops - unknown) | \"T\" (branch)\n",
204     "#                     | \"N\" (not branch)\n",
205     "#    %o = operation = \"add\" | \"mov\" | \"del\"\n",
206     "#    %c = canonical name of the command being executed\n",
207     "#    %I = unique (randomly generated) commit ID\n",
208 #ifdef PROXY_SUPPORT
209     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
210 #endif
211     "#    %p = path relative to repository\n",
212     "#    %r = repository (path portion of $CVSROOT)\n",
213     "#    %t = tagname\n",
214     "#    %{sVv} = attribute list = file name, old version tag will be deleted\n",
215     "#             from, new version tag will be added to (or deleted from, but\n",
216     "#             this feature is deprecated.  When either old or new revision is\n",
217     "#             unknown, doesn't exist, or isn't applicable, the string \"NONE\"\n",
218     "#             will be placed on the command line.\n",
219     "#\n",
220     "# Note that %{sVv} is a list operator and not all elements are necessary.\n",
221     "# Thus %{sV} is a legal format string, but will only be replaced with file\n",
222     "# name and old revision. it also generates multiple arguments for each file\n",
223     "# being operated upon.  i.e. if two files, file1 & file2, are having a tag\n",
224     "# moved from version 1.1 to version 1.1.2.9, %{sVv} will generate the\n",
225     "# following six arguments in this order:\n",
226     "# file1, 1.1, 1.1.2.9, file2, 1.1, 1.1.2.9.\n",
227     "#\n",
228     "# A non-zero exit of the filter program will cause the tag to be aborted.\n",
229     "#\n",
230     "# The first entry on a line is a regular expression which is tested\n",
231     "# against the directory that the change is being committed to, relative\n",
232     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
233     "# of the line is the name of the filter to run.\n",
234     "#\n",
235     "# If the repository name does not match any of the regular expressions in this\n",
236     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
237     "#\n",
238     "# If the name \"ALL\" appears as a regular expression it is always used\n",
239     "# in addition to the first matching regex or \"DEFAULT\".\n",
240     NULL
241 };
242 
243 static const char *const preproxy_contents[] = {
244     "# The \"preproxy\" file is called form the secondary server as soon as\n",
245     "# the secondary server determines that it will be proxying a write\n",
246     "# command to a primary server and immediately before it opens a\n",
247     "# connection to the primary server.  This script might, for example, be\n",
248     "# used to launch a dial up or VPN connection to the primary server's\n",
249     "# network.\n",
250     "#\n",
251     "# If any format strings are present in the filter, they will be replaced\n",
252     "# as follows:\n",
253     "#    %c = canonical name of the command being executed\n",
254     "#    %I = unique (randomly generated) commit ID\n",
255 #ifdef PROXY_SUPPORT
256     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
257 #endif
258     "#    %p = path relative to repository (currently always \".\")\n",
259     "#    %r = repository (path portion of $CVSROOT)\n",
260     "#\n",
261     "# The first entry on a line is a regular expression which is tested\n",
262     "# against the directory that the change is being committed to, relative\n",
263     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
264     "# of the line is the name of the filter to run.\n",
265     "#\n",
266     "# If the repository name does not match any of the regular expressions in this\n",
267     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
268     "#\n",
269     "# If the name \"ALL\" appears as a regular expression it is always used\n",
270     "# in addition to the first matching regex or \"DEFAULT\".\n",
271     NULL
272 };
273 
274 static const char *const postadmin_contents[] = {
275     "# The \"postadmin\" file is called after the \"admin\" command finishes\n",
276     "# processing a directory.\n",
277     "#\n",
278     "# If any format strings are present in the filter, they will be replaced\n",
279     "# as follows:\n",
280     "#    %c = canonical name of the command being executed\n",
281     "#    %I = unique (randomly generated) commit ID\n",
282 #ifdef PROXY_SUPPORT
283     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
284 #endif
285     "#    %p = path relative to repository\n",
286     "#    %r = repository (path portion of $CVSROOT)\n",
287     "#\n",
288     "# The first entry on a line is a regular expression which is tested\n",
289     "# against the directory that the change is being committed to, relative\n",
290     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
291     "# of the line is the name of the filter to run.\n",
292     "#\n",
293     "# If the repository name does not match any of the regular expressions in this\n",
294     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
295     "#\n",
296     "# If the name \"ALL\" appears as a regular expression it is always used\n",
297     "# in addition to the first matching regex or \"DEFAULT\".\n",
298     NULL
299 };
300 
301 static const char *const postproxy_contents[] = {
302     "# The \"postproxy\" file is called from a secondary server as soon as\n",
303     "# the secondary server closes its connection to the primary server.\n",
304     "# This script might, for example, be used to shut down a dial up\n",
305     "# or VPN connection to the primary server's network.\n",
306     "#\n",
307     "# If any format strings are present in the filter, they will be replaced\n",
308     "# as follows:\n",
309     "#    %c = canonical name of the command being executed\n",
310     "#    %I = unique (randomly generated) commit ID\n",
311 #ifdef PROXY_SUPPORT
312     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
313 #endif
314     "#    %p = path relative to repository (currently always \".\")\n",
315     "#    %r = repository (path portion of $CVSROOT)\n",
316     "#\n",
317     "# The first entry on a line is a regular expression which is tested\n",
318     "# against the directory that the change is being committed to, relative\n",
319     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
320     "# of the line is the name of the filter to run.\n",
321     "#\n",
322     "# If the repository name does not match any of the regular expressions in this\n",
323     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
324     "#\n",
325     "# If the name \"ALL\" appears as a regular expression it is always used\n",
326     "# in addition to the first matching regex or \"DEFAULT\".\n",
327     NULL
328 };
329 
330 static const char *const posttag_contents[] = {
331     "# The \"posttag\" file is called after the \"tag\" command finishes\n",
332     "# processing a directory.\n",
333     "#\n",
334     "# If any format strings are present in the filter, they will be replaced\n",
335     "# as follows:\n",
336     "#    %b = branch mode = \"?\" (delete ops - unknown) | \"T\" (branch)\n",
337     "#                     | \"N\" (not branch)\n",
338     "#    %o = operation = \"add\" | \"mov\" | \"del\"\n",
339     "#    %c = canonical name of the command being executed\n",
340     "#    %I = unique (randomly generated) commit ID\n",
341 #ifdef PROXY_SUPPORT
342     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
343 #endif
344     "#    %p = path relative to repository\n",
345     "#    %r = repository (path portion of $CVSROOT)\n",
346     "#    %t = tagname\n",
347     "#    %{sVv} = attribute list = file name, old version tag will be deleted\n",
348     "#             from, new version tag will be added to (or deleted from, but\n",
349     "#             this feature is deprecated.  When either old or new revision is\n",
350     "#             unknown, doesn't exist, or isn't applicable, the string \"NONE\"\n",
351     "#             will be placed on the command line.\n",
352     "#\n",
353     "# Note that %{sVv} is a list operator and not all elements are necessary.\n",
354     "# Thus %{sV} is a legal format string, but will only be replaced with file\n",
355     "# name and old revision. it also generates multiple arguments for each file\n",
356     "# being operated upon.  i.e. if two files, file1 & file2, are having a tag\n",
357     "# moved from version 1.1 to version 1.1.2.9, %{sVv} will generate the\n",
358     "# following six arguments in this order:\n",
359     "# file1, 1.1, 1.1.2.9, file2, 1.1, 1.1.2.9.\n",
360     "#\n",
361     "# The first entry on a line is a regular expression which is tested\n",
362     "# against the directory that the change is being committed to, relative\n",
363     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
364     "# of the line is the name of the filter to run.\n",
365     "#\n",
366     "# If the repository name does not match any of the regular expressions in this\n",
367     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
368     "#\n",
369     "# If the name \"ALL\" appears as a regular expression it is always used\n",
370     "# in addition to the first matching regex or \"DEFAULT\".\n",
371     NULL
372 };
373 
374 static const char *const postwatch_contents[] = {
375     "# The \"postwatch\" file is called after any command finishes writing new\n",
376     "# file attibute (watch/edit) information in a directory.\n",
377     "#\n",
378     "# If any format strings are present in the filter, they will be replaced\n",
379     "# as follows:\n",
380     "#    %c = canonical name of the command being executed\n",
381     "#    %I = unique (randomly generated) commit ID\n",
382 #ifdef PROXY_SUPPORT
383     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
384 #endif
385     "#    %p = path relative to repository\n",
386     "#    %r = repository (path portion of $CVSROOT)\n",
387     "#\n",
388     "# The first entry on a line is a regular expression which is tested\n",
389     "# against the directory that the change is being committed to, relative\n",
390     "# to the $CVSROOT.  For the first match that is found, then the remainder\n",
391     "# of the line is the name of the filter to run.\n",
392     "#\n",
393     "# If the repository name does not match any of the regular expressions in this\n",
394     "# file, the \"DEFAULT\" line is used, if it is specified.\n",
395     "#\n",
396     "# If the name \"ALL\" appears as a regular expression it is always used\n",
397     "# in addition to the first matching regex or \"DEFAULT\".\n",
398     NULL
399 };
400 
401 static const char *const checkoutlist_contents[] = {
402     "# The \"checkoutlist\" file is used to support additional version controlled\n",
403     "# administrative files in $CVSROOT/CVSROOT, such as template files.\n",
404     "#\n",
405     "# The first entry on a line is a filename which will be checked out from\n",
406     "# the corresponding RCS file in the $CVSROOT/CVSROOT directory.\n",
407     "# The remainder of the line is an error message to use if the file cannot\n",
408     "# be checked out.\n",
409     "#\n",
410     "# File format:\n",
411     "#\n",
412     "#	[<whitespace>]<filename>[<whitespace><error message>]<end-of-line>\n",
413     "#\n",
414     "# comment lines begin with '#'\n",
415     NULL
416 };
417 
418 static const char *const cvswrappers_contents[] = {
419     "# This file affects handling of files based on their names.\n",
420     "#\n",
421 #if 0    /* see comments in wrap_add in wrapper.c */
422     "# The -t/-f options allow one to treat directories of files\n",
423     "# as a single file, or to transform a file in other ways on\n",
424     "# its way in and out of CVS.\n",
425     "#\n",
426 #endif
427     "# The -m option specifies whether CVS attempts to merge files.\n",
428     "#\n",
429     "# The -k option specifies keyword expansion (e.g. -kb for binary).\n",
430     "#\n",
431     "# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)\n",
432     "#\n",
433     "#  wildcard	[option value][option value]...\n",
434     "#\n",
435     "#  where option is one of\n",
436     "#  -f		from cvs filter		value: path to filter\n",
437     "#  -t		to cvs filter		value: path to filter\n",
438     "#  -m		update methodology	value: MERGE or COPY\n",
439     "#  -k		expansion mode		value: b, o, kkv, &c\n",
440     "#\n",
441     "#  and value is a single-quote delimited value.\n",
442     "# For example:\n",
443     "#*.gif -k 'b'\n",
444     NULL
445 };
446 
447 static const char *const notify_contents[] = {
448     "# The \"notify\" file controls where notifications from watches set by\n",
449     "# \"cvs watch add\" or \"cvs edit\" are sent.  The first entry on a line is\n",
450     "# a regular expression which is tested against the directory that the\n",
451     "# change is being made to, relative to the $CVSROOT.  If it matches,\n",
452     "# then the remainder of the line is a filter program that should contain\n",
453     "# one occurrence of %s for the user to notify, and information on its\n",
454     "# standard input.\n",
455     "#\n",
456     "# \"ALL\" or \"DEFAULT\" can be used in place of the regular expression.\n",
457     "#\n",
458     "# format strings are replaceed as follows:\n",
459     "#    %c = canonical name of the command being executed\n",
460     "#    %I = unique (randomly generated) commit ID\n",
461 #ifdef PROXY_SUPPORT
462     "#    %R = the name of the referrer, if any, otherwise the value NONE\n",
463 #endif
464     "#    %p = path relative to repository\n",
465     "#    %r = repository (path portion of $CVSROOT)\n",
466     "#    %s = user to notify\n",
467     "#\n",
468     "# For example:\n",
469     "#ALL (echo Committed to %r/%p; cat) |mail %s -s \"CVS notification\"\n",
470     NULL
471 };
472 
473 static const char *const modules_contents[] = {
474     "# Three different line formats are valid:\n",
475     "#	key	-a    aliases...\n",
476     "#	key [options] directory\n",
477     "#	key [options] directory files...\n",
478     "#\n",
479     "# Where \"options\" are composed of:\n",
480     "#	-o prog		Run \"prog\" on \"cvs checkout\" of module.\n",
481     "#	-e prog		Run \"prog\" on \"cvs export\" of module.\n",
482     "#	-s status	Assign a status to the module.\n",
483     "#	-t prog		Run \"prog\" on \"cvs rtag\" of module.\n",
484     "#	-d dir		Place module in directory \"dir\" instead of module name.\n",
485     "#	-l		Top-level directory only -- do not recurse.\n",
486     "#\n",
487     "# NOTE:  If you change any of the \"Run\" options above, you'll have to\n",
488     "# release and re-checkout any working directories of these modules.\n",
489     "#\n",
490     "# And \"directory\" is a path to a directory relative to $CVSROOT.\n",
491     "#\n",
492     "# The \"-a\" option specifies an alias.  An alias is interpreted as if\n",
493     "# everything on the right of the \"-a\" had been typed on the command line.\n",
494     "#\n",
495     "# You can encode a module within a module by using the special '&'\n",
496     "# character to interpose another module into the current module.  This\n",
497     "# can be useful for creating a module that consists of many directories\n",
498     "# spread out over the entire source repository.\n",
499     NULL
500 };
501 
502 static const char *const config_contents[] = {
503     "# Set 'SystemAuth' to 'no' if pserver shouldn't check system users/passwords.\n",
504     "#SystemAuth=no\n",
505     "\n",
506     "# Set 'LocalKeyword' to specify a local alias for a standard keyword.\n",
507     "#LocalKeyword=MYCVS=CVSHeader\n",
508     "\n",
509     "# Set 'KeywordExpand' to 'i' followed by a list of keywords to expand or\n",
510     "# 'e' followed by a list of keywords to not expand.\n"
511     "#KeywordExpand=iMYCVS,Name,Date,Mdocdate\n",
512     "#KeywordExpand=eCVSHeader\n",
513     "\n",
514 #ifdef PRESERVE_PERMISSIONS_SUPPORT
515     "# Set 'PreservePermissions' to 'yes' to save file status information\n",
516     "# in the repository.\n",
517     "#PreservePermissions=no\n",
518     "\n",
519 #endif
520     "# Set 'TopLevelAdmin' to 'yes' to create a CVS directory at the top\n",
521     "# level of the new working directory when using the 'cvs checkout'\n",
522     "# command.\n",
523     "#TopLevelAdmin=no\n",
524     "\n",
525     "# Put CVS lock files in this directory rather than directly in the repository.\n",
526     "#LockDir=/var/lock/cvs\n",
527     "\n",
528     "# Set 'LogHistory' to 'all' or '" ALL_HISTORY_REC_TYPES "' to log all transactions to the\n",
529     "# history file, or a subset as needed (ie 'TMAR' logs all write operations)\n",
530     "#LogHistory=" ALL_HISTORY_REC_TYPES "\n",
531     "\n",
532     "# Set 'RereadLogAfterVerify' to 'always' (the default) to allow the verifymsg\n",
533     "# script to change the log message.  Set it to 'stat' to force CVS to verify\n",
534     "# that the file has changed before reading it (this can take up to an extra\n",
535     "# second per directory being committed, so it is not recommended for large\n",
536     "# repositories.  Set it to 'never' (the previous CVS behavior) to prevent\n",
537     "# verifymsg scripts from changing the log message.\n",
538     "#RereadLogAfterVerify=always\n",
539     "\n",
540     "# Set 'UserAdminOptions' to the list of 'cvs admin' commands (options)\n",
541     "# that users not in the '_cvsadmin' group are allowed to run.  This\n",
542     "# defaults to 'k', or only allowing the changing of the default\n",
543     "# keyword expansion mode for files for users not in the '_cvsadmin' group.\n",
544     "# This value is ignored if the '_cvsadmin' group does not exist.\n",
545     "#\n",
546     "# The following string would enable all 'cvs admin' commands for all\n",
547     "# users:\n",
548     "#UserAdminOptions=aAbceIklLmnNostuU\n",
549 #ifdef SUPPORT_OLD_INFO_FMT_STRINGS
550     "\n",
551     "# Set 'UseNewInfoFmtStrings' to 'no' if you must support a legacy system by\n",
552     "# enabling the deprecated old style info file command line format strings.\n",
553     "# Be warned that these strings could be disabled in any new version of CVS.\n",
554     "UseNewInfoFmtStrings=yes\n",
555 #endif /* SUPPORT_OLD_INFO_FMT_STRINGS */
556     "\n",
557     "# Set 'ImportNewFilesToVendorBranchOnly' to 'yes' if you wish to force\n",
558     "# every 'cvs import' command to behave as if the '-X' flag was\n",
559     "# specified.\n",
560     "#ImportNewFilesToVendorBranchOnly=no\n",
561 #ifdef PROXY_SUPPORT
562     "\n",
563     "# Set 'PrimaryServer' to the CVSROOT to the primary, or write, server when\n",
564     "# establishing one or more read-only mirrors which serve as proxies for\n",
565     "# the write server in write mode or redirect the client to the primary for\n",
566     "# write requests.\n",
567     "#\n",
568     "# For example:\n",
569     "#\n",
570     "#   PrimaryServer=:fork:localhost/cvsroot\n",
571     "\n",
572     "# Set 'MaxProxyBufferSize' to the the maximum allowable secondary\n",
573     "# buffer memory cache size before the buffer begins being stored to disk, in\n",
574     "# bytes.  Must be a positive integer but may end in 'K', 'M', 'G', or 'T' (for\n",
575     "# Kibi, Mebi, Gibi, & Tebi, respectively).  If an otherwise valid number you\n",
576     "# specify is greater than the SIZE_MAX defined by your system's C compiler,\n",
577     "# then it will be resolved to SIZE_MAX without a warning.  Defaults to 8M (8\n",
578     "# Mebibytes). The 'i' from 'Ki', 'Mi', etc. is omitted.\n",
579     "#\n",
580     "# High values for MaxProxyBufferSize may speed up a secondary server\n",
581     "# with old hardware and a lot of available memory but can actually slow a\n",
582     "# modern system down slightly.\n",
583     "#\n",
584     "# For example:\n",
585     "#\n",
586     "#   MaxProxyBufferSize=1G\n",
587 #endif /* PROXY_SUPPORT */
588     "\n",
589     "# Set 'MaxCommentLeaderLength' to the maximum length permitted for the\n",
590     "# automagically determined comment leader used when expanding the Log\n",
591     "# keyword, in bytes.  CVS's behavior when the automagically determined\n",
592     "# comment leader exceeds this length is dependent on the value of\n",
593     "# 'UseArchiveCommentLeader' set in this file.  'unlimited' is a valid\n",
594     "# setting for this value.  Defaults to 20 bytes.\n",
595     "#\n",
596     "# For example:\n",
597     "#\n",
598     "#   MaxCommentLeaderLength=20\n",
599     "\n",
600     "# Set 'UseArchiveCommentLeader' to 'yes' to cause CVS to fall back on\n",
601     "# the comment leader set in the RCS archive file, if any, when the\n",
602     "# automagically determined comment leader exceeds 'MaxCommentLeaderLength'\n",
603     "# bytes.  If 'UseArchiveCommentLeader' is not set and a comment leader\n",
604     "# greater than 'MaxCommentLeaderLength' is calculated, the Log keyword\n",
605     "# being examined will not be expanded.  Defaults to 'no'.\n",
606     "#\n",
607     "# For example:\n",
608     "#\n",
609     "#   UseArchiveCommentLeader=no\n",
610     NULL
611 };
612 
613 static const struct admin_file filelist[] = {
614     {CVSROOTADM_CHECKOUTLIST,
615 	"a %s file can specify extra CVSROOT files to auto-checkout",
616 	checkoutlist_contents},
617     {CVSROOTADM_COMMITINFO,
618 	"a %s file can be used to configure 'cvs commit' checking",
619 	commitinfo_contents},
620     {CVSROOTADM_IGNORE,
621 	"a %s file can be used to specify files to ignore",
622 	NULL},
623     {CVSROOTADM_LOGINFO,
624 	"no logging of 'cvs commit' messages is done without a %s file",
625 	&loginfo_contents[0]},
626     {CVSROOTADM_MODULES,
627 	/* modules is special-cased in mkmodules.  */
628 	NULL,
629 	modules_contents},
630     {CVSROOTADM_NOTIFY,
631 	"a %s file can be used to specify where notifications go",
632 	notify_contents},
633     {CVSROOTADM_POSTADMIN,
634 	"a %s file can be used to configure 'cvs admin' logging",
635 	postadmin_contents},
636     {CVSROOTADM_POSTPROXY,
637 	"a %s file can be used to close or log connections to a primary server",
638 	postproxy_contents},
639     {CVSROOTADM_POSTTAG,
640 	"a %s file can be used to configure 'cvs tag' logging",
641 	posttag_contents},
642     {CVSROOTADM_POSTWATCH,
643 	"a %s file can be used to configure 'cvs watch' logging",
644 	postwatch_contents},
645     {CVSROOTADM_PREPROXY,
646 	"a %s file can be used to open or log connections to a primary server",
647 	preproxy_contents},
648     {CVSROOTADM_RCSINFO,
649 	"a %s file can be used to configure 'cvs commit' templates",
650 	rcsinfo_contents},
651     {CVSROOTADM_READERS,
652 	"a %s file specifies read-only users",
653 	NULL},
654     {CVSROOTADM_TAGINFO,
655 	"a %s file can be used to configure 'cvs tag' checking",
656 	taginfo_contents},
657     {CVSROOTADM_VERIFYMSG,
658 	"a %s file can be used to validate log messages",
659 	verifymsg_contents},
660     {CVSROOTADM_WRAPPER,
661 	"a %s file can be used to specify files to treat as wrappers",
662 	cvswrappers_contents},
663     {CVSROOTADM_WRITERS,
664 	"a %s file specifies read/write users",
665 	NULL},
666 
667     /* Some have suggested listing CVSROOTADM_PASSWD here too.  This
668        would mean that CVS commands which operate on the
669        CVSROOTADM_PASSWD file would transmit hashed passwords over the
670        net.  This might seem to be no big deal, as pserver normally
671        transmits cleartext passwords, but the difference is that
672        CVSROOTADM_PASSWD contains *all* passwords, not just the ones
673        currently being used.  For example, it could be too easy to
674        accidentally give someone readonly access to CVSROOTADM_PASSWD
675        (e.g. via anonymous CVS or cvsweb), and then if there are any
676        guessable passwords for read/write access (usually there will be)
677        they get read/write access.
678 
679        Another worry is the implications of storing old passwords--if
680        someone used a password in the past they might be using it
681        elsewhere, using a similar password, etc, and so saving old
682        passwords, even hashed, is probably not a good idea.  */
683 
684     {CVSROOTADM_CONFIG,
685 	 "a %s file configures various behaviors",
686 	 config_contents},
687     {NULL, NULL, NULL}
688 };
689 
690 /* Rebuild the checked out administrative files in directory DIR.  */
691 int
mkmodules(char * dir)692 mkmodules (char *dir)
693 {
694     struct saved_cwd cwd;
695     char *temp;
696     char *cp, *last, *fname;
697 #ifdef MY_NDBM
698     DBM *db;
699 #endif
700     FILE *fp;
701     char *line = NULL;
702     size_t line_allocated = 0;
703     const struct admin_file *fileptr;
704 
705     if (noexec)
706 	return 0;
707 
708     if (save_cwd (&cwd))
709 	error (1, errno, "Failed to save current directory.");
710 
711     if (CVS_CHDIR (dir) < 0)
712 	error (1, errno, "cannot chdir to %s", dir);
713 
714     /*
715      * First, do the work necessary to update the "modules" database.
716      */
717     temp = make_tempfile ();
718     switch (checkout_file (CVSROOTADM_MODULES, temp))
719     {
720 
721 	case 0:			/* everything ok */
722 #ifdef MY_NDBM
723 	    /* open it, to generate any duplicate errors */
724 	    if ((db = dbm_open (temp, O_RDONLY, 0666)) != NULL)
725 		dbm_close (db);
726 #else
727 	    write_dbmfile (temp);
728 	    rename_dbmfile (temp);
729 #endif
730 	    rename_rcsfile (temp, CVSROOTADM_MODULES);
731 	    break;
732 
733 	default:
734 	    error (0, 0,
735 		"'cvs checkout' is less functional without a %s file",
736 		CVSROOTADM_MODULES);
737 	    break;
738     }					/* switch on checkout_file() */
739 
740     if (unlink_file (temp) < 0
741 	&& !existence_error (errno))
742 	error (0, errno, "cannot remove %s", temp);
743     free (temp);
744 
745     /* Checkout the files that need it in CVSROOT dir */
746     for (fileptr = filelist; fileptr && fileptr->filename; fileptr++) {
747 	if (fileptr->errormsg == NULL)
748 	    continue;
749 	temp = make_tempfile ();
750 	if (checkout_file (fileptr->filename, temp) == 0)
751 	    rename_rcsfile (temp, fileptr->filename);
752 	/* else
753 	 *   If there was some problem other than the file not existing,
754 	 *   checkout_file already printed a real error message.  If the
755 	 *   file does not exist, it is harmless--it probably just means
756 	 *   that the repository was created with an old version of CVS
757 	 *   which didn't have so many files in CVSROOT.
758 	 */
759 
760 	if (unlink_file (temp) < 0
761 	    && !existence_error (errno))
762 	    error (0, errno, "cannot remove %s", temp);
763 	free (temp);
764     }
765 
766     fp = CVS_FOPEN (CVSROOTADM_CHECKOUTLIST, "r");
767     if (fp)
768     {
769 	/*
770 	 * File format:
771 	 *  [<whitespace>]<filename>[<whitespace><error message>]<end-of-line>
772 	 *
773 	 * comment lines begin with '#'
774 	 */
775 	while (getline (&line, &line_allocated, fp) >= 0)
776 	{
777 	    /* skip lines starting with # */
778 	    if (line[0] == '#')
779 		continue;
780 
781 	    if ((last = strrchr (line, '\n')) != NULL)
782 		*last = '\0';			/* strip the newline */
783 
784 	    /* Skip leading white space. */
785 	    for (fname = line;
786 		 *fname && isspace ((unsigned char) *fname);
787 		 fname++)
788 		;
789 
790 	    /* Find end of filename. */
791 	    for (cp = fname; *cp && !isspace ((unsigned char) *cp); cp++)
792 		;
793 	    *cp = '\0';
794 
795 	    temp = make_tempfile ();
796 	    if (checkout_file (fname, temp) == 0)
797 	    {
798 		rename_rcsfile (temp, fname);
799 	    }
800 	    else
801 	    {
802 		/* Skip leading white space before the error message.  */
803 		for (cp++;
804 		     cp < last && *cp && isspace ((unsigned char) *cp);
805 		     cp++)
806 		    ;
807 		if (cp < last && *cp)
808 		    error (0, 0, "%s", cp);
809 	    }
810 	    if (unlink_file (temp) < 0
811 		&& !existence_error (errno))
812 		error (0, errno, "cannot remove %s", temp);
813 	    free (temp);
814 	}
815 	if (line)
816 	    free (line);
817 	if (ferror (fp))
818 	    error (0, errno, "cannot read %s", CVSROOTADM_CHECKOUTLIST);
819 	if (fclose (fp) < 0)
820 	    error (0, errno, "cannot close %s", CVSROOTADM_CHECKOUTLIST);
821     }
822     else
823     {
824 	/* Error from CVS_FOPEN.  */
825 	if (!existence_error (errno))
826 	    error (0, errno, "cannot open %s", CVSROOTADM_CHECKOUTLIST);
827     }
828 
829     if (restore_cwd (&cwd))
830 	error (1, errno, "Failed to restore current directory, '%s'.",
831 	       cwd.name);
832     free_cwd (&cwd);
833 
834     return 0;
835 }
836 
837 
838 
839 /*
840  * Yeah, I know, there are NFS race conditions here.
841  */
842 static char *
make_tempfile(void)843 make_tempfile (void)
844 {
845     static int seed = 0;
846     int fd;
847     char *temp;
848 
849     if (seed == 0)
850 	seed = getpid ();
851     temp = xmalloc (sizeof (BAKPREFIX) + 40);
852     while (1)
853     {
854 	(void) sprintf (temp, "%s%d", BAKPREFIX, seed++);
855 	if ((fd = CVS_OPEN (temp, O_CREAT|O_EXCL|O_RDWR, 0666)) != -1)
856 	    break;
857 	if (errno != EEXIST)
858 	    error (1, errno, "cannot create temporary file %s", temp);
859     }
860     if (close(fd) < 0)
861 	error(1, errno, "cannot close temporary file %s", temp);
862     return temp;
863 }
864 
865 
866 
867 /* Get a file.  If the file does not exist, return 1 silently.  If
868    there is an error, print a message and return 1 (FIXME: probably
869    not a very clean convention).  On success, return 0.  */
870 static int
checkout_file(char * file,char * temp)871 checkout_file (char *file, char *temp)
872 {
873     char *rcs;
874     RCSNode *rcsnode;
875     int retcode = 0;
876 
877     if (noexec)
878 	return 0;
879 
880     rcs = Xasprintf ("%s%s", file, RCSEXT);
881     if (!isfile (rcs))
882     {
883 	free (rcs);
884 	return 1;
885     }
886 
887     rcsnode = RCS_parsercsfile (rcs);
888     if (!rcsnode)
889     {
890 	/* Probably not necessary (?); RCS_parsercsfile already printed a
891 	   message.  */
892 	error (0, 0, "Failed to parse '%s'.", rcs);
893 	free (rcs);
894 	return 1;
895     }
896 
897     retcode = RCS_checkout (rcsnode, NULL, NULL, NULL, NULL, temp, NULL, NULL);
898     if (retcode != 0)
899     {
900 	/* Probably not necessary (?); RCS_checkout already printed a
901 	   message.  */
902 	error (0, 0, "failed to check out %s file",
903 	       file);
904     }
905     freercsnode (&rcsnode);
906     free (rcs);
907     return retcode;
908 }
909 
910 
911 
912 #ifndef MY_NDBM
913 
914 static void
write_dbmfile(char * temp)915 write_dbmfile( char *temp )
916 {
917     char line[DBLKSIZ], value[DBLKSIZ];
918     FILE *fp;
919     DBM *db;
920     char *cp, *vp;
921     datum key, val;
922     int len, cont, err = 0;
923 
924     fp = xfopen (temp, "r");
925     if ((db = dbm_open (temp, O_RDWR | O_CREAT | O_TRUNC, 0666)) == NULL)
926 	error (1, errno, "cannot open dbm file %s for creation", temp);
927     for (cont = 0; fgets (line, sizeof (line), fp) != NULL;)
928     {
929 	if ((cp = strrchr (line, '\n')) != NULL)
930 	    *cp = '\0';			/* strip the newline */
931 
932 	/*
933 	 * Add the line to the value, at the end if this is a continuation
934 	 * line; otherwise at the beginning, but only after any trailing
935 	 * backslash is removed.
936 	 */
937 	vp = value;
938 	if (cont)
939 	    vp += strlen (value);
940 
941 	/*
942 	 * See if the line we read is a continuation line, and strip the
943 	 * backslash if so.
944 	 */
945 	len = strlen (line);
946 	if (len > 0)
947 	    cp = &line[len - 1];
948 	else
949 	    cp = line;
950 	if (*cp == '\\')
951 	{
952 	    cont = 1;
953 	    *cp = '\0';
954 	}
955 	else
956 	{
957 	    cont = 0;
958 	}
959 	(void) strcpy (vp, line);
960 	if (value[0] == '#')
961 	    continue;			/* comment line */
962 	vp = value;
963 	while (*vp && isspace ((unsigned char) *vp))
964 	    vp++;
965 	if (*vp == '\0')
966 	    continue;			/* empty line */
967 
968 	/*
969 	 * If this was not a continuation line, add the entry to the database
970 	 */
971 	if (!cont)
972 	{
973 	    key.dptr = vp;
974 	    while (*vp && !isspace ((unsigned char) *vp))
975 		vp++;
976 	    key.dsize = vp - (char *)key.dptr;
977 	    *vp++ = '\0';		/* NULL terminate the key */
978 	    while (*vp && isspace ((unsigned char) *vp))
979 		vp++;			/* skip whitespace to value */
980 	    if (*vp == '\0')
981 	    {
982 		error (0, 0, "warning: NULL value for key '%s'",
983 		    (char *)key.dptr);
984 		continue;
985 	    }
986 	    val.dptr = vp;
987 	    val.dsize = strlen (vp);
988 	    if (dbm_store (db, key, val, DBM_INSERT) == 1)
989 	    {
990 		error (0, 0, "duplicate key found for '%s'",
991 		    (char *)key.dptr);
992 		err++;
993 	    }
994 	}
995     }
996     dbm_close (db);
997     if (fclose (fp) < 0)
998 	error (0, errno, "cannot close %s", temp);
999     if (err)
1000     {
1001 	/* I think that the size of the buffer needed here is
1002 	   just determined by sizeof (CVSROOTADM_MODULES), the
1003 	   filenames created by make_tempfile, and other things that won't
1004 	   overflow.  */
1005 	char dotdir[50], dotpag[50], dotdb[50];
1006 
1007 	(void) sprintf (dotdir, "%s.dir", temp);
1008 	(void) sprintf (dotpag, "%s.pag", temp);
1009 	(void) sprintf (dotdb, "%s.db", temp);
1010 	if (unlink_file (dotdir) < 0
1011 	    && !existence_error (errno))
1012 	    error (0, errno, "cannot remove %s", dotdir);
1013 	if (unlink_file (dotpag) < 0
1014 	    && !existence_error (errno))
1015 	    error (0, errno, "cannot remove %s", dotpag);
1016 	if (unlink_file (dotdb) < 0
1017 	    && !existence_error (errno))
1018 	    error (0, errno, "cannot remove %s", dotdb);
1019 	error (1, 0, "DBM creation failed; correct above errors");
1020     }
1021 }
1022 
1023 static void
rename_dbmfile(char * temp)1024 rename_dbmfile( char *temp )
1025 {
1026     /* I think that the size of the buffer needed here is
1027        just determined by sizeof (CVSROOTADM_MODULES), the
1028        filenames created by make_tempfile, and other things that won't
1029        overflow.  */
1030     char newdir[50], newpag[50], newdb[50];
1031     char dotdir[50], dotpag[50], dotdb[50];
1032     char bakdir[50], bakpag[50], bakdb[50];
1033 
1034     int dir1_errno = 0, pag1_errno = 0, db1_errno = 0;
1035     int dir2_errno = 0, pag2_errno = 0, db2_errno = 0;
1036     int dir3_errno = 0, pag3_errno = 0, db3_errno = 0;
1037 
1038     (void) sprintf (dotdir, "%s.dir", CVSROOTADM_MODULES);
1039     (void) sprintf (dotpag, "%s.pag", CVSROOTADM_MODULES);
1040     (void) sprintf (dotdb, "%s.db", CVSROOTADM_MODULES);
1041     (void) sprintf (bakdir, "%s%s.dir", BAKPREFIX, CVSROOTADM_MODULES);
1042     (void) sprintf (bakpag, "%s%s.pag", BAKPREFIX, CVSROOTADM_MODULES);
1043     (void) sprintf (bakdb, "%s%s.db", BAKPREFIX, CVSROOTADM_MODULES);
1044     (void) sprintf (newdir, "%s.dir", temp);
1045     (void) sprintf (newpag, "%s.pag", temp);
1046     (void) sprintf (newdb, "%s.db", temp);
1047 
1048     (void) chmod (newdir, 0666);
1049     (void) chmod (newpag, 0666);
1050     (void) chmod (newdb, 0666);
1051 
1052     /* don't mess with me */
1053     SIG_beginCrSect ();
1054 
1055     /* rm .#modules.dir .#modules.pag */
1056     if (unlink_file (bakdir) < 0)
1057 	dir1_errno = errno;
1058     if (unlink_file (bakpag) < 0)
1059 	pag1_errno = errno;
1060     if (unlink_file (bakdb) < 0)
1061 	db1_errno = errno;
1062 
1063     /* mv modules.dir .#modules.dir */
1064     if (CVS_RENAME (dotdir, bakdir) < 0)
1065 	dir2_errno = errno;
1066     /* mv modules.pag .#modules.pag */
1067     if (CVS_RENAME (dotpag, bakpag) < 0)
1068 	pag2_errno = errno;
1069     /* mv modules.db .#modules.db */
1070     if (CVS_RENAME (dotdb, bakdb) < 0)
1071 	db2_errno = errno;
1072 
1073     /* mv "temp".dir modules.dir */
1074     if (CVS_RENAME (newdir, dotdir) < 0)
1075 	dir3_errno = errno;
1076     /* mv "temp".pag modules.pag */
1077     if (CVS_RENAME (newpag, dotpag) < 0)
1078 	pag3_errno = errno;
1079     /* mv "temp".db modules.db */
1080     if (CVS_RENAME (newdb, dotdb) < 0)
1081 	db3_errno = errno;
1082 
1083     /* OK -- make my day */
1084     SIG_endCrSect ();
1085 
1086     /* I didn't want to call error() when we had signals blocked
1087        (unnecessary?), but do it now.  */
1088     if (dir1_errno && !existence_error (dir1_errno))
1089 	error (0, dir1_errno, "cannot remove %s", bakdir);
1090     if (pag1_errno && !existence_error (pag1_errno))
1091 	error (0, pag1_errno, "cannot remove %s", bakpag);
1092     if (db1_errno && !existence_error (db1_errno))
1093 	error (0, db1_errno, "cannot remove %s", bakdb);
1094 
1095     if (dir2_errno && !existence_error (dir2_errno))
1096 	error (0, dir2_errno, "cannot remove %s", bakdir);
1097     if (pag2_errno && !existence_error (pag2_errno))
1098 	error (0, pag2_errno, "cannot remove %s", bakpag);
1099     if (db2_errno && !existence_error (db2_errno))
1100 	error (0, db2_errno, "cannot remove %s", bakdb);
1101 
1102     if (dir3_errno && !existence_error (dir3_errno))
1103 	error (0, dir3_errno, "cannot remove %s", bakdir);
1104     if (pag3_errno && !existence_error (pag3_errno))
1105 	error (0, pag3_errno, "cannot remove %s", bakpag);
1106     if (db3_errno && !existence_error (db3_errno))
1107 	error (0, db3_errno, "cannot remove %s", bakdb);
1108 }
1109 
1110 #endif				/* !MY_NDBM */
1111 
1112 static void
rename_rcsfile(char * temp,char * real)1113 rename_rcsfile (char *temp, char *real)
1114 {
1115     char *bak;
1116     struct stat statbuf;
1117     char *rcs;
1118 
1119     /* Set "x" bits if set in original. */
1120     rcs = Xasprintf ("%s%s", real, RCSEXT);
1121     statbuf.st_mode = 0; /* in case rcs file doesn't exist, but it should... */
1122     if (stat (rcs, &statbuf) < 0
1123 	&& !existence_error (errno))
1124 	error (0, errno, "cannot stat %s", rcs);
1125     free (rcs);
1126 
1127     if (chmod (temp, 0444 | (statbuf.st_mode & 0111)) < 0)
1128 	error (0, errno, "warning: cannot chmod %s", temp);
1129     bak = Xasprintf ("%s%s", BAKPREFIX, real);
1130 
1131     /* rm .#loginfo */
1132     if (unlink_file (bak) < 0
1133 	&& !existence_error (errno))
1134 	error (0, errno, "cannot remove %s", bak);
1135 
1136     /* mv loginfo .#loginfo */
1137     if (CVS_RENAME (real, bak) < 0
1138 	&& !existence_error (errno))
1139 	error (0, errno, "cannot rename %s to %s", real, bak);
1140 
1141     /* mv "temp" loginfo */
1142     if (CVS_RENAME (temp, real) < 0
1143 	&& !existence_error (errno))
1144 	error (0, errno, "cannot rename %s to %s", temp, real);
1145 
1146     free (bak);
1147 }
1148 
1149 const char *const init_usage[] = {
1150     "Usage: %s %s\n",
1151     "(Specify the --help global option for a list of other help options)\n",
1152     NULL
1153 };
1154 
1155 int
init(int argc,char ** argv)1156 init (int argc, char **argv)
1157 {
1158     /* Name of CVSROOT directory.  */
1159     char *adm;
1160     /* Name of this administrative file.  */
1161     char *info;
1162     /* Name of ,v file for this administrative file.  */
1163     char *info_v;
1164     /* Exit status.  */
1165     int err = 0;
1166 
1167     const struct admin_file *fileptr;
1168 
1169     umask (cvsumask);
1170 
1171     if (argc == -1 || argc > 1)
1172 	usage (init_usage);
1173 
1174 #ifdef CLIENT_SUPPORT
1175     if (current_parsed_root->isremote)
1176     {
1177 	start_server ();
1178 
1179 	ign_setup ();
1180 	send_init_command ();
1181 	return get_responses_and_close ();
1182     }
1183 #endif /* CLIENT_SUPPORT */
1184 
1185     /* Note: we do *not* create parent directories as needed like the
1186        old cvsinit.sh script did.  Few utilities do that, and a
1187        non-existent parent directory is as likely to be a typo as something
1188        which needs to be created.  */
1189     mkdir_if_needed (current_parsed_root->directory);
1190 
1191     if (noexec)
1192 	return (0);
1193 
1194     adm = Xasprintf ("%s/%s", current_parsed_root->directory, CVSROOTADM);
1195     mkdir_if_needed (adm);
1196 
1197     /* This is needed because we pass "fileptr->filename" not "info"
1198        to add_rcs_file below.  I think this would be easy to change,
1199        thus nuking the need for CVS_CHDIR here, but I haven't looked
1200        closely (e.g. see wrappers calls within add_rcs_file).  */
1201     if ( CVS_CHDIR (adm) < 0)
1202 	error (1, errno, "cannot change to directory %s", adm);
1203 
1204     /* Make Emptydir so it's there if we need it */
1205     mkdir_if_needed (CVSNULLREPOS);
1206 
1207     /* 80 is long enough for all the administrative file names, plus
1208        "/" and so on.  */
1209     info = xmalloc (strlen (adm) + 80);
1210     info_v = xmalloc (strlen (adm) + 80);
1211     for (fileptr = filelist; fileptr && fileptr->filename; ++fileptr)
1212     {
1213 	if (fileptr->contents == NULL)
1214 	    continue;
1215 	strcpy (info, adm);
1216 	strcat (info, "/");
1217 	strcat (info, fileptr->filename);
1218 	strcpy (info_v, info);
1219 	strcat (info_v, RCSEXT);
1220 	if (isfile (info_v))
1221 	    /* We will check out this file in the mkmodules step.
1222 	       Nothing else is required.  */
1223 	    ;
1224 	else
1225 	{
1226 	    int retcode;
1227 
1228 	    if (!isfile (info))
1229 	    {
1230 		FILE *fp;
1231 		const char * const *p;
1232 
1233 		fp = xfopen (info, "w");
1234 		for (p = fileptr->contents; *p != NULL; ++p)
1235 		    if (fputs (*p, fp) < 0)
1236 			error (1, errno, "cannot write %s", info);
1237 		if (fclose (fp) < 0)
1238 		    error (1, errno, "cannot close %s", info);
1239 	    }
1240 	    /* The message used to say " of " and fileptr->filename after
1241 	       "initial checkin" but I fail to see the point as we know what
1242 	       file it is from the name.  */
1243 	    retcode = add_rcs_file ("initial checkin", info_v,
1244 				    fileptr->filename, "1.1", NULL,
1245 
1246 				    /* No vendor branch.  */
1247 				    NULL, NULL, 0, NULL,
1248 
1249 				    NULL, 0, NULL, 0);
1250 	    if (retcode != 0)
1251 		/* add_rcs_file already printed an error message.  */
1252 		err = 1;
1253 	}
1254     }
1255 
1256     /* Turn on history logging by default.  The user can remove the file
1257        to disable it.  */
1258     strcpy (info, adm);
1259     strcat (info, "/");
1260     strcat (info, CVSROOTADM_HISTORY);
1261     if (!isfile (info))
1262     {
1263 	FILE *fp;
1264 
1265 	fp = xfopen (info, "w");
1266 	if (fclose (fp) < 0)
1267 	    error (1, errno, "cannot close %s", info);
1268 
1269         /* Make the new history file world-writeable, since every CVS
1270            user will need to be able to write to it.  We use chmod()
1271            because xchmod() is too shy. */
1272         chmod (info, 0666);
1273     }
1274 
1275     /* Make an empty val-tags file to prevent problems creating it later.  */
1276     strcpy (info, adm);
1277     strcat (info, "/");
1278     strcat (info, CVSROOTADM_VALTAGS);
1279     if (!isfile (info))
1280     {
1281 	FILE *fp;
1282 
1283 	fp = xfopen (info, "w");
1284 	if (fclose (fp) < 0)
1285 	    error (1, errno, "cannot close %s", info);
1286 
1287         /* Make the new val-tags file world-writeable, since every CVS
1288            user will need to be able to write to it.  We use chmod()
1289            because xchmod() is too shy. */
1290         chmod (info, 0666);
1291     }
1292 
1293     free (info);
1294     free (info_v);
1295 
1296     mkmodules (adm);
1297 
1298     free (adm);
1299     return err;
1300 }
1301