path.con commit Merge early part of branch 'jc/fetchupload' (210a0be)
   1/*
   2 * I'm tired of doing "vsnprintf()" etc just to open a
   3 * file, so here's a "return static buffer with printf"
   4 * interface for paths.
   5 *
   6 * It's obviously not thread-safe. Sue me. But it's quite
   7 * useful for doing things like
   8 *
   9 *   f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
  10 *
  11 * which is what it's designed for.
  12 */
  13#include "cache.h"
  14#include <pwd.h>
  15
  16static char pathname[PATH_MAX];
  17static char bad_path[] = "/bad-path/";
  18
  19static char *cleanup_path(char *path)
  20{
  21        /* Clean it up */
  22        if (!memcmp(path, "./", 2)) {
  23                path += 2;
  24                while (*path == '/')
  25                        path++;
  26        }
  27        return path;
  28}
  29
  30char *mkpath(const char *fmt, ...)
  31{
  32        va_list args;
  33        unsigned len;
  34
  35        va_start(args, fmt);
  36        len = vsnprintf(pathname, PATH_MAX, fmt, args);
  37        va_end(args);
  38        if (len >= PATH_MAX)
  39                return bad_path;
  40        return cleanup_path(pathname);
  41}
  42
  43char *git_path(const char *fmt, ...)
  44{
  45        const char *git_dir = get_git_dir();
  46        va_list args;
  47        unsigned len;
  48
  49        len = strlen(git_dir);
  50        if (len > PATH_MAX-100)
  51                return bad_path;
  52        memcpy(pathname, git_dir, len);
  53        if (len && git_dir[len-1] != '/')
  54                pathname[len++] = '/';
  55        va_start(args, fmt);
  56        len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
  57        va_end(args);
  58        if (len >= PATH_MAX)
  59                return bad_path;
  60        return cleanup_path(pathname);
  61}
  62
  63
  64/* git_mkstemp() - create tmp file honoring TMPDIR variable */
  65int git_mkstemp(char *path, size_t len, const char *template)
  66{
  67        char *env, *pch = path;
  68
  69        if ((env = getenv("TMPDIR")) == NULL) {
  70                strcpy(pch, "/tmp/");
  71                len -= 5;
  72                pch += 5;
  73        } else {
  74                size_t n = snprintf(pch, len, "%s/", env);
  75
  76                len -= n;
  77                pch += n;
  78        }
  79
  80        safe_strncpy(pch, template, len);
  81
  82        return mkstemp(path);
  83}
  84
  85
  86size_t safe_strncpy(char *dest, const char *src, size_t size)
  87{
  88        size_t ret = strlen(src);
  89
  90        if (size) {
  91                size_t len = (ret >= size) ? size - 1 : ret;
  92                memcpy(dest, src, len);
  93                dest[len] = '\0';
  94        }
  95        return ret;
  96}
  97
  98
  99int validate_symref(const char *path)
 100{
 101        struct stat st;
 102        char *buf, buffer[256];
 103        int len, fd;
 104
 105        if (lstat(path, &st) < 0)
 106                return -1;
 107
 108        /* Make sure it is a "refs/.." symlink */
 109        if (S_ISLNK(st.st_mode)) {
 110                len = readlink(path, buffer, sizeof(buffer)-1);
 111                if (len >= 5 && !memcmp("refs/", buffer, 5))
 112                        return 0;
 113                return -1;
 114        }
 115
 116        /*
 117         * Anything else, just open it and try to see if it is a symbolic ref.
 118         */
 119        fd = open(path, O_RDONLY);
 120        if (fd < 0)
 121                return -1;
 122        len = read(fd, buffer, sizeof(buffer)-1);
 123        close(fd);
 124
 125        /*
 126         * Is it a symbolic ref?
 127         */
 128        if (len < 4 || memcmp("ref:", buffer, 4))
 129                return -1;
 130        buf = buffer + 4;
 131        len -= 4;
 132        while (len && isspace(*buf))
 133                buf++, len--;
 134        if (len >= 5 && !memcmp("refs/", buf, 5))
 135                return 0;
 136        return -1;
 137}
 138
 139static char *user_path(char *buf, char *path, int sz)
 140{
 141        struct passwd *pw;
 142        char *slash;
 143        int len, baselen;
 144
 145        if (!path || path[0] != '~')
 146                return NULL;
 147        path++;
 148        slash = strchr(path, '/');
 149        if (path[0] == '/' || !path[0]) {
 150                pw = getpwuid(getuid());
 151        }
 152        else {
 153                if (slash) {
 154                        *slash = 0;
 155                        pw = getpwnam(path);
 156                        *slash = '/';
 157                }
 158                else
 159                        pw = getpwnam(path);
 160        }
 161        if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
 162                return NULL;
 163        baselen = strlen(pw->pw_dir);
 164        memcpy(buf, pw->pw_dir, baselen);
 165        while ((1 < baselen) && (buf[baselen-1] == '/')) {
 166                buf[baselen-1] = 0;
 167                baselen--;
 168        }
 169        if (slash && slash[1]) {
 170                len = strlen(slash);
 171                if (sz <= baselen + len)
 172                        return NULL;
 173                memcpy(buf + baselen, slash, len + 1);
 174        }
 175        return buf;
 176}
 177
 178/*
 179 * First, one directory to try is determined by the following algorithm.
 180 *
 181 * (0) If "strict" is given, the path is used as given and no DWIM is
 182 *     done. Otherwise:
 183 * (1) "~/path" to mean path under the running user's home directory;
 184 * (2) "~user/path" to mean path under named user's home directory;
 185 * (3) "relative/path" to mean cwd relative directory; or
 186 * (4) "/absolute/path" to mean absolute directory.
 187 *
 188 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
 189 * "%s/.git", "%s.git", "%s" in this order.  The first one that exists is
 190 * what we try.
 191 *
 192 * Second, we try chdir() to that.  Upon failure, we return NULL.
 193 *
 194 * Then, we try if the current directory is a valid git repository.
 195 * Upon failure, we return NULL.
 196 *
 197 * If all goes well, we return the directory we used to chdir() (but
 198 * before ~user is expanded), avoiding getcwd() resolving symbolic
 199 * links.  User relative paths are also returned as they are given,
 200 * except DWIM suffixing.
 201 */
 202char *enter_repo(char *path, int strict)
 203{
 204        static char used_path[PATH_MAX];
 205        static char validated_path[PATH_MAX];
 206
 207        if (!path)
 208                return NULL;
 209
 210        if (!strict) {
 211                static const char *suffix[] = {
 212                        ".git/.git", "/.git", ".git", "", NULL,
 213                };
 214                int len = strlen(path);
 215                int i;
 216                while ((1 < len) && (path[len-1] == '/')) {
 217                        path[len-1] = 0;
 218                        len--;
 219                }
 220                if (PATH_MAX <= len)
 221                        return NULL;
 222                if (path[0] == '~') {
 223                        if (!user_path(used_path, path, PATH_MAX))
 224                                return NULL;
 225                        strcpy(validated_path, path);
 226                        path = used_path;
 227                }
 228                else if (PATH_MAX - 10 < len)
 229                        return NULL;
 230                else {
 231                        path = strcpy(used_path, path);
 232                        strcpy(validated_path, path);
 233                }
 234                len = strlen(path);
 235                for (i = 0; suffix[i]; i++) {
 236                        strcpy(path + len, suffix[i]);
 237                        if (!access(path, F_OK)) {
 238                                strcat(validated_path, suffix[i]);
 239                                break;
 240                        }
 241                }
 242                if (!suffix[i] || chdir(path))
 243                        return NULL;
 244                path = validated_path;
 245        }
 246        else if (chdir(path))
 247                return NULL;
 248
 249        if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
 250            validate_symref("HEAD") == 0) {
 251                putenv("GIT_DIR=.");
 252                check_repository_format();
 253                return path;
 254        }
 255
 256        return NULL;
 257}
 258
 259int adjust_shared_perm(const char *path)
 260{
 261        struct stat st;
 262        int mode;
 263
 264        if (!shared_repository)
 265                return 0;
 266        if (lstat(path, &st) < 0)
 267                return -1;
 268        mode = st.st_mode;
 269        if (mode & S_IRUSR)
 270                mode |= S_IRGRP;
 271        if (mode & S_IWUSR)
 272                mode |= S_IWGRP;
 273        if (mode & S_IXUSR)
 274                mode |= S_IXGRP;
 275        if (S_ISDIR(mode))
 276                mode |= S_ISGID;
 277        if (chmod(path, mode) < 0)
 278                return -2;
 279        return 0;
 280}