84b3272d94dbc301131d55660be41d5150a1cdef
   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
  86char *safe_strncpy(char *dest, const char *src, size_t n)
  87{
  88        strncpy(dest, src, n);
  89        dest[n - 1] = '\0';
  90
  91        return dest;
  92}
  93
  94int validate_symref(const char *path)
  95{
  96        struct stat st;
  97        char *buf, buffer[256];
  98        int len, fd;
  99
 100        if (lstat(path, &st) < 0)
 101                return -1;
 102
 103        /* Make sure it is a "refs/.." symlink */
 104        if (S_ISLNK(st.st_mode)) {
 105                len = readlink(path, buffer, sizeof(buffer)-1);
 106                if (len >= 5 && !memcmp("refs/", buffer, 5))
 107                        return 0;
 108                return -1;
 109        }
 110
 111        /*
 112         * Anything else, just open it and try to see if it is a symbolic ref.
 113         */
 114        fd = open(path, O_RDONLY);
 115        if (fd < 0)
 116                return -1;
 117        len = read(fd, buffer, sizeof(buffer)-1);
 118        close(fd);
 119
 120        /*
 121         * Is it a symbolic ref?
 122         */
 123        if (len < 4 || memcmp("ref:", buffer, 4))
 124                return -1;
 125        buf = buffer + 4;
 126        len -= 4;
 127        while (len && isspace(*buf))
 128                buf++, len--;
 129        if (len >= 5 && !memcmp("refs/", buf, 5))
 130                return 0;
 131        return -1;
 132}
 133
 134static char *current_dir(void)
 135{
 136        return getcwd(pathname, sizeof(pathname));
 137}
 138
 139static int user_chdir(char *path)
 140{
 141        char *dir = path;
 142
 143        if(*dir == '~') {               /* user-relative path */
 144                struct passwd *pw;
 145                char *slash = strchr(dir, '/');
 146
 147                dir++;
 148                /* '~/' and '~' (no slash) means users own home-dir */
 149                if(!*dir || *dir == '/')
 150                        pw = getpwuid(getuid());
 151                else {
 152                        if (slash) {
 153                                *slash = '\0';
 154                                pw = getpwnam(dir);
 155                                *slash = '/';
 156                        }
 157                        else
 158                                pw = getpwnam(dir);
 159                }
 160
 161                /* make sure we got something back that we can chdir() to */
 162                if(!pw || chdir(pw->pw_dir) < 0)
 163                        return -1;
 164
 165                if(!slash || !slash[1]) /* no path following username */
 166                        return 0;
 167
 168                dir = slash + 1;
 169        }
 170
 171        /* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
 172        if(chdir(dir) < 0)
 173                return -1;
 174
 175        return 0;
 176}
 177
 178char *enter_repo(char *path, int strict)
 179{
 180        if(!path)
 181                return NULL;
 182
 183        if (strict) {
 184                if((path[0] != '/') || chdir(path) < 0)
 185                        return NULL;
 186        }
 187        else {
 188                if (!*path)
 189                        ; /* happy -- no chdir */
 190                else if (!user_chdir(path))
 191                        ; /* happy -- as given */
 192                else if (!user_chdir(mkpath("%s.git", path)))
 193                        ; /* happy -- uemacs --> uemacs.git */
 194                else
 195                        return NULL;
 196                (void)chdir(".git");
 197        }
 198
 199        if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
 200           validate_symref("HEAD") == 0) {
 201                putenv("GIT_DIR=.");
 202                return current_dir();
 203        }
 204
 205        return NULL;
 206}