1#include"cache.h" 2 3/* 4 * Do not use this for inspecting *tracked* content. When path is a 5 * symlink to a directory, we do not want to say it is a directory when 6 * dealing with tracked content in the working tree. 7 */ 8intis_directory(const char*path) 9{ 10struct stat st; 11return(!stat(path, &st) &&S_ISDIR(st.st_mode)); 12} 13 14/* We allow "recursive" symbolic links. Only within reason, though. */ 15#define MAXDEPTH 5 16 17/* 18 * Return the real path (i.e., absolute path, with symlinks resolved 19 * and extra slashes removed) equivalent to the specified path. (If 20 * you want an absolute path but don't mind links, use 21 * absolute_path().) The return value is a pointer to a static 22 * buffer. 23 * 24 * The input and all intermediate paths must be shorter than MAX_PATH. 25 * The directory part of path (i.e., everything up to the last 26 * dir_sep) must denote a valid, existing directory, but the last 27 * component need not exist. If die_on_error is set, then die with an 28 * informative error message if there is a problem. Otherwise, return 29 * NULL on errors (without generating any output). 30 * 31 * If path is our buffer, then return path, as it's already what the 32 * user wants. 33 */ 34static const char*real_path_internal(const char*path,int die_on_error) 35{ 36static char bufs[2][PATH_MAX +1], *buf = bufs[0], *next_buf = bufs[1]; 37char*retval = NULL; 38 39/* 40 * If we have to temporarily chdir(), store the original CWD 41 * here so that we can chdir() back to it at the end of the 42 * function: 43 */ 44char cwd[1024] =""; 45 46int buf_index =1; 47 48int depth = MAXDEPTH; 49char*last_elem = NULL; 50struct stat st; 51 52/* We've already done it */ 53if(path == buf || path == next_buf) 54return path; 55 56if(!*path) { 57if(die_on_error) 58die("The empty string is not a valid path"); 59else 60goto error_out; 61} 62 63if(strlcpy(buf, path, PATH_MAX) >= PATH_MAX) { 64if(die_on_error) 65die("Too long path: %.*s",60, path); 66else 67goto error_out; 68} 69 70while(depth--) { 71if(!is_directory(buf)) { 72char*last_slash =find_last_dir_sep(buf); 73if(last_slash) { 74 last_elem =xstrdup(last_slash +1); 75 last_slash[1] ='\0'; 76}else{ 77 last_elem =xstrdup(buf); 78*buf ='\0'; 79} 80} 81 82if(*buf) { 83if(!*cwd && !getcwd(cwd,sizeof(cwd))) { 84if(die_on_error) 85die_errno("Could not get current working directory"); 86else 87goto error_out; 88} 89 90if(chdir(buf)) { 91if(die_on_error) 92die_errno("Could not switch to '%s'", buf); 93else 94goto error_out; 95} 96} 97if(!getcwd(buf, PATH_MAX)) { 98if(die_on_error) 99die_errno("Could not get current working directory"); 100else 101goto error_out; 102} 103 104if(last_elem) { 105size_t len =strlen(buf); 106if(len +strlen(last_elem) +2> PATH_MAX) { 107if(die_on_error) 108die("Too long path name: '%s/%s'", 109 buf, last_elem); 110else 111goto error_out; 112} 113if(len && !is_dir_sep(buf[len-1])) 114 buf[len++] ='/'; 115strcpy(buf + len, last_elem); 116free(last_elem); 117 last_elem = NULL; 118} 119 120if(!lstat(buf, &st) &&S_ISLNK(st.st_mode)) { 121 ssize_t len =readlink(buf, next_buf, PATH_MAX); 122if(len <0) { 123if(die_on_error) 124die_errno("Invalid symlink '%s'", buf); 125else 126goto error_out; 127} 128if(PATH_MAX <= len) { 129if(die_on_error) 130die("symbolic link too long:%s", buf); 131else 132goto error_out; 133} 134 next_buf[len] ='\0'; 135 buf = next_buf; 136 buf_index =1- buf_index; 137 next_buf = bufs[buf_index]; 138}else 139break; 140} 141 142 retval = buf; 143error_out: 144free(last_elem); 145if(*cwd &&chdir(cwd)) 146die_errno("Could not change back to '%s'", cwd); 147 148return retval; 149} 150 151const char*real_path(const char*path) 152{ 153returnreal_path_internal(path,1); 154} 155 156const char*real_path_if_valid(const char*path) 157{ 158returnreal_path_internal(path,0); 159} 160 161static const char*get_pwd_cwd(void) 162{ 163static char cwd[PATH_MAX +1]; 164char*pwd; 165struct stat cwd_stat, pwd_stat; 166if(getcwd(cwd, PATH_MAX) == NULL) 167return NULL; 168 pwd =getenv("PWD"); 169if(pwd &&strcmp(pwd, cwd)) { 170stat(cwd, &cwd_stat); 171if((cwd_stat.st_dev || cwd_stat.st_ino) && 172!stat(pwd, &pwd_stat) && 173 pwd_stat.st_dev == cwd_stat.st_dev && 174 pwd_stat.st_ino == cwd_stat.st_ino) { 175strlcpy(cwd, pwd, PATH_MAX); 176} 177} 178return cwd; 179} 180 181/* 182 * Use this to get an absolute path from a relative one. If you want 183 * to resolve links, you should use real_path. 184 * 185 * If the path is already absolute, then return path. As the user is 186 * never meant to free the return value, we're safe. 187 */ 188const char*absolute_path(const char*path) 189{ 190static char buf[PATH_MAX +1]; 191 192if(!*path) { 193die("The empty string is not a valid path"); 194}else if(is_absolute_path(path)) { 195if(strlcpy(buf, path, PATH_MAX) >= PATH_MAX) 196die("Too long path: %.*s",60, path); 197}else{ 198size_t len; 199const char*fmt; 200const char*cwd =get_pwd_cwd(); 201if(!cwd) 202die_errno("Cannot determine the current working directory"); 203 len =strlen(cwd); 204 fmt = (len >0&&is_dir_sep(cwd[len-1])) ?"%s%s":"%s/%s"; 205if(snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX) 206die("Too long path: %.*s",60, path); 207} 208return buf; 209} 210 211/* 212 * Unlike prefix_path, this should be used if the named file does 213 * not have to interact with index entry; i.e. name of a random file 214 * on the filesystem. 215 */ 216const char*prefix_filename(const char*pfx,int pfx_len,const char*arg) 217{ 218static char path[PATH_MAX]; 219#ifndef GIT_WINDOWS_NATIVE 220if(!pfx_len ||is_absolute_path(arg)) 221return arg; 222memcpy(path, pfx, pfx_len); 223strcpy(path + pfx_len, arg); 224#else 225char*p; 226/* don't add prefix to absolute paths, but still replace '\' by '/' */ 227if(is_absolute_path(arg)) 228 pfx_len =0; 229else if(pfx_len) 230memcpy(path, pfx, pfx_len); 231strcpy(path + pfx_len, arg); 232for(p = path + pfx_len; *p; p++) 233if(*p =='\\') 234*p ='/'; 235#endif 236return path; 237}