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 * Use this to get the real path, i.e. resolve links. If you want an 19 * absolute path but don't mind links, use absolute_path. 20 * 21 * If path is our buffer, then return path, as it's already what the 22 * user wants. 23 */ 24const char*real_path(const char*path) 25{ 26static char bufs[2][PATH_MAX +1], *buf = bufs[0], *next_buf = bufs[1]; 27char cwd[1024] =""; 28int buf_index =1; 29 30int depth = MAXDEPTH; 31char*last_elem = NULL; 32struct stat st; 33 34/* We've already done it */ 35if(path == buf || path == next_buf) 36return path; 37 38if(strlcpy(buf, path, PATH_MAX) >= PATH_MAX) 39die("Too long path: %.*s",60, path); 40 41while(depth--) { 42if(!is_directory(buf)) { 43char*last_slash =find_last_dir_sep(buf); 44if(last_slash) { 45*last_slash ='\0'; 46 last_elem =xstrdup(last_slash +1); 47}else{ 48 last_elem =xstrdup(buf); 49*buf ='\0'; 50} 51} 52 53if(*buf) { 54if(!*cwd && !getcwd(cwd,sizeof(cwd))) 55die_errno("Could not get current working directory"); 56 57if(chdir(buf)) 58die_errno("Could not switch to '%s'", buf); 59} 60if(!getcwd(buf, PATH_MAX)) 61die_errno("Could not get current working directory"); 62 63if(last_elem) { 64size_t len =strlen(buf); 65if(len +strlen(last_elem) +2> PATH_MAX) 66die("Too long path name: '%s/%s'", 67 buf, last_elem); 68if(len && !is_dir_sep(buf[len-1])) 69 buf[len++] ='/'; 70strcpy(buf + len, last_elem); 71free(last_elem); 72 last_elem = NULL; 73} 74 75if(!lstat(buf, &st) &&S_ISLNK(st.st_mode)) { 76 ssize_t len =readlink(buf, next_buf, PATH_MAX); 77if(len <0) 78die_errno("Invalid symlink '%s'", buf); 79if(PATH_MAX <= len) 80die("symbolic link too long:%s", buf); 81 next_buf[len] ='\0'; 82 buf = next_buf; 83 buf_index =1- buf_index; 84 next_buf = bufs[buf_index]; 85}else 86break; 87} 88 89if(*cwd &&chdir(cwd)) 90die_errno("Could not change back to '%s'", cwd); 91 92return buf; 93} 94 95static const char*get_pwd_cwd(void) 96{ 97static char cwd[PATH_MAX +1]; 98char*pwd; 99struct stat cwd_stat, pwd_stat; 100if(getcwd(cwd, PATH_MAX) == NULL) 101return NULL; 102 pwd =getenv("PWD"); 103if(pwd &&strcmp(pwd, cwd)) { 104stat(cwd, &cwd_stat); 105if((cwd_stat.st_dev || cwd_stat.st_ino) && 106!stat(pwd, &pwd_stat) && 107 pwd_stat.st_dev == cwd_stat.st_dev && 108 pwd_stat.st_ino == cwd_stat.st_ino) { 109strlcpy(cwd, pwd, PATH_MAX); 110} 111} 112return cwd; 113} 114 115/* 116 * Use this to get an absolute path from a relative one. If you want 117 * to resolve links, you should use real_path. 118 * 119 * If the path is already absolute, then return path. As the user is 120 * never meant to free the return value, we're safe. 121 */ 122const char*absolute_path(const char*path) 123{ 124static char buf[PATH_MAX +1]; 125 126if(is_absolute_path(path)) { 127if(strlcpy(buf, path, PATH_MAX) >= PATH_MAX) 128die("Too long path: %.*s",60, path); 129}else{ 130size_t len; 131const char*fmt; 132const char*cwd =get_pwd_cwd(); 133if(!cwd) 134die_errno("Cannot determine the current working directory"); 135 len =strlen(cwd); 136 fmt = (len >0&&is_dir_sep(cwd[len-1])) ?"%s%s":"%s/%s"; 137if(snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX) 138die("Too long path: %.*s",60, path); 139} 140return buf; 141} 142 143/* 144 * Unlike prefix_path, this should be used if the named file does 145 * not have to interact with index entry; i.e. name of a random file 146 * on the filesystem. 147 */ 148const char*prefix_filename(const char*pfx,int pfx_len,const char*arg) 149{ 150static char path[PATH_MAX]; 151#ifndef WIN32 152if(!pfx_len ||is_absolute_path(arg)) 153return arg; 154memcpy(path, pfx, pfx_len); 155strcpy(path + pfx_len, arg); 156#else 157char*p; 158/* don't add prefix to absolute paths, but still replace '\' by '/' */ 159if(is_absolute_path(arg)) 160 pfx_len =0; 161else if(pfx_len) 162memcpy(path, pfx, pfx_len); 163strcpy(path + pfx_len, arg); 164for(p = path + pfx_len; *p; p++) 165if(*p =='\\') 166*p ='/'; 167#endif 168return path; 169}