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/* removes the last path component from 'path' except if 'path' is root */ 15static voidstrip_last_component(struct strbuf *path) 16{ 17size_t offset =offset_1st_component(path->buf); 18size_t len = path->len; 19 20/* Find start of the last component */ 21while(offset < len && !is_dir_sep(path->buf[len -1])) 22 len--; 23/* Skip sequences of multiple path-separators */ 24while(offset < len &&is_dir_sep(path->buf[len -1])) 25 len--; 26 27strbuf_setlen(path, len); 28} 29 30/* get (and remove) the next component in 'remaining' and place it in 'next' */ 31static voidget_next_component(struct strbuf *next,struct strbuf *remaining) 32{ 33char*start = NULL; 34char*end = NULL; 35 36strbuf_reset(next); 37 38/* look for the next component */ 39/* Skip sequences of multiple path-separators */ 40for(start = remaining->buf;is_dir_sep(*start); start++) 41;/* nothing */ 42/* Find end of the path component */ 43for(end = start; *end && !is_dir_sep(*end); end++) 44;/* nothing */ 45 46strbuf_add(next, start, end - start); 47/* remove the component from 'remaining' */ 48strbuf_remove(remaining,0, end - remaining->buf); 49} 50 51/* copies root part from remaining to resolved, canonicalizing it on the way */ 52static voidget_root_part(struct strbuf *resolved,struct strbuf *remaining) 53{ 54int offset =offset_1st_component(remaining->buf); 55 56strbuf_reset(resolved); 57strbuf_add(resolved, remaining->buf, offset); 58#ifdef GIT_WINDOWS_NATIVE 59convert_slashes(resolved->buf); 60#endif 61strbuf_remove(remaining,0, offset); 62} 63 64/* We allow "recursive" symbolic links. Only within reason, though. */ 65#ifndef MAXSYMLINKS 66#define MAXSYMLINKS 32 67#endif 68 69/* 70 * Return the real path (i.e., absolute path, with symlinks resolved 71 * and extra slashes removed) equivalent to the specified path. (If 72 * you want an absolute path but don't mind links, use 73 * absolute_path().) Places the resolved realpath in the provided strbuf. 74 * 75 * The directory part of path (i.e., everything up to the last 76 * dir_sep) must denote a valid, existing directory, but the last 77 * component need not exist. If die_on_error is set, then die with an 78 * informative error message if there is a problem. Otherwise, return 79 * NULL on errors (without generating any output). 80 */ 81char*strbuf_realpath(struct strbuf *resolved,const char*path, 82int die_on_error) 83{ 84struct strbuf remaining = STRBUF_INIT; 85struct strbuf next = STRBUF_INIT; 86struct strbuf symlink = STRBUF_INIT; 87char*retval = NULL; 88int num_symlinks =0; 89struct stat st; 90 91if(!*path) { 92if(die_on_error) 93die("The empty string is not a valid path"); 94else 95goto error_out; 96} 97 98strbuf_addstr(&remaining, path); 99get_root_part(resolved, &remaining); 100 101if(!resolved->len) { 102/* relative path; can use CWD as the initial resolved path */ 103if(strbuf_getcwd(resolved)) { 104if(die_on_error) 105die_errno("unable to get current working directory"); 106else 107goto error_out; 108} 109} 110 111/* Iterate over the remaining path components */ 112while(remaining.len >0) { 113get_next_component(&next, &remaining); 114 115if(next.len ==0) { 116continue;/* empty component */ 117}else if(next.len ==1&& !strcmp(next.buf,".")) { 118continue;/* '.' component */ 119}else if(next.len ==2&& !strcmp(next.buf,"..")) { 120/* '..' component; strip the last path component */ 121strip_last_component(resolved); 122continue; 123} 124 125/* append the next component and resolve resultant path */ 126if(!is_dir_sep(resolved->buf[resolved->len -1])) 127strbuf_addch(resolved,'/'); 128strbuf_addbuf(resolved, &next); 129 130if(lstat(resolved->buf, &st)) { 131/* error out unless this was the last component */ 132if(errno != ENOENT || remaining.len) { 133if(die_on_error) 134die_errno("Invalid path '%s'", 135 resolved->buf); 136else 137goto error_out; 138} 139}else if(S_ISLNK(st.st_mode)) { 140 ssize_t len; 141strbuf_reset(&symlink); 142 143if(num_symlinks++ > MAXSYMLINKS) { 144if(die_on_error) 145die("More than%dnested symlinks " 146"on path '%s'", MAXSYMLINKS, path); 147else 148goto error_out; 149} 150 151 len =strbuf_readlink(&symlink, resolved->buf, 152 st.st_size); 153if(len <0) { 154if(die_on_error) 155die_errno("Invalid symlink '%s'", 156 resolved->buf); 157else 158goto error_out; 159} 160 161if(is_absolute_path(symlink.buf)) { 162/* absolute symlink; set resolved to root */ 163get_root_part(resolved, &symlink); 164}else{ 165/* 166 * relative symlink 167 * strip off the last component since it will 168 * be replaced with the contents of the symlink 169 */ 170strip_last_component(resolved); 171} 172 173/* 174 * if there are still remaining components to resolve 175 * then append them to symlink 176 */ 177if(remaining.len) { 178strbuf_addch(&symlink,'/'); 179strbuf_addbuf(&symlink, &remaining); 180} 181 182/* 183 * use the symlink as the remaining components that 184 * need to be resloved 185 */ 186strbuf_swap(&symlink, &remaining); 187} 188} 189 190 retval = resolved->buf; 191 192error_out: 193strbuf_release(&remaining); 194strbuf_release(&next); 195strbuf_release(&symlink); 196 197if(!retval) 198strbuf_reset(resolved); 199 200return retval; 201} 202 203const char*real_path(const char*path) 204{ 205static struct strbuf realpath = STRBUF_INIT; 206returnstrbuf_realpath(&realpath, path,1); 207} 208 209const char*real_path_if_valid(const char*path) 210{ 211static struct strbuf realpath = STRBUF_INIT; 212returnstrbuf_realpath(&realpath, path,0); 213} 214 215char*real_pathdup(const char*path) 216{ 217struct strbuf realpath = STRBUF_INIT; 218char*retval = NULL; 219 220if(strbuf_realpath(&realpath, path,0)) 221 retval =strbuf_detach(&realpath, NULL); 222 223strbuf_release(&realpath); 224 225return retval; 226} 227 228/* 229 * Use this to get an absolute path from a relative one. If you want 230 * to resolve links, you should use real_path. 231 */ 232const char*absolute_path(const char*path) 233{ 234static struct strbuf sb = STRBUF_INIT; 235strbuf_reset(&sb); 236strbuf_add_absolute_path(&sb, path); 237return sb.buf; 238} 239 240/* 241 * Unlike prefix_path, this should be used if the named file does 242 * not have to interact with index entry; i.e. name of a random file 243 * on the filesystem. 244 */ 245const char*prefix_filename(const char*pfx,int pfx_len,const char*arg) 246{ 247static struct strbuf path = STRBUF_INIT; 248#ifndef GIT_WINDOWS_NATIVE 249if(!pfx_len ||is_absolute_path(arg)) 250return arg; 251strbuf_reset(&path); 252strbuf_add(&path, pfx, pfx_len); 253strbuf_addstr(&path, arg); 254#else 255/* don't add prefix to absolute paths, but still replace '\' by '/' */ 256strbuf_reset(&path); 257if(is_absolute_path(arg)) 258 pfx_len =0; 259else if(pfx_len) 260strbuf_add(&path, pfx, pfx_len); 261strbuf_addstr(&path, arg); 262convert_slashes(path.buf + pfx_len); 263#endif 264return path.buf; 265}