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