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 "cvs.h" 15 #include "getline.h" 16 17 18 19 /* Determine the name of the RCS repository for directory DIR in the 20 current working directory, or for the current working directory 21 itself if DIR is NULL. Returns the name in a newly-malloc'd 22 string. On error, gives a fatal error and does not return. 23 UPDATE_DIR is the path from where cvs was invoked (for use in error 24 messages), and should contain DIR as its last component. 25 UPDATE_DIR can be NULL to signify the directory in which cvs was 26 invoked. */ 27 28 char * 29 Name_Repository (const char *dir, const char *update_dir) 30 { 31 FILE *fpin; 32 const char *xupdate_dir; 33 char *repos = NULL; 34 size_t repos_allocated = 0; 35 char *tmp; 36 char *cp; 37 38 if (update_dir && *update_dir) 39 xupdate_dir = update_dir; 40 else 41 xupdate_dir = "."; 42 43 if (dir != NULL) 44 tmp = Xasprintf ("%s/%s", dir, CVSADM_REP); 45 else 46 tmp = xstrdup (CVSADM_REP); 47 48 /* 49 * The assumption here is that the repository is always contained in the 50 * first line of the "Repository" file. 51 */ 52 fpin = CVS_FOPEN (tmp, "r"); 53 54 if (fpin == NULL) 55 { 56 int save_errno = errno; 57 char *cvsadm; 58 59 if (dir != NULL) 60 cvsadm = Xasprintf ("%s/%s", dir, CVSADM); 61 else 62 cvsadm = xstrdup (CVSADM); 63 64 if (!isdir (cvsadm)) 65 { 66 error (0, 0, "in directory `%s':", xupdate_dir); 67 error (1, 0, "there is no version here; do `%s checkout' first", 68 program_name); 69 } 70 free (cvsadm); 71 72 if (existence_error (save_errno)) 73 { 74 /* This occurs at least in the case where the user manually 75 * creates a directory named CVS. 76 */ 77 error (0, 0, "in directory `%s':", xupdate_dir); 78 error (0, 0, "CVS directory found without administrative files."); 79 error (0, 0, "Use CVS to create the CVS directory, or rename the"); 80 error (0, 0, "directory if it is intended to store something"); 81 error (0, 0, "besides CVS administrative files."); 82 error (1, 0, "*PANIC* administration files missing!"); 83 } 84 85 error (1, save_errno, "cannot open `%s'", tmp); 86 } 87 88 if (getline (&repos, &repos_allocated, fpin) < 0) 89 { 90 /* FIXME: should be checking for end of file separately. */ 91 error (0, 0, "in directory `%s':", xupdate_dir); 92 error (1, errno, "cannot read `%s'", CVSADM_REP); 93 } 94 if (fclose (fpin) < 0) 95 error (0, errno, "cannot close `%s'", tmp); 96 free (tmp); 97 98 if ((cp = strrchr (repos, '\n')) != NULL) 99 *cp = '\0'; /* strip the newline */ 100 101 /* If this is a relative repository pathname, turn it into an absolute 102 * one by tacking on the current root. There is no need to grab it from 103 * the CVS/Root file via the Name_Root() function because by the time 104 * this function is called, we the contents of CVS/Root have already been 105 * compared to original_root and found to match. 106 */ 107 if (!ISABSOLUTE (repos)) 108 { 109 char *newrepos; 110 111 if (current_parsed_root == NULL) 112 { 113 error (0, 0, "in directory `%s:", xupdate_dir); 114 error (0, 0, "must set the CVSROOT environment variable\n"); 115 error (0, 0, "or specify the '-d' option to `%s'.", program_name); 116 error (1, 0, "invalid repository setting"); 117 } 118 if (pathname_levels (repos) > 0) 119 { 120 error (0, 0, "in directory `%s':", xupdate_dir); 121 error (0, 0, "`..'-relative repositories are not supported."); 122 error (1, 0, "invalid source repository"); 123 } 124 newrepos = Xasprintf ("%s/%s", original_parsed_root->directory, repos); 125 free (repos); 126 repos = newrepos; 127 } 128 129 Sanitize_Repository_Name (repos); 130 131 return repos; 132 } 133 134 135 136 /* 137 * Return a pointer to the repository name relative to CVSROOT from a 138 * possibly fully qualified repository 139 */ 140 const char * 141 Short_Repository (const char *repository) 142 { 143 if (repository == NULL) 144 return NULL; 145 146 /* If repository matches CVSroot at the beginning, strip off CVSroot */ 147 /* And skip leading '/' in rep, in case CVSroot ended with '/'. */ 148 if (strncmp (original_parsed_root->directory, repository, 149 strlen (original_parsed_root->directory)) == 0) 150 { 151 const char *rep = repository + strlen (original_parsed_root->directory); 152 return (*rep == '/') ? rep+1 : rep; 153 } 154 else 155 return repository; 156 } 157 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 184 void 185 Sanitize_Repository_Name (char *repository) 186 { 187 size_t len; 188 189 assert (repository != NULL); 190 191 strip_trailing_slashes (repository); 192 193 len = strlen (repository); 194 if (len >= 2 195 && repository[len - 1] == '.' 196 && ISSLASH (repository[len - 2])) 197 { 198 repository[len - 2] = '\0'; 199 } 200 } 201