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