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