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