]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/cvs/src/repos.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / cvs / src / repos.c
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
14 #include <assert.h>
15 #include "cvs.h"
16 #include "getline.h"
17
18 /* Determine the name of the RCS repository for directory DIR in the
19    current working directory, or for the current working directory
20    itself if DIR is NULL.  Returns the name in a newly-malloc'd
21    string.  On error, gives a fatal error and does not return.
22    UPDATE_DIR is the path from where cvs was invoked (for use in error
23    messages), and should contain DIR as its last component.
24    UPDATE_DIR can be NULL to signify the directory in which cvs was
25    invoked.  */
26
27 char *
28 Name_Repository (dir, update_dir)
29     const char *dir;
30     const char *update_dir;
31 {
32     FILE *fpin;
33     const char *xupdate_dir;
34     char *repos = NULL;
35     size_t repos_allocated = 0;
36     char *tmp;
37     char *cp;
38
39     if (update_dir && *update_dir)
40         xupdate_dir = update_dir;
41     else
42         xupdate_dir = ".";
43
44     if (dir != NULL)
45     {
46         tmp = xmalloc (strlen (dir) + sizeof (CVSADM_REP) + 10);
47         (void) sprintf (tmp, "%s/%s", dir, CVSADM_REP);
48     }
49     else
50         tmp = xstrdup (CVSADM_REP);
51
52     /*
53      * The assumption here is that the repository is always contained in the
54      * first line of the "Repository" file.
55      */
56     fpin = CVS_FOPEN (tmp, "r");
57
58     if (fpin == NULL)
59     {
60         int save_errno = errno;
61         char *cvsadm;
62
63         if (dir != NULL)
64         {
65             cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
66             (void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
67         }
68         else
69             cvsadm = xstrdup (CVSADM);
70
71         if (!isdir (cvsadm))
72         {
73             error (0, 0, "in directory %s:", xupdate_dir);
74             error (1, 0, "there is no version here; do '%s checkout' first",
75                    program_name);
76         }
77         free (cvsadm);
78
79         if (existence_error (save_errno))
80         {
81             /* FIXME: This is a very poorly worded error message.  It
82                occurs at least in the case where the user manually
83                creates a directory named CVS, so the error message
84                should be more along the lines of "CVS directory found
85                without administrative files; use CVS to create the CVS
86                directory, or rename it to something else if the
87                intention is to store something besides CVS
88                administrative files".  */
89             error (0, 0, "in directory %s:", xupdate_dir);
90             error (1, 0, "*PANIC* administration files missing");
91         }
92
93         error (1, save_errno, "cannot open %s", tmp);
94     }
95
96     if (getline (&repos, &repos_allocated, fpin) < 0)
97     {
98         /* FIXME: should be checking for end of file separately.  */
99         error (0, 0, "in directory %s:", xupdate_dir);
100         error (1, errno, "cannot read %s", CVSADM_REP);
101     }
102     if (fclose (fpin) < 0)
103         error (0, errno, "cannot close %s", tmp);
104     free (tmp);
105
106     if ((cp = strrchr (repos, '\n')) != NULL)
107         *cp = '\0';                     /* strip the newline */
108
109     /*
110      * If this is a relative repository pathname, turn it into an absolute
111      * one by tacking on the CVSROOT environment variable. If the CVSROOT
112      * environment variable is not set, die now.
113      */
114     if (! isabsolute(repos))
115     {
116         char *newrepos;
117
118         if (current_parsed_root == NULL)
119         {
120             error (0, 0, "in directory %s:", xupdate_dir);
121             error (0, 0, "must set the CVSROOT environment variable\n");
122             error (0, 0, "or specify the '-d' option to %s.", program_name);
123             error (1, 0, "illegal repository setting");
124         }
125         if (pathname_levels (repos) > 0)
126         {
127             error (0, 0, "in directory %s:", xupdate_dir);
128             error (0, 0, "`..'-relative repositories are not supported.");
129             error (1, 0, "illegal source repository");
130         }
131         newrepos = xmalloc (strlen (current_parsed_root->directory)
132                             + strlen (repos) + 2);
133         sprintf (newrepos, "%s/%s", current_parsed_root->directory, repos);
134         free (repos);
135         repos = newrepos;
136     }
137
138     Sanitize_Repository_Name (repos);
139
140     return repos;
141 }
142
143
144
145 /*
146  * Return a pointer to the repository name relative to CVSROOT from a
147  * possibly fully qualified repository
148  */
149 const char *
150 Short_Repository (repository)
151     const char *repository;
152 {
153     if (repository == NULL)
154         return NULL;
155
156     /* If repository matches CVSroot at the beginning, strip off CVSroot */
157     /* And skip leading '/' in rep, in case CVSroot ended with '/'. */
158     if (strncmp (current_parsed_root->directory, repository,
159                  strlen (current_parsed_root->directory)) == 0)
160     {
161         const char *rep = repository + strlen (current_parsed_root->directory);
162         return (*rep == '/') ? rep+1 : rep;
163     }
164     else
165         return repository;
166 }
167
168
169
170 /* Sanitize the repository name (in place) by removing trailing
171  * slashes and a trailing "." if present.  It should be safe for
172  * callers to use strcat and friends to create repository names.
173  * Without this check, names like "/path/to/repos/./foo" and
174  * "/path/to/repos//foo" would be created.  For example, one
175  * significant case is the CVSROOT-detection code in commit.c.  It
176  * decides whether or not it needs to rebuild the administrative file
177  * database by doing a string compare.  If we've done a `cvs co .' to
178  * get the CVSROOT files, "/path/to/repos/./CVSROOT" and
179  * "/path/to/repos/CVSROOT" are the arguments that are compared!
180  *
181  * This function ends up being called from the same places as
182  * strip_path, though what it does is much more conservative.  Many
183  * comments about this operation (which was scattered around in
184  * several places in the source code) ran thus:
185  *
186  *    ``repository ends with "/."; omit it.  This sort of thing used
187  *    to be taken care of by strip_path.  Now we try to be more
188  *    selective.  I suspect that it would be even better to push it
189  *    back further someday, so that the trailing "/." doesn't get into
190  *    repository in the first place, but we haven't taken things that
191  *    far yet.''        --Jim Kingdon (recurse.c, 07-Sep-97)
192  */
193
194 void
195 Sanitize_Repository_Name (repository)
196     char *repository;
197 {
198     size_t len;
199
200     assert (repository != NULL);
201
202     strip_trailing_slashes (repository);
203
204     len = strlen (repository);
205     if (len >= 2
206         && repository[len - 1] == '.'
207         && ISDIRSEP (repository[len - 2]))
208     {
209         repository[len - 2] = '\0';
210     }
211 }