path.con commit use 'tree-ish' instead of 'treeish' (bb8040f)
   1/*
   2 * Utilities for paths and pathnames
   3 */
   4#include "cache.h"
   5#include "strbuf.h"
   6#include "string-list.h"
   7
   8static int get_st_mode_bits(const char *path, int *mode)
   9{
  10        struct stat st;
  11        if (lstat(path, &st) < 0)
  12                return -1;
  13        *mode = st.st_mode;
  14        return 0;
  15}
  16
  17static char bad_path[] = "/bad-path/";
  18
  19static char *get_pathname(void)
  20{
  21        static char pathname_array[4][PATH_MAX];
  22        static int index;
  23        return pathname_array[3 & ++index];
  24}
  25
  26static char *cleanup_path(char *path)
  27{
  28        /* Clean it up */
  29        if (!memcmp(path, "./", 2)) {
  30                path += 2;
  31                while (*path == '/')
  32                        path++;
  33        }
  34        return path;
  35}
  36
  37char *mksnpath(char *buf, size_t n, const char *fmt, ...)
  38{
  39        va_list args;
  40        unsigned len;
  41
  42        va_start(args, fmt);
  43        len = vsnprintf(buf, n, fmt, args);
  44        va_end(args);
  45        if (len >= n) {
  46                strlcpy(buf, bad_path, n);
  47                return buf;
  48        }
  49        return cleanup_path(buf);
  50}
  51
  52static char *vsnpath(char *buf, size_t n, const char *fmt, va_list args)
  53{
  54        const char *git_dir = get_git_dir();
  55        size_t len;
  56
  57        len = strlen(git_dir);
  58        if (n < len + 1)
  59                goto bad;
  60        memcpy(buf, git_dir, len);
  61        if (len && !is_dir_sep(git_dir[len-1]))
  62                buf[len++] = '/';
  63        len += vsnprintf(buf + len, n - len, fmt, args);
  64        if (len >= n)
  65                goto bad;
  66        return cleanup_path(buf);
  67bad:
  68        strlcpy(buf, bad_path, n);
  69        return buf;
  70}
  71
  72char *git_snpath(char *buf, size_t n, const char *fmt, ...)
  73{
  74        char *ret;
  75        va_list args;
  76        va_start(args, fmt);
  77        ret = vsnpath(buf, n, fmt, args);
  78        va_end(args);
  79        return ret;
  80}
  81
  82char *git_pathdup(const char *fmt, ...)
  83{
  84        char path[PATH_MAX], *ret;
  85        va_list args;
  86        va_start(args, fmt);
  87        ret = vsnpath(path, sizeof(path), fmt, args);
  88        va_end(args);
  89        return xstrdup(ret);
  90}
  91
  92char *mkpathdup(const char *fmt, ...)
  93{
  94        char *path;
  95        struct strbuf sb = STRBUF_INIT;
  96        va_list args;
  97
  98        va_start(args, fmt);
  99        strbuf_vaddf(&sb, fmt, args);
 100        va_end(args);
 101        path = xstrdup(cleanup_path(sb.buf));
 102
 103        strbuf_release(&sb);
 104        return path;
 105}
 106
 107char *mkpath(const char *fmt, ...)
 108{
 109        va_list args;
 110        unsigned len;
 111        char *pathname = get_pathname();
 112
 113        va_start(args, fmt);
 114        len = vsnprintf(pathname, PATH_MAX, fmt, args);
 115        va_end(args);
 116        if (len >= PATH_MAX)
 117                return bad_path;
 118        return cleanup_path(pathname);
 119}
 120
 121char *git_path(const char *fmt, ...)
 122{
 123        char *pathname = get_pathname();
 124        va_list args;
 125        char *ret;
 126
 127        va_start(args, fmt);
 128        ret = vsnpath(pathname, PATH_MAX, fmt, args);
 129        va_end(args);
 130        return ret;
 131}
 132
 133void home_config_paths(char **global, char **xdg, char *file)
 134{
 135        char *xdg_home = getenv("XDG_CONFIG_HOME");
 136        char *home = getenv("HOME");
 137        char *to_free = NULL;
 138
 139        if (!home) {
 140                if (global)
 141                        *global = NULL;
 142        } else {
 143                if (!xdg_home) {
 144                        to_free = mkpathdup("%s/.config", home);
 145                        xdg_home = to_free;
 146                }
 147                if (global)
 148                        *global = mkpathdup("%s/.gitconfig", home);
 149        }
 150
 151        if (!xdg_home)
 152                *xdg = NULL;
 153        else
 154                *xdg = mkpathdup("%s/git/%s", xdg_home, file);
 155
 156        free(to_free);
 157}
 158
 159char *git_path_submodule(const char *path, const char *fmt, ...)
 160{
 161        char *pathname = get_pathname();
 162        struct strbuf buf = STRBUF_INIT;
 163        const char *git_dir;
 164        va_list args;
 165        unsigned len;
 166
 167        len = strlen(path);
 168        if (len > PATH_MAX-100)
 169                return bad_path;
 170
 171        strbuf_addstr(&buf, path);
 172        if (len && path[len-1] != '/')
 173                strbuf_addch(&buf, '/');
 174        strbuf_addstr(&buf, ".git");
 175
 176        git_dir = read_gitfile(buf.buf);
 177        if (git_dir) {
 178                strbuf_reset(&buf);
 179                strbuf_addstr(&buf, git_dir);
 180        }
 181        strbuf_addch(&buf, '/');
 182
 183        if (buf.len >= PATH_MAX)
 184                return bad_path;
 185        memcpy(pathname, buf.buf, buf.len + 1);
 186
 187        strbuf_release(&buf);
 188        len = strlen(pathname);
 189
 190        va_start(args, fmt);
 191        len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
 192        va_end(args);
 193        if (len >= PATH_MAX)
 194                return bad_path;
 195        return cleanup_path(pathname);
 196}
 197
 198int validate_headref(const char *path)
 199{
 200        struct stat st;
 201        char *buf, buffer[256];
 202        unsigned char sha1[20];
 203        int fd;
 204        ssize_t len;
 205
 206        if (lstat(path, &st) < 0)
 207                return -1;
 208
 209        /* Make sure it is a "refs/.." symlink */
 210        if (S_ISLNK(st.st_mode)) {
 211                len = readlink(path, buffer, sizeof(buffer)-1);
 212                if (len >= 5 && !memcmp("refs/", buffer, 5))
 213                        return 0;
 214                return -1;
 215        }
 216
 217        /*
 218         * Anything else, just open it and try to see if it is a symbolic ref.
 219         */
 220        fd = open(path, O_RDONLY);
 221        if (fd < 0)
 222                return -1;
 223        len = read_in_full(fd, buffer, sizeof(buffer)-1);
 224        close(fd);
 225
 226        /*
 227         * Is it a symbolic ref?
 228         */
 229        if (len < 4)
 230                return -1;
 231        if (!memcmp("ref:", buffer, 4)) {
 232                buf = buffer + 4;
 233                len -= 4;
 234                while (len && isspace(*buf))
 235                        buf++, len--;
 236                if (len >= 5 && !memcmp("refs/", buf, 5))
 237                        return 0;
 238        }
 239
 240        /*
 241         * Is this a detached HEAD?
 242         */
 243        if (!get_sha1_hex(buffer, sha1))
 244                return 0;
 245
 246        return -1;
 247}
 248
 249static struct passwd *getpw_str(const char *username, size_t len)
 250{
 251        struct passwd *pw;
 252        char *username_z = xmalloc(len + 1);
 253        memcpy(username_z, username, len);
 254        username_z[len] = '\0';
 255        pw = getpwnam(username_z);
 256        free(username_z);
 257        return pw;
 258}
 259
 260/*
 261 * Return a string with ~ and ~user expanded via getpw*.  If buf != NULL,
 262 * then it is a newly allocated string. Returns NULL on getpw failure or
 263 * if path is NULL.
 264 */
 265char *expand_user_path(const char *path)
 266{
 267        struct strbuf user_path = STRBUF_INIT;
 268        const char *first_slash = strchrnul(path, '/');
 269        const char *to_copy = path;
 270
 271        if (path == NULL)
 272                goto return_null;
 273        if (path[0] == '~') {
 274                const char *username = path + 1;
 275                size_t username_len = first_slash - username;
 276                if (username_len == 0) {
 277                        const char *home = getenv("HOME");
 278                        if (!home)
 279                                goto return_null;
 280                        strbuf_add(&user_path, home, strlen(home));
 281                } else {
 282                        struct passwd *pw = getpw_str(username, username_len);
 283                        if (!pw)
 284                                goto return_null;
 285                        strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
 286                }
 287                to_copy = first_slash;
 288        }
 289        strbuf_add(&user_path, to_copy, strlen(to_copy));
 290        return strbuf_detach(&user_path, NULL);
 291return_null:
 292        strbuf_release(&user_path);
 293        return NULL;
 294}
 295
 296/*
 297 * First, one directory to try is determined by the following algorithm.
 298 *
 299 * (0) If "strict" is given, the path is used as given and no DWIM is
 300 *     done. Otherwise:
 301 * (1) "~/path" to mean path under the running user's home directory;
 302 * (2) "~user/path" to mean path under named user's home directory;
 303 * (3) "relative/path" to mean cwd relative directory; or
 304 * (4) "/absolute/path" to mean absolute directory.
 305 *
 306 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
 307 * "%s/.git", "%s.git", "%s" in this order.  The first one that exists is
 308 * what we try.
 309 *
 310 * Second, we try chdir() to that.  Upon failure, we return NULL.
 311 *
 312 * Then, we try if the current directory is a valid git repository.
 313 * Upon failure, we return NULL.
 314 *
 315 * If all goes well, we return the directory we used to chdir() (but
 316 * before ~user is expanded), avoiding getcwd() resolving symbolic
 317 * links.  User relative paths are also returned as they are given,
 318 * except DWIM suffixing.
 319 */
 320const char *enter_repo(const char *path, int strict)
 321{
 322        static char used_path[PATH_MAX];
 323        static char validated_path[PATH_MAX];
 324
 325        if (!path)
 326                return NULL;
 327
 328        if (!strict) {
 329                static const char *suffix[] = {
 330                        "/.git", "", ".git/.git", ".git", NULL,
 331                };
 332                const char *gitfile;
 333                int len = strlen(path);
 334                int i;
 335                while ((1 < len) && (path[len-1] == '/'))
 336                        len--;
 337
 338                if (PATH_MAX <= len)
 339                        return NULL;
 340                strncpy(used_path, path, len); used_path[len] = 0 ;
 341                strcpy(validated_path, used_path);
 342
 343                if (used_path[0] == '~') {
 344                        char *newpath = expand_user_path(used_path);
 345                        if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
 346                                free(newpath);
 347                                return NULL;
 348                        }
 349                        /*
 350                         * Copy back into the static buffer. A pity
 351                         * since newpath was not bounded, but other
 352                         * branches of the if are limited by PATH_MAX
 353                         * anyway.
 354                         */
 355                        strcpy(used_path, newpath); free(newpath);
 356                }
 357                else if (PATH_MAX - 10 < len)
 358                        return NULL;
 359                len = strlen(used_path);
 360                for (i = 0; suffix[i]; i++) {
 361                        struct stat st;
 362                        strcpy(used_path + len, suffix[i]);
 363                        if (!stat(used_path, &st) &&
 364                            (S_ISREG(st.st_mode) ||
 365                            (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
 366                                strcat(validated_path, suffix[i]);
 367                                break;
 368                        }
 369                }
 370                if (!suffix[i])
 371                        return NULL;
 372                gitfile = read_gitfile(used_path) ;
 373                if (gitfile)
 374                        strcpy(used_path, gitfile);
 375                if (chdir(used_path))
 376                        return NULL;
 377                path = validated_path;
 378        }
 379        else if (chdir(path))
 380                return NULL;
 381
 382        if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
 383            validate_headref("HEAD") == 0) {
 384                set_git_dir(".");
 385                check_repository_format();
 386                return path;
 387        }
 388
 389        return NULL;
 390}
 391
 392static int calc_shared_perm(int mode)
 393{
 394        int tweak;
 395
 396        if (shared_repository < 0)
 397                tweak = -shared_repository;
 398        else
 399                tweak = shared_repository;
 400
 401        if (!(mode & S_IWUSR))
 402                tweak &= ~0222;
 403        if (mode & S_IXUSR)
 404                /* Copy read bits to execute bits */
 405                tweak |= (tweak & 0444) >> 2;
 406        if (shared_repository < 0)
 407                mode = (mode & ~0777) | tweak;
 408        else
 409                mode |= tweak;
 410
 411        return mode;
 412}
 413
 414
 415int adjust_shared_perm(const char *path)
 416{
 417        int old_mode, new_mode;
 418
 419        if (!shared_repository)
 420                return 0;
 421        if (get_st_mode_bits(path, &old_mode) < 0)
 422                return -1;
 423
 424        new_mode = calc_shared_perm(old_mode);
 425        if (S_ISDIR(old_mode)) {
 426                /* Copy read bits to execute bits */
 427                new_mode |= (new_mode & 0444) >> 2;
 428                new_mode |= FORCE_DIR_SET_GID;
 429        }
 430
 431        if (((old_mode ^ new_mode) & ~S_IFMT) &&
 432                        chmod(path, (new_mode & ~S_IFMT)) < 0)
 433                return -2;
 434        return 0;
 435}
 436
 437/*
 438 * Give path as relative to prefix.
 439 *
 440 * The strbuf may or may not be used, so do not assume it contains the
 441 * returned path.
 442 */
 443const char *relative_path(const char *in, const char *prefix,
 444                          struct strbuf *sb)
 445{
 446        int in_len = in ? strlen(in) : 0;
 447        int prefix_len = prefix ? strlen(prefix) : 0;
 448        int in_off = 0;
 449        int prefix_off = 0;
 450        int i = 0, j = 0;
 451
 452        if (!in_len)
 453                return "./";
 454        else if (!prefix_len)
 455                return in;
 456
 457        while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
 458                if (is_dir_sep(prefix[i])) {
 459                        while (is_dir_sep(prefix[i]))
 460                                i++;
 461                        while (is_dir_sep(in[j]))
 462                                j++;
 463                        prefix_off = i;
 464                        in_off = j;
 465                } else {
 466                        i++;
 467                        j++;
 468                }
 469        }
 470
 471        if (
 472            /* "prefix" seems like prefix of "in" */
 473            i >= prefix_len &&
 474            /*
 475             * but "/foo" is not a prefix of "/foobar"
 476             * (i.e. prefix not end with '/')
 477             */
 478            prefix_off < prefix_len) {
 479                if (j >= in_len) {
 480                        /* in="/a/b", prefix="/a/b" */
 481                        in_off = in_len;
 482                } else if (is_dir_sep(in[j])) {
 483                        /* in="/a/b/c", prefix="/a/b" */
 484                        while (is_dir_sep(in[j]))
 485                                j++;
 486                        in_off = j;
 487                } else {
 488                        /* in="/a/bbb/c", prefix="/a/b" */
 489                        i = prefix_off;
 490                }
 491        } else if (
 492                   /* "in" is short than "prefix" */
 493                   j >= in_len &&
 494                   /* "in" not end with '/' */
 495                   in_off < in_len) {
 496                if (is_dir_sep(prefix[i])) {
 497                        /* in="/a/b", prefix="/a/b/c/" */
 498                        while (is_dir_sep(prefix[i]))
 499                                i++;
 500                        in_off = in_len;
 501                }
 502        }
 503        in += in_off;
 504        in_len -= in_off;
 505
 506        if (i >= prefix_len) {
 507                if (!in_len)
 508                        return "./";
 509                else
 510                        return in;
 511        }
 512
 513        strbuf_reset(sb);
 514        strbuf_grow(sb, in_len);
 515
 516        while (i < prefix_len) {
 517                if (is_dir_sep(prefix[i])) {
 518                        strbuf_addstr(sb, "../");
 519                        while (is_dir_sep(prefix[i]))
 520                                i++;
 521                        continue;
 522                }
 523                i++;
 524        }
 525        if (!is_dir_sep(prefix[prefix_len - 1]))
 526                strbuf_addstr(sb, "../");
 527
 528        strbuf_addstr(sb, in);
 529
 530        return sb->buf;
 531}
 532
 533/*
 534 * It is okay if dst == src, but they should not overlap otherwise.
 535 *
 536 * Performs the following normalizations on src, storing the result in dst:
 537 * - Ensures that components are separated by '/' (Windows only)
 538 * - Squashes sequences of '/'.
 539 * - Removes "." components.
 540 * - Removes ".." components, and the components the precede them.
 541 * Returns failure (non-zero) if a ".." component appears as first path
 542 * component anytime during the normalization. Otherwise, returns success (0).
 543 *
 544 * Note that this function is purely textual.  It does not follow symlinks,
 545 * verify the existence of the path, or make any system calls.
 546 */
 547int normalize_path_copy(char *dst, const char *src)
 548{
 549        char *dst0;
 550
 551        if (has_dos_drive_prefix(src)) {
 552                *dst++ = *src++;
 553                *dst++ = *src++;
 554        }
 555        dst0 = dst;
 556
 557        if (is_dir_sep(*src)) {
 558                *dst++ = '/';
 559                while (is_dir_sep(*src))
 560                        src++;
 561        }
 562
 563        for (;;) {
 564                char c = *src;
 565
 566                /*
 567                 * A path component that begins with . could be
 568                 * special:
 569                 * (1) "." and ends   -- ignore and terminate.
 570                 * (2) "./"           -- ignore them, eat slash and continue.
 571                 * (3) ".." and ends  -- strip one and terminate.
 572                 * (4) "../"          -- strip one, eat slash and continue.
 573                 */
 574                if (c == '.') {
 575                        if (!src[1]) {
 576                                /* (1) */
 577                                src++;
 578                        } else if (is_dir_sep(src[1])) {
 579                                /* (2) */
 580                                src += 2;
 581                                while (is_dir_sep(*src))
 582                                        src++;
 583                                continue;
 584                        } else if (src[1] == '.') {
 585                                if (!src[2]) {
 586                                        /* (3) */
 587                                        src += 2;
 588                                        goto up_one;
 589                                } else if (is_dir_sep(src[2])) {
 590                                        /* (4) */
 591                                        src += 3;
 592                                        while (is_dir_sep(*src))
 593                                                src++;
 594                                        goto up_one;
 595                                }
 596                        }
 597                }
 598
 599                /* copy up to the next '/', and eat all '/' */
 600                while ((c = *src++) != '\0' && !is_dir_sep(c))
 601                        *dst++ = c;
 602                if (is_dir_sep(c)) {
 603                        *dst++ = '/';
 604                        while (is_dir_sep(c))
 605                                c = *src++;
 606                        src--;
 607                } else if (!c)
 608                        break;
 609                continue;
 610
 611        up_one:
 612                /*
 613                 * dst0..dst is prefix portion, and dst[-1] is '/';
 614                 * go up one level.
 615                 */
 616                dst--;  /* go to trailing '/' */
 617                if (dst <= dst0)
 618                        return -1;
 619                /* Windows: dst[-1] cannot be backslash anymore */
 620                while (dst0 < dst && dst[-1] != '/')
 621                        dst--;
 622        }
 623        *dst = '\0';
 624        return 0;
 625}
 626
 627/*
 628 * path = Canonical absolute path
 629 * prefixes = string_list containing normalized, absolute paths without
 630 * trailing slashes (except for the root directory, which is denoted by "/").
 631 *
 632 * Determines, for each path in prefixes, whether the "prefix"
 633 * is an ancestor directory of path.  Returns the length of the longest
 634 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
 635 * is an ancestor.  (Note that this means 0 is returned if prefixes is
 636 * ["/"].) "/foo" is not considered an ancestor of "/foobar".  Directories
 637 * are not considered to be their own ancestors.  path must be in a
 638 * canonical form: empty components, or "." or ".." components are not
 639 * allowed.
 640 */
 641int longest_ancestor_length(const char *path, struct string_list *prefixes)
 642{
 643        int i, max_len = -1;
 644
 645        if (!strcmp(path, "/"))
 646                return -1;
 647
 648        for (i = 0; i < prefixes->nr; i++) {
 649                const char *ceil = prefixes->items[i].string;
 650                int len = strlen(ceil);
 651
 652                if (len == 1 && ceil[0] == '/')
 653                        len = 0; /* root matches anything, with length 0 */
 654                else if (!strncmp(path, ceil, len) && path[len] == '/')
 655                        ; /* match of length len */
 656                else
 657                        continue; /* no match */
 658
 659                if (len > max_len)
 660                        max_len = len;
 661        }
 662
 663        return max_len;
 664}
 665
 666/* strip arbitrary amount of directory separators at end of path */
 667static inline int chomp_trailing_dir_sep(const char *path, int len)
 668{
 669        while (len && is_dir_sep(path[len - 1]))
 670                len--;
 671        return len;
 672}
 673
 674/*
 675 * If path ends with suffix (complete path components), returns the
 676 * part before suffix (sans trailing directory separators).
 677 * Otherwise returns NULL.
 678 */
 679char *strip_path_suffix(const char *path, const char *suffix)
 680{
 681        int path_len = strlen(path), suffix_len = strlen(suffix);
 682
 683        while (suffix_len) {
 684                if (!path_len)
 685                        return NULL;
 686
 687                if (is_dir_sep(path[path_len - 1])) {
 688                        if (!is_dir_sep(suffix[suffix_len - 1]))
 689                                return NULL;
 690                        path_len = chomp_trailing_dir_sep(path, path_len);
 691                        suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
 692                }
 693                else if (path[--path_len] != suffix[--suffix_len])
 694                        return NULL;
 695        }
 696
 697        if (path_len && !is_dir_sep(path[path_len - 1]))
 698                return NULL;
 699        return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
 700}
 701
 702int daemon_avoid_alias(const char *p)
 703{
 704        int sl, ndot;
 705
 706        /*
 707         * This resurrects the belts and suspenders paranoia check by HPA
 708         * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
 709         * does not do getcwd() based path canonicalization.
 710         *
 711         * sl becomes true immediately after seeing '/' and continues to
 712         * be true as long as dots continue after that without intervening
 713         * non-dot character.
 714         */
 715        if (!p || (*p != '/' && *p != '~'))
 716                return -1;
 717        sl = 1; ndot = 0;
 718        p++;
 719
 720        while (1) {
 721                char ch = *p++;
 722                if (sl) {
 723                        if (ch == '.')
 724                                ndot++;
 725                        else if (ch == '/') {
 726                                if (ndot < 3)
 727                                        /* reject //, /./ and /../ */
 728                                        return -1;
 729                                ndot = 0;
 730                        }
 731                        else if (ch == 0) {
 732                                if (0 < ndot && ndot < 3)
 733                                        /* reject /.$ and /..$ */
 734                                        return -1;
 735                                return 0;
 736                        }
 737                        else
 738                                sl = ndot = 0;
 739                }
 740                else if (ch == 0)
 741                        return 0;
 742                else if (ch == '/') {
 743                        sl = 1;
 744                        ndot = 0;
 745                }
 746        }
 747}
 748
 749int offset_1st_component(const char *path)
 750{
 751        if (has_dos_drive_prefix(path))
 752                return 2 + is_dir_sep(path[2]);
 753        return is_dir_sep(path[0]);
 754}