lockfile.con commit gitweb: SHA-1 in commit log message links to "object" view (bfe2191)
   1/*
   2 * Copyright (c) 2005, Junio C Hamano
   3 */
   4#include <signal.h>
   5#include "cache.h"
   6
   7static struct lock_file *lock_file_list;
   8
   9static void remove_lock_file(void)
  10{
  11        while (lock_file_list) {
  12                if (lock_file_list->filename[0])
  13                        unlink(lock_file_list->filename);
  14                lock_file_list = lock_file_list->next;
  15        }
  16}
  17
  18static void remove_lock_file_on_signal(int signo)
  19{
  20        remove_lock_file();
  21        signal(SIGINT, SIG_DFL);
  22        raise(signo);
  23}
  24
  25static int lock_file(struct lock_file *lk, const char *path)
  26{
  27        int fd;
  28        sprintf(lk->filename, "%s.lock", path);
  29        fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
  30        if (0 <= fd) {
  31                if (!lk->next) {
  32                        lk->next = lock_file_list;
  33                        lock_file_list = lk;
  34                        signal(SIGINT, remove_lock_file_on_signal);
  35                        atexit(remove_lock_file);
  36                }
  37                if (adjust_shared_perm(lk->filename))
  38                        return error("cannot fix permission bits on %s",
  39                                     lk->filename);
  40        }
  41        return fd;
  42}
  43
  44int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
  45{
  46        int fd = lock_file(lk, path);
  47        if (fd < 0 && die_on_error)
  48                die("unable to create '%s': %s", path, strerror(errno));
  49        return fd;
  50}
  51
  52int commit_lock_file(struct lock_file *lk)
  53{
  54        char result_file[PATH_MAX];
  55        int i;
  56        strcpy(result_file, lk->filename);
  57        i = strlen(result_file) - 5; /* .lock */
  58        result_file[i] = 0;
  59        i = rename(lk->filename, result_file);
  60        lk->filename[0] = 0;
  61        return i;
  62}
  63
  64void rollback_lock_file(struct lock_file *lk)
  65{
  66        if (lk->filename[0])
  67                unlink(lk->filename);
  68        lk->filename[0] = 0;
  69}
  70