repos.c revision 17721
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 1.4 kit.
7 */
8
9#include "cvs.h"
10
11/* Determine the name of the RCS repository for directory DIR in the
12   current working directory, or for the current working directory
13   itself if DIR is NULL.  Returns the name in a newly-malloc'd
14   string.  On error, gives a fatal error and does not return.
15   UPDATE_DIR is the path from where cvs was invoked (for use in error
16   messages), and should contain DIR as its last component.
17   UPDATE_DIR can be NULL to signify the directory in which cvs was
18   invoked.  */
19
20char *
21Name_Repository (dir, update_dir)
22    char *dir;
23    char *update_dir;
24{
25    FILE *fpin;
26    char *ret, *xupdate_dir;
27    char repos[PATH_MAX];
28    char path[PATH_MAX];
29    char tmp[PATH_MAX];
30    char cvsadm[PATH_MAX];
31    char *cp;
32
33    if (update_dir && *update_dir)
34	xupdate_dir = update_dir;
35    else
36	xupdate_dir = ".";
37
38    if (dir != NULL)
39	(void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
40    else
41	(void) strcpy (cvsadm, CVSADM);
42
43    /* sanity checks */
44    if (!isdir (cvsadm))
45    {
46	error (0, 0, "in directory %s:", xupdate_dir);
47	error (1, 0, "there is no version here; do '%s checkout' first",
48	       program_name);
49    }
50
51    if (dir != NULL)
52	(void) sprintf (tmp, "%s/%s", dir, CVSADM_ENT);
53    else
54	(void) strcpy (tmp, CVSADM_ENT);
55
56    if (!isreadable (tmp))
57    {
58	error (0, 0, "in directory %s:", xupdate_dir);
59	error (1, 0, "*PANIC* administration files missing");
60    }
61
62    if (dir != NULL)
63	(void) sprintf (tmp, "%s/%s", dir, CVSADM_REP);
64    else
65	(void) strcpy (tmp, CVSADM_REP);
66
67    if (!isreadable (tmp))
68    {
69	error (0, 0, "in directory %s:", xupdate_dir);
70	error (1, 0, "*PANIC* administration files missing");
71    }
72
73    /*
74     * The assumption here is that the repository is always contained in the
75     * first line of the "Repository" file.
76     */
77    fpin = open_file (tmp, "r");
78
79    if (fgets (repos, PATH_MAX, fpin) == NULL)
80    {
81	error (0, 0, "in directory %s:", xupdate_dir);
82	error (1, errno, "cannot read %s", CVSADM_REP);
83    }
84    (void) fclose (fpin);
85    if ((cp = strrchr (repos, '\n')) != NULL)
86	*cp = '\0';			/* strip the newline */
87
88    /*
89     * If this is a relative repository pathname, turn it into an absolute
90     * one by tacking on the CVSROOT environment variable. If the CVSROOT
91     * environment variable is not set, die now.
92     */
93    if (strcmp (repos, "..") == 0 || strncmp (repos, "../", 3) == 0)
94    {
95	error (0, 0, "in directory %s:", xupdate_dir);
96	error (0, 0, "`..'-relative repositories are not supported.");
97	error (1, 0, "illegal source repository");
98    }
99    if (! isabsolute(repos))
100    {
101	if (CVSroot == NULL)
102	{
103	    error (0, 0, "in directory %s:", xupdate_dir);
104	    error (0, 0, "must set the CVSROOT environment variable\n");
105	    error (0, 0, "or specify the '-d' option to %s.", program_name);
106	    error (1, 0, "illegal repository setting");
107	}
108	(void) strcpy (path, repos);
109	(void) sprintf (repos, "%s/%s", CVSroot, path);
110    }
111#ifdef CLIENT_SUPPORT
112    if (!client_active && !isdir (repos))
113#else
114    if (!isdir (repos))
115#endif
116    {
117	error (0, 0, "in directory %s:", xupdate_dir);
118	error (1, 0, "there is no repository %s", repos);
119    }
120
121    /* allocate space to return and fill it in */
122    strip_path (repos);
123    ret = xstrdup (repos);
124    return (ret);
125}
126
127/*
128 * Return a pointer to the repository name relative to CVSROOT from a
129 * possibly fully qualified repository
130 */
131char *
132Short_Repository (repository)
133    char *repository;
134{
135    if (repository == NULL)
136	return (NULL);
137
138    /* If repository matches CVSroot at the beginning, strip off CVSroot */
139    /* And skip leading '/' in rep, in case CVSroot ended with '/'. */
140    if (strncmp (CVSroot, repository, strlen (CVSroot)) == 0)
141    {
142	char *rep = repository + strlen (CVSroot);
143	return (*rep == '/') ? rep+1 : rep;
144    }
145    else
146	return (repository);
147}
148