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