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 source distribution.
12 *
13 * Create Administration.
14 *
15 * Creates a CVS administration directory based on the argument repository; the
16 * "Entries" file is prefilled from the "initrecord" argument.
17 */
18
19#include "cvs.h"
20
21
22
23/* update_dir includes dir as its last component.
24
25   Return value is 0 for success, or 1 if we printed a warning message.
26   Note that many errors are still fatal; particularly for unlikely errors
27   a fatal error is probably better than a warning which might be missed
28   or after which CVS might do something non-useful.  If WARN is zero, then
29   don't print warnings; all errors are fatal then.  */
30
31int
32Create_Admin (const char *dir, const char *update_dir, const char *repository,
33              const char *tag, const char *date, int nonbranch, int warn,
34              int dotemplate)
35{
36    FILE *fout;
37    char *cp;
38    char *reposcopy;
39    char *tmp;
40
41    TRACE (TRACE_FUNCTION, "Create_Admin (%s, %s, %s, %s, %s, %d, %d, %d)",
42	   dir, update_dir, repository, tag ? tag : "",
43	   date ? date : "", nonbranch, warn, dotemplate);
44
45    if (noexec)
46	return 0;
47
48    tmp = Xasprintf ("%s/%s", dir, CVSADM);
49    if (isfile (tmp))
50	error (1, 0, "there is a version in %s already", update_dir);
51
52    if (CVS_MKDIR (tmp, 0777) < 0)
53    {
54	free (tmp);
55	tmp = NULL;
56
57	/* We want to print out the entire update_dir, since a lot of
58	   our code calls this function with dir == "." or dir ==
59	   NULL.  I hope that gives enough information in cases like
60	   absolute pathnames; printing out xgetcwd() or something would
61	   be way too verbose in the common cases.  */
62
63	if (warn)
64	{
65	    /* The reason that this is a warning, rather than silently
66	       just skipping creating the directory, is that we don't want
67	       CVS's behavior to vary subtly based on factors (like directory
68	       permissions) which are not made clear to the user.  With
69	       the warning at least we let them know what is going on.  */
70	    error (0, errno, "warning: cannot make directory %s in %s",
71		   CVSADM, update_dir);
72	    return 1;
73	}
74	else
75	    error (1, errno, "cannot make directory %s in %s",
76		   CVSADM, update_dir);
77    }
78    else
79    {
80	free (tmp);
81	tmp = NULL;
82    }
83
84    /* record the current cvs root for later use */
85
86    Create_Root (dir, original_parsed_root->original);
87    if (dir != NULL)
88	tmp = Xasprintf ("%s/%s", dir, CVSADM_REP);
89    else
90	tmp = xstrdup (CVSADM_REP);
91    fout = CVS_FOPEN (tmp, "w+");
92    if (fout == NULL)
93    {
94	if (update_dir[0] == '\0')
95	    error (1, errno, "cannot open %s", tmp);
96	else
97	    error (1, errno, "cannot open %s/%s", update_dir, CVSADM_REP);
98    }
99    reposcopy = xstrdup (repository);
100    Sanitize_Repository_Name (reposcopy);
101
102    /* The top level of the repository is a special case -- we need to
103       write it with an extra dot at the end.  This trailing `.' stuff
104       rubs me the wrong way -- on the other hand, I don't want to
105       spend the time making sure all of the code can handle it if we
106       don't do it. */
107
108    if (strcmp (reposcopy, current_parsed_root->directory) == 0)
109    {
110	reposcopy = xrealloc (reposcopy, strlen (reposcopy) + 3);
111	strcat (reposcopy, "/.");
112    }
113
114    cp = reposcopy;
115
116    /*
117     * If the Repository file is to hold a relative path, try to strip off
118     * the leading CVSroot argument.
119     */
120    {
121	char *path = Xasprintf ("%s/", current_parsed_root->directory);
122	if (strncmp (cp, path, strlen (path)) == 0)
123	    cp += strlen (path);
124	free (path);
125    }
126
127    if (fprintf (fout, "%s\n", cp) < 0)
128    {
129	if (update_dir[0] == '\0')
130	    error (1, errno, "write to %s failed", tmp);
131	else
132	    error (1, errno, "write to %s/%s failed", update_dir, CVSADM_REP);
133    }
134    if (fclose (fout) == EOF)
135    {
136	if (update_dir[0] == '\0')
137	    error (1, errno, "cannot close %s", tmp);
138	else
139	    error (1, errno, "cannot close %s/%s", update_dir, CVSADM_REP);
140    }
141
142    /* now, do the Entries file */
143    if (dir != NULL)
144	(void) sprintf (tmp, "%s/%s", dir, CVSADM_ENT);
145    else
146	(void) strcpy (tmp, CVSADM_ENT);
147    fout = CVS_FOPEN (tmp, "w+");
148    if (fout == NULL)
149    {
150	if (update_dir[0] == '\0')
151	    error (1, errno, "cannot open %s", tmp);
152	else
153	    error (1, errno, "cannot open %s/%s", update_dir, CVSADM_ENT);
154    }
155    if (fclose (fout) == EOF)
156    {
157	if (update_dir[0] == '\0')
158	    error (1, errno, "cannot close %s", tmp);
159	else
160	    error (1, errno, "cannot close %s/%s", update_dir, CVSADM_ENT);
161    }
162
163    /* Create a new CVS/Tag file */
164    WriteTag (dir, tag, date, nonbranch, update_dir, repository);
165
166    TRACE (TRACE_FUNCTION, "Create_Admin");
167
168    free (reposcopy);
169    free (tmp);
170    return 0;
171}
172