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; 38char cwd[1024] =""; 39int buf_index =1; 40 41int depth = MAXDEPTH; 42char*last_elem = NULL; 43struct stat st; 44 45/* We've already done it */ 46if(path == buf || path == next_buf) 47return path; 48 49if(!*path) { 50if(die_on_error) 51die("The empty string is not a valid path"); 52else 53goto error_out; 54} 55 56if(strlcpy(buf, path, PATH_MAX) >= PATH_MAX) { 57if(die_on_error) 58die("Too long path: %.*s",60, path); 59else 60goto error_out; 61} 62 63while(depth--) { 64if(!is_directory(buf)) { 65char*last_slash =find_last_dir_sep(buf); 66if(last_slash) { 67 last_elem =xstrdup(last_slash +1); 68 last_slash[1] ='\0'; 69}else{ 70 last_elem =xstrdup(buf); 71*buf ='\0'; 72} 73} 74 75if(*buf) { 76if(!*cwd && !getcwd(cwd,sizeof(cwd))) { 77if(die_on_error) 78die_errno("Could not get current working directory"); 79else 80goto error_out; 81} 82 83if(chdir(buf)) { 84if(die_on_error) 85die_errno("Could not switch to '%s'", buf); 86else 87goto error_out; 88} 89} 90if(!getcwd(buf, PATH_MAX)) { 91if(die_on_error) 92die_errno("Could not get current working directory"); 93else 94goto error_out; 95} 96 97if(last_elem) { 98size_t len =strlen(buf); 99if(len +strlen(last_elem) +2> PATH_MAX) { 100if(die_on_error) 101die("Too long path name: '%s/%s'", 102 buf, last_elem); 103else 104goto error_out; 105} 106if(len && !is_dir_sep(buf[len-1])) 107 buf[len++] ='/'; 108strcpy(buf + len, last_elem); 109free(last_elem); 110 last_elem = NULL; 111} 112 113if(!lstat(buf, &st) &&S_ISLNK(st.st_mode)) { 114 ssize_t len =readlink(buf, next_buf, PATH_MAX); 115if(len <0) { 116if(die_on_error) 117die_errno("Invalid symlink '%s'", buf); 118else 119goto error_out; 120} 121if(PATH_MAX <= len) { 122if(die_on_error) 123die("symbolic link too long:%s", buf); 124else 125goto error_out; 126} 127 next_buf[len] ='\0'; 128 buf = next_buf; 129 buf_index =1- buf_index; 130 next_buf = bufs[buf_index]; 131}else 132break; 133} 134 135 retval = buf; 136error_out: 137free(last_elem); 138if(*cwd &&chdir(cwd)) 139die_errno("Could not change back to '%s'", cwd); 140 141return retval; 142} 143 144const char*real_path(const char*path) 145{ 146returnreal_path_internal(path,1); 147} 148 149static const char*get_pwd_cwd(void) 150{ 151static char cwd[PATH_MAX +1]; 152char*pwd; 153struct stat cwd_stat, pwd_stat; 154if(getcwd(cwd, PATH_MAX) == NULL) 155return NULL; 156 pwd =getenv("PWD"); 157if(pwd &&strcmp(pwd, cwd)) { 158stat(cwd, &cwd_stat); 159if((cwd_stat.st_dev || cwd_stat.st_ino) && 160!stat(pwd, &pwd_stat) && 161 pwd_stat.st_dev == cwd_stat.st_dev && 162 pwd_stat.st_ino == cwd_stat.st_ino) { 163strlcpy(cwd, pwd, PATH_MAX); 164} 165} 166return cwd; 167} 168 169/* 170 * Use this to get an absolute path from a relative one. If you want 171 * to resolve links, you should use real_path. 172 * 173 * If the path is already absolute, then return path. As the user is 174 * never meant to free the return value, we're safe. 175 */ 176const char*absolute_path(const char*path) 177{ 178static char buf[PATH_MAX +1]; 179 180if(!*path) { 181die("The empty string is not a valid path"); 182}else if(is_absolute_path(path)) { 183if(strlcpy(buf, path, PATH_MAX) >= PATH_MAX) 184die("Too long path: %.*s",60, path); 185}else{ 186size_t len; 187const char*fmt; 188const char*cwd =get_pwd_cwd(); 189if(!cwd) 190die_errno("Cannot determine the current working directory"); 191 len =strlen(cwd); 192 fmt = (len >0&&is_dir_sep(cwd[len-1])) ?"%s%s":"%s/%s"; 193if(snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX) 194die("Too long path: %.*s",60, path); 195} 196return buf; 197} 198 199/* 200 * Unlike prefix_path, this should be used if the named file does 201 * not have to interact with index entry; i.e. name of a random file 202 * on the filesystem. 203 */ 204const char*prefix_filename(const char*pfx,int pfx_len,const char*arg) 205{ 206static char path[PATH_MAX]; 207#ifndef WIN32 208if(!pfx_len ||is_absolute_path(arg)) 209return arg; 210memcpy(path, pfx, pfx_len); 211strcpy(path + pfx_len, arg); 212#else 213char*p; 214/* don't add prefix to absolute paths, but still replace '\' by '/' */ 215if(is_absolute_path(arg)) 216 pfx_len =0; 217else if(pfx_len) 218memcpy(path, pfx, pfx_len); 219strcpy(path + pfx_len, arg); 220for(p = path + pfx_len; *p; p++) 221if(*p =='\\') 222*p ='/'; 223#endif 224return path; 225}