mirror of
https://github.com/git/git.git
synced 2026-03-13 10:23:30 +01:00
close fd of lockfile before unlinking
This commit is contained in:
committed by
Johannes Sixt
parent
9c6663c419
commit
4a4ae17738
1
cache.h
1
cache.h
@@ -180,6 +180,7 @@ extern int refresh_cache(unsigned int flags);
|
||||
|
||||
struct lock_file {
|
||||
struct lock_file *next;
|
||||
int fd;
|
||||
char on_list;
|
||||
char filename[PATH_MAX];
|
||||
};
|
||||
|
||||
17
lockfile.c
17
lockfile.c
@@ -8,8 +8,10 @@ static struct lock_file *lock_file_list;
|
||||
static void remove_lock_file(void)
|
||||
{
|
||||
while (lock_file_list) {
|
||||
if (lock_file_list->filename[0])
|
||||
if (lock_file_list->filename[0]) {
|
||||
close(lock_file_list->fd);
|
||||
unlink(lock_file_list->filename);
|
||||
}
|
||||
lock_file_list = lock_file_list->next;
|
||||
}
|
||||
}
|
||||
@@ -23,10 +25,9 @@ static void remove_lock_file_on_signal(int signo)
|
||||
|
||||
static int lock_file(struct lock_file *lk, const char *path)
|
||||
{
|
||||
int fd;
|
||||
sprintf(lk->filename, "%s.lock", path);
|
||||
fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
|
||||
if (0 <= fd) {
|
||||
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
|
||||
if (0 <= lk->fd) {
|
||||
if (!lk->on_list) {
|
||||
lk->next = lock_file_list;
|
||||
lock_file_list = lk;
|
||||
@@ -42,7 +43,7 @@ static int lock_file(struct lock_file *lk, const char *path)
|
||||
}
|
||||
else
|
||||
lk->filename[0] = 0;
|
||||
return fd;
|
||||
return lk->fd;
|
||||
}
|
||||
|
||||
int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
|
||||
@@ -57,9 +58,11 @@ int commit_lock_file(struct lock_file *lk)
|
||||
{
|
||||
char result_file[PATH_MAX];
|
||||
int i;
|
||||
close(lk->fd);
|
||||
strcpy(result_file, lk->filename);
|
||||
i = strlen(result_file) - 5; /* .lock */
|
||||
result_file[i] = 0;
|
||||
unlink(result_file);
|
||||
i = rename(lk->filename, result_file);
|
||||
lk->filename[0] = 0;
|
||||
return i;
|
||||
@@ -67,8 +70,10 @@ int commit_lock_file(struct lock_file *lk)
|
||||
|
||||
void rollback_lock_file(struct lock_file *lk)
|
||||
{
|
||||
if (lk->filename[0])
|
||||
if (lk->filename[0]) {
|
||||
close(lk->fd);
|
||||
unlink(lk->filename);
|
||||
}
|
||||
lk->filename[0] = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user