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