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