builtin-pack-refs.con commit Fix broken sha1 locking (53cce84)
   1#include "cache.h"
   2#include "refs.h"
   3
   4static FILE *refs_file;
   5static const char *result_path, *lock_path;
   6
   7static void remove_lock_file(void)
   8{
   9        if (lock_path)
  10                unlink(lock_path);
  11}
  12
  13static int handle_one_ref(const char *path, const unsigned char *sha1)
  14{
  15        fprintf(refs_file, "%s %s\n", sha1_to_hex(sha1), path);
  16        return 0;
  17}
  18
  19int cmd_pack_refs(int argc, const char **argv, const char *prefix)
  20{
  21        int fd;
  22
  23        result_path = xstrdup(git_path("packed-refs"));
  24        lock_path = xstrdup(mkpath("%s.lock", result_path));
  25
  26        fd = open(lock_path, O_CREAT | O_EXCL | O_WRONLY, 0666);
  27        if (fd < 0)
  28                die("unable to create new ref-pack file (%s)", strerror(errno));
  29        atexit(remove_lock_file);
  30
  31        refs_file = fdopen(fd, "w");
  32        if (!refs_file)
  33                die("unable to create ref-pack file structure (%s)", strerror(errno));
  34        for_each_ref(handle_one_ref);
  35        fsync(fd);
  36        fclose(refs_file);
  37        if (rename(lock_path, result_path) < 0)
  38                die("unable to overwrite old ref-pack file (%s)", strerror(errno));
  39        lock_path = NULL;
  40        return 0;
  41}