repos.c revision 32785
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    char *dir;
25    char *update_dir;
26{
27    FILE *fpin;
28    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 (strcmp (repos, "..") == 0 || strncmp (repos, "../", 3) == 0)
110    {
111	error (0, 0, "in directory %s:", xupdate_dir);
112	error (0, 0, "`..'-relative repositories are not supported.");
113	error (1, 0, "illegal source repository");
114    }
115    if (! isabsolute(repos))
116    {
117	char *newrepos;
118
119	if (CVSroot_original == NULL)
120	{
121	    error (0, 0, "in directory %s:", xupdate_dir);
122	    error (0, 0, "must set the CVSROOT environment variable\n");
123	    error (0, 0, "or specify the '-d' option to %s.", program_name);
124	    error (1, 0, "illegal repository setting");
125	}
126	newrepos = xmalloc (strlen (CVSroot_directory) + strlen (repos) + 10);
127	(void) sprintf (newrepos, "%s/%s", CVSroot_directory, repos);
128	free (repos);
129	repos = newrepos;
130    }
131
132    Sanitize_Repository_Name (repos);
133
134    return repos;
135}
136
137/*
138 * Return a pointer to the repository name relative to CVSROOT from a
139 * possibly fully qualified repository
140 */
141char *
142Short_Repository (repository)
143    char *repository;
144{
145    if (repository == NULL)
146	return (NULL);
147
148    /* If repository matches CVSroot at the beginning, strip off CVSroot */
149    /* And skip leading '/' in rep, in case CVSroot ended with '/'. */
150    if (strncmp (CVSroot_directory, repository,
151		 strlen (CVSroot_directory)) == 0)
152    {
153	char *rep = repository + strlen (CVSroot_directory);
154	return (*rep == '/') ? rep+1 : rep;
155    }
156    else
157	return (repository);
158}
159
160/* Sanitize the repository name (in place) by removing trailing
161 * slashes and a trailing "." if present.  It should be safe for
162 * callers to use strcat and friends to create repository names.
163 * Without this check, names like "/path/to/repos/./foo" and
164 * "/path/to/repos//foo" would be created.  For example, one
165 * significant case is the CVSROOT-detection code in commit.c.  It
166 * decides whether or not it needs to rebuild the administrative file
167 * database by doing a string compare.  If we've done a `cvs co .' to
168 * get the CVSROOT files, "/path/to/repos/./CVSROOT" and
169 * "/path/to/repos/CVSROOT" are the arguments that are compared!
170 *
171 * This function ends up being called from the same places as
172 * strip_path, though what it does is much more conservative.  Many
173 * comments about this operation (which was scattered around in
174 * several places in the source code) ran thus:
175 *
176 *    ``repository ends with "/."; omit it.  This sort of thing used
177 *    to be taken care of by strip_path.  Now we try to be more
178 *    selective.  I suspect that it would be even better to push it
179 *    back further someday, so that the trailing "/." doesn't get into
180 *    repository in the first place, but we haven't taken things that
181 *    far yet.''        --Jim Kingdon (recurse.c, 07-Sep-97)
182 *
183 * Ahh, all too true.  The major consideration is RELATIVE_REPOS.  If
184 * the "/." doesn't end up in the repository while RELATIVE_REPOS is
185 * defined, there will be nothing in the CVS/Repository file.  I
186 * haven't verified that the remote protocol will handle that
187 * correctly yet, so I've not made that change. */
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