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 #include <sys/cdefs.h> 14 __RCSID("$NetBSD: repos.c,v 1.2 2016/05/17 14:00:09 christos Exp $"); 15 16 #include "cvs.h" 17 #include "getline.h" 18 19 20 21 /* Determine the name of the RCS repository for directory DIR in the 22 current working directory, or for the current working directory 23 itself if DIR is NULL. Returns the name in a newly-malloc'd 24 string. On error, gives a fatal error and does not return. 25 UPDATE_DIR is the path from where cvs was invoked (for use in error 26 messages), and should contain DIR as its last component. 27 UPDATE_DIR can be NULL to signify the directory in which cvs was 28 invoked. */ 29 30 char * 31 Name_Repository (const char *dir, const char *update_dir) 32 { 33 FILE *fpin; 34 const char *xupdate_dir; 35 char *repos = NULL; 36 size_t repos_allocated = 0; 37 char *tmp; 38 char *cp; 39 40 if (update_dir && *update_dir) 41 xupdate_dir = update_dir; 42 else 43 xupdate_dir = "."; 44 45 if (dir != NULL) 46 tmp = Xasprintf ("%s/%s", dir, CVSADM_REP); 47 else 48 tmp = xstrdup (CVSADM_REP); 49 50 /* 51 * The assumption here is that the repository is always contained in the 52 * first line of the "Repository" file. 53 */ 54 fpin = CVS_FOPEN (tmp, "r"); 55 56 if (fpin == NULL) 57 { 58 int save_errno = errno; 59 char *cvsadm; 60 61 if (dir != NULL) 62 cvsadm = Xasprintf ("%s/%s", dir, CVSADM); 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 /* This occurs at least in the case where the user manually 77 * creates a directory named CVS. 78 */ 79 error (0, 0, "in directory `%s':", xupdate_dir); 80 error (0, 0, "CVS directory found without administrative files."); 81 error (0, 0, "Use CVS to create the CVS directory, or rename the"); 82 error (0, 0, "directory if it is intended to store something"); 83 error (0, 0, "besides CVS administrative files."); 84 error (1, 0, "*PANIC* administration files missing!"); 85 } 86 87 error (1, save_errno, "cannot open `%s'", tmp); 88 } 89 90 if (getline (&repos, &repos_allocated, fpin) < 0) 91 { 92 /* FIXME: should be checking for end of file separately. */ 93 error (0, 0, "in directory `%s':", xupdate_dir); 94 error (1, errno, "cannot read `%s'", CVSADM_REP); 95 } 96 if (fclose (fpin) < 0) 97 error (0, errno, "cannot close `%s'", tmp); 98 free (tmp); 99 100 if ((cp = strrchr (repos, '\n')) != NULL) 101 *cp = '\0'; /* strip the newline */ 102 103 /* If this is a relative repository pathname, turn it into an absolute 104 * one by tacking on the current root. There is no need to grab it from 105 * the CVS/Root file via the Name_Root() function because by the time 106 * this function is called, we the contents of CVS/Root have already been 107 * compared to original_root and found to match. 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, "invalid 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, "invalid source repository"); 125 } 126 newrepos = Xasprintf ("%s/%s", original_parsed_root->directory, repos); 127 free (repos); 128 repos = newrepos; 129 } 130 131 Sanitize_Repository_Name (repos); 132 133 return repos; 134 } 135 136 137 138 /* 139 * Return a pointer to the repository name relative to CVSROOT from a 140 * possibly fully qualified repository 141 */ 142 const char * 143 Short_Repository (const 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 (original_parsed_root->directory, repository, 151 strlen (original_parsed_root->directory)) == 0) 152 { 153 const char *rep = repository + strlen (original_parsed_root->directory); 154 return (*rep == '/') ? rep+1 : rep; 155 } 156 else 157 return repository; 158 } 159 160 161 162 /* Sanitize the repository name (in place) by removing trailing 163 * slashes and a trailing "." if present. It should be safe for 164 * callers to use strcat and friends to create repository names. 165 * Without this check, names like "/path/to/repos/./foo" and 166 * "/path/to/repos//foo" would be created. For example, one 167 * significant case is the CVSROOT-detection code in commit.c. It 168 * decides whether or not it needs to rebuild the administrative file 169 * database by doing a string compare. If we've done a `cvs co .' to 170 * get the CVSROOT files, "/path/to/repos/./CVSROOT" and 171 * "/path/to/repos/CVSROOT" are the arguments that are compared! 172 * 173 * This function ends up being called from the same places as 174 * strip_path, though what it does is much more conservative. Many 175 * comments about this operation (which was scattered around in 176 * several places in the source code) ran thus: 177 * 178 * ``repository ends with "/."; omit it. This sort of thing used 179 * to be taken care of by strip_path. Now we try to be more 180 * selective. I suspect that it would be even better to push it 181 * back further someday, so that the trailing "/." doesn't get into 182 * repository in the first place, but we haven't taken things that 183 * far yet.'' --Jim Kingdon (recurse.c, 07-Sep-97) 184 */ 185 186 void 187 Sanitize_Repository_Name (char *repository) 188 { 189 size_t len; 190 191 assert (repository != NULL); 192 193 strip_trailing_slashes (repository); 194 195 len = strlen (repository); 196 if (len >= 2 197 && repository[len - 1] == '.' 198 && ISSLASH (repository[len - 2])) 199 { 200 repository[len - 2] = '\0'; 201 } 202 } 203