refs.con commit Fix silly thinko in "head_ref()" (2f34ba3)
   1#include "refs.h"
   2#include "cache.h"
   3
   4#include <errno.h>
   5
   6static int read_ref(const char *path, unsigned char *sha1)
   7{
   8        int ret = -1;
   9        int fd = open(path, O_RDONLY);
  10
  11        if (fd >= 0) {
  12                char buffer[60];
  13                if (read(fd, buffer, sizeof(buffer)) >= 40)
  14                        ret = get_sha1_hex(buffer, sha1);
  15                close(fd);
  16        }
  17        return ret;
  18}
  19
  20static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
  21{
  22        int retval = 0;
  23        DIR *dir = opendir(base);
  24
  25        if (dir) {
  26                struct dirent *de;
  27                int baselen = strlen(base);
  28                char *path = xmalloc(baselen + 257);
  29
  30                if (!strncmp(base, "./", 2)) {
  31                        base += 2;
  32                        baselen -= 2;
  33                }
  34                memcpy(path, base, baselen);
  35                if (baselen && base[baselen-1] != '/')
  36                        path[baselen++] = '/';
  37
  38                while ((de = readdir(dir)) != NULL) {
  39                        unsigned char sha1[20];
  40                        struct stat st;
  41                        int namelen;
  42
  43                        if (de->d_name[0] == '.')
  44                                continue;
  45                        namelen = strlen(de->d_name);
  46                        if (namelen > 255)
  47                                continue;
  48                        memcpy(path + baselen, de->d_name, namelen+1);
  49                        if (lstat(path, &st) < 0)
  50                                continue;
  51                        if (S_ISDIR(st.st_mode)) {
  52                                retval = do_for_each_ref(path, fn);
  53                                if (retval)
  54                                        break;
  55                                continue;
  56                        }
  57                        if (read_ref(path, sha1) < 0)
  58                                continue;
  59                        if (!has_sha1_file(sha1))
  60                                continue;
  61                        retval = fn(path, sha1);
  62                        if (retval)
  63                                break;
  64                }
  65                free(path);
  66                closedir(dir);
  67        }
  68        return retval;
  69}
  70
  71int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
  72{
  73        unsigned char sha1[20];
  74        const char *headpath = git_path("HEAD");
  75        if (!read_ref(headpath, sha1))
  76                return fn(headpath, sha1);
  77        return 0;
  78}
  79
  80int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
  81{
  82        return do_for_each_ref(get_refs_directory(), fn);
  83}
  84
  85static char *ref_file_name(const char *ref)
  86{
  87        char *base = get_refs_directory();
  88        int baselen = strlen(base);
  89        int reflen = strlen(ref);
  90        char *ret = xmalloc(baselen + 2 + reflen);
  91        sprintf(ret, "%s/%s", base, ref);
  92        return ret;
  93}
  94
  95static char *ref_lock_file_name(const char *ref)
  96{
  97        char *base = get_refs_directory();
  98        int baselen = strlen(base);
  99        int reflen = strlen(ref);
 100        char *ret = xmalloc(baselen + 7 + reflen);
 101        sprintf(ret, "%s/%s.lock", base, ref);
 102        return ret;
 103}
 104
 105static int read_ref_file(const char *filename, unsigned char *sha1) {
 106        int fd = open(filename, O_RDONLY);
 107        char hex[41];
 108        if (fd < 0) {
 109                return error("Couldn't open %s\n", filename);
 110        }
 111        if ((read(fd, hex, 41) < 41) ||
 112            (hex[40] != '\n') ||
 113            get_sha1_hex(hex, sha1)) {
 114                error("Couldn't read a hash from %s\n", filename);
 115                close(fd);
 116                return -1;
 117        }
 118        close(fd);
 119        return 0;
 120}
 121
 122int get_ref_sha1(const char *ref, unsigned char *sha1)
 123{
 124        char *filename;
 125        int retval;
 126        if (check_ref_format(ref))
 127                return -1;
 128        filename = ref_file_name(ref);
 129        retval = read_ref_file(filename, sha1);
 130        free(filename);
 131        return retval;
 132}
 133
 134static int lock_ref_file(const char *filename, const char *lock_filename,
 135                         const unsigned char *old_sha1)
 136{
 137        int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 138        unsigned char current_sha1[20];
 139        int retval;
 140        if (fd < 0) {
 141                return error("Couldn't open lock file for %s: %s",
 142                             filename, strerror(errno));
 143        }
 144        retval = read_ref_file(filename, current_sha1);
 145        if (old_sha1) {
 146                if (retval) {
 147                        close(fd);
 148                        unlink(lock_filename);
 149                        return error("Could not read the current value of %s",
 150                                     filename);
 151                }
 152                if (memcmp(current_sha1, old_sha1, 20)) {
 153                        close(fd);
 154                        unlink(lock_filename);
 155                        error("The current value of %s is %s",
 156                              filename, sha1_to_hex(current_sha1));
 157                        return error("Expected %s",
 158                                     sha1_to_hex(old_sha1));
 159                }
 160        } else {
 161                if (!retval) {
 162                        close(fd);
 163                        unlink(lock_filename);
 164                        return error("Unexpectedly found a value of %s for %s",
 165                                     sha1_to_hex(current_sha1), filename);
 166                }
 167        }
 168        return fd;
 169}
 170
 171int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
 172{
 173        char *filename;
 174        char *lock_filename;
 175        int retval;
 176        if (check_ref_format(ref))
 177                return -1;
 178        filename = ref_file_name(ref);
 179        lock_filename = ref_lock_file_name(ref);
 180        retval = lock_ref_file(filename, lock_filename, old_sha1);
 181        free(filename);
 182        free(lock_filename);
 183        return retval;
 184}
 185
 186static int write_ref_file(const char *filename,
 187                          const char *lock_filename, int fd,
 188                          const unsigned char *sha1)
 189{
 190        char *hex = sha1_to_hex(sha1);
 191        char term = '\n';
 192        if (write(fd, hex, 40) < 40 ||
 193            write(fd, &term, 1) < 1) {
 194                error("Couldn't write %s\n", filename);
 195                close(fd);
 196                return -1;
 197        }
 198        close(fd);
 199        rename(lock_filename, filename);
 200        return 0;
 201}
 202
 203int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
 204{
 205        char *filename;
 206        char *lock_filename;
 207        int retval;
 208        if (fd < 0)
 209                return -1;
 210        if (check_ref_format(ref))
 211                return -1;
 212        filename = ref_file_name(ref);
 213        lock_filename = ref_lock_file_name(ref);
 214        retval = write_ref_file(filename, lock_filename, fd, sha1);
 215        free(filename);
 216        free(lock_filename);
 217        return retval;
 218}
 219
 220int check_ref_format(const char *ref)
 221{
 222        char *middle;
 223        if (ref[0] == '.' || ref[0] == '/')
 224                return -1;
 225        middle = strchr(ref, '/');
 226        if (!middle || !middle[1])
 227                return -1;
 228        if (strchr(middle + 1, '/'))
 229                return -1;
 230        return 0;
 231}
 232
 233int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
 234{
 235        char *filename;
 236        char *lock_filename;
 237        int fd;
 238        int retval;
 239        if (check_ref_format(ref))
 240                return -1;
 241        filename = ref_file_name(ref);
 242        lock_filename = ref_lock_file_name(ref);
 243        fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 244        if (fd < 0) {
 245                error("Writing %s", lock_filename);
 246                perror("Open");
 247        }
 248        retval = write_ref_file(filename, lock_filename, fd, sha1);
 249        free(filename);
 250        free(lock_filename);
 251        return retval;
 252}