create_adm.c revision 128266
1/*
2 * Copyright (c) 1992, Brian Berliner and Jeff Polk
3 * Copyright (c) 1989-1992, Brian Berliner
4 *
5 * You may distribute under the terms of the GNU General Public License as
6 * specified in the README file that comes with the CVS source distribution.
7 *
8 * Create Administration.
9 *
10 * Creates a CVS administration directory based on the argument repository; the
11 * "Entries" file is prefilled from the "initrecord" argument.
12 */
13
14#include "cvs.h"
15
16/* update_dir includes dir as its last component.
17
18   Return value is 0 for success, or 1 if we printed a warning message.
19   Note that many errors are still fatal; particularly for unlikely errors
20   a fatal error is probably better than a warning which might be missed
21   or after which CVS might do something non-useful.  If WARN is zero, then
22   don't print warnings; all errors are fatal then.  */
23
24int
25Create_Admin (dir, update_dir, repository, tag, date, nonbranch, warn,
26	      dotemplate)
27    const char *dir;
28    const char *update_dir;
29    const char *repository;
30    const char *tag;
31    const char *date;
32    int nonbranch;
33    int warn;
34    int dotemplate;
35{
36    FILE *fout;
37    char *cp;
38    char *reposcopy;
39    char *tmp;
40
41    if (trace)
42    {
43	fprintf (stderr, "%s-> Create_Admin (%s, %s, %s, %s, %s, %d, %d, %d)\n",
44		 CLIENT_SERVER_STR,
45		 dir, update_dir, repository, tag ? tag : "",
46		 date ? date : "", nonbranch, warn, dotemplate);
47    }
48
49    if (noexec)
50	return 0;
51
52    tmp = xmalloc (strlen (dir) + 100);
53    (void) sprintf (tmp, "%s/%s", dir, CVSADM);
54    if (isfile (tmp))
55	error (1, 0, "there is a version in %s already", update_dir);
56
57    if (CVS_MKDIR (tmp, 0777) < 0)
58    {
59	/* We want to print out the entire update_dir, since a lot of
60	   our code calls this function with dir == "." or dir ==
61	   NULL.  I hope that gives enough information in cases like
62	   absolute pathnames; printing out xgetwd or something would
63	   be way too verbose in the common cases.  */
64
65	if (warn)
66	{
67	    /* The reason that this is a warning, rather than silently
68	       just skipping creating the directory, is that we don't want
69	       CVS's behavior to vary subtly based on factors (like directory
70	       permissions) which are not made clear to the user.  With
71	       the warning at least we let them know what is going on.  */
72	    error (0, errno, "warning: cannot make directory %s in %s",
73		   CVSADM, update_dir);
74	    free (tmp);
75	    return 1;
76	}
77	else
78	    error (1, errno, "cannot make directory %s in %s",
79		   CVSADM, update_dir);
80    }
81
82    /* record the current cvs root for later use */
83
84    Create_Root (dir, current_parsed_root->original);
85    if (dir != NULL)
86	(void) sprintf (tmp, "%s/%s", dir, CVSADM_REP);
87    else
88	(void) strcpy (tmp, CVSADM_REP);
89    fout = CVS_FOPEN (tmp, "w+");
90    if (fout == NULL)
91    {
92	if (update_dir[0] == '\0')
93	    error (1, errno, "cannot open %s", tmp);
94	else
95	    error (1, errno, "cannot open %s/%s", update_dir, CVSADM_REP);
96    }
97    reposcopy = xstrdup (repository);
98    Sanitize_Repository_Name (reposcopy);
99
100    /* The top level of the repository is a special case -- we need to
101       write it with an extra dot at the end.  This trailing `.' stuff
102       rubs me the wrong way -- on the other hand, I don't want to
103       spend the time making sure all of the code can handle it if we
104       don't do it. */
105
106    if (strcmp (reposcopy, current_parsed_root->directory) == 0)
107    {
108	reposcopy = xrealloc (reposcopy, strlen (reposcopy) + 3);
109	strcat (reposcopy, "/.");
110    }
111
112    cp = reposcopy;
113
114    /*
115     * If the Repository file is to hold a relative path, try to strip off
116     * the leading CVSroot argument.
117     */
118    {
119    char *path = xmalloc (strlen (current_parsed_root->directory) + 2);
120
121    (void) sprintf (path, "%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#ifdef SERVER_SUPPORT
167    if (server_active && dotemplate)
168    {
169	server_template (update_dir, repository);
170    }
171
172    if (trace)
173    {
174	fprintf (stderr, "%c<- Create_Admin\n",
175		 (server_active) ? 'S' : ' ');
176    }
177#endif
178
179    free (reposcopy);
180    free (tmp);
181    return 0;
182}
183