pack-refs: use lockfile as everybody else does.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 3, 2006 at 02:15 UTC
03a182107fdb36170a72b8a3d94de2b52e3f6668
1 file changed
+4
-17
builtin-pack-refs.c
+4
-17
@@ -1,7 +1,6 @@
1
#include "cache.h"
2
#include "refs.h"
3
4
-static const char *result_path, *lock_path;
4
static const char builtin_pack_refs_usage[] =
5
"git-pack-refs [--prune]";
6
@@ -17,12 +16,6 @@ struct pack_refs_cb_data {
16
FILE *refs_file;
17
};
18
20
-static void remove_lock_file(void)
21
-{
22
- if (lock_path)
23
- unlink(lock_path);
24
-}
25
-
19
static int do_not_prune(int flags)
20
{
21
/* If it is already packed or if it is a symref,
@@ -69,6 +62,8 @@ static void prune_refs(struct ref_to_prune *r)
62
}
63
}
64
65
+static struct lock_file packed;
66
+
67
int cmd_pack_refs(int argc, const char **argv, const char *prefix)
68
{
69
int fd, i;
@@ -88,14 +83,7 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
83
if (i != argc)
84
usage(builtin_pack_refs_usage);
85
91
- result_path = xstrdup(git_path("packed-refs"));
92
- lock_path = xstrdup(mkpath("%s.lock", result_path));
93
-
94
- fd = open(lock_path, O_CREAT | O_EXCL | O_WRONLY, 0666);
95
- if (fd < 0)
96
- die("unable to create new ref-pack file (%s)", strerror(errno));
97
- atexit(remove_lock_file);
98
-
86
+ fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
87
cbdata.refs_file = fdopen(fd, "w");
88
if (!cbdata.refs_file)
89
die("unable to create ref-pack file structure (%s)",
@@ -103,9 +91,8 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
91
for_each_ref(handle_one_ref, &cbdata);
92
fsync(fd);
93
fclose(cbdata.refs_file);
106
- if (rename(lock_path, result_path) < 0)
94
+ if (commit_lock_file(&packed) < 0)
95
die("unable to overwrite old ref-pack file (%s)", strerror(errno));
108
- lock_path = NULL;
96
if (cbdata.prune)
97
prune_refs(cbdata.ref_to_prune);
98
return 0;