Skip to content

Commit acd3b9e

Browse files
committed
Enhance hold_lock_file_for_{update,append}() API
This changes the "die_on_error" boolean parameter to a mere "flags", and changes the existing callers of hold_lock_file_for_update/append() functions to pass LOCK_DIE_ON_ERROR. Signed-off-by: Junio C Hamano <[email protected]>
1 parent f563754 commit acd3b9e

File tree

11 files changed

+39
-23
lines changed

11 files changed

+39
-23
lines changed

builtin-commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
320320
die("unable to write new_index file");
321321

322322
fd = hold_lock_file_for_update(&false_lock,
323-
git_path("next-index-%d", getpid()), 1);
323+
git_path("next-index-%d", getpid()),
324+
LOCK_DIE_ON_ERROR);
324325

325326
create_base_index();
326327
add_remove_files(&partial);

builtin-fetch-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
813813
)
814814
die("shallow file was changed during fetch");
815815

816-
fd = hold_lock_file_for_update(&lock, shallow, 1);
816+
fd = hold_lock_file_for_update(&lock, shallow,
817+
LOCK_DIE_ON_ERROR);
817818
if (!write_shallow_commits(fd, 0)) {
818819
unlink(shallow);
819820
rollback_lock_file(&lock);

builtin-revert.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
338338
* reverse of it if we are revert.
339339
*/
340340

341-
msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
341+
msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
342+
LOCK_DIE_ON_ERROR);
342343

343344
encoding = get_encoding(message);
344345
if (!encoding)

bundle.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ int create_bundle(struct bundle_header *header, const char *path,
186186
if (bundle_to_stdout)
187187
bundle_fd = 1;
188188
else
189-
bundle_fd = hold_lock_file_for_update(&lock, path, 1);
189+
bundle_fd = hold_lock_file_for_update(&lock, path,
190+
LOCK_DIE_ON_ERROR);
190191

191192
/* write signature */
192193
write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ struct lock_file {
411411
char on_list;
412412
char filename[PATH_MAX];
413413
};
414+
#define LOCK_DIE_ON_ERROR 1
415+
#define LOCK_NODEREF 2
414416
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
415417
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
416418
extern int commit_lock_file(struct lock_file *);

lockfile.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,17 @@ static char *resolve_symlink(char *p, size_t s)
121121
}
122122

123123

124-
static int lock_file(struct lock_file *lk, const char *path)
124+
static int lock_file(struct lock_file *lk, const char *path, int flags)
125125
{
126-
if (strlen(path) >= sizeof(lk->filename)) return -1;
126+
if (strlen(path) >= sizeof(lk->filename))
127+
return -1;
127128
strcpy(lk->filename, path);
128129
/*
129130
* subtract 5 from size to make sure there's room for adding
130131
* ".lock" for the lock file name
131132
*/
132-
resolve_symlink(lk->filename, sizeof(lk->filename)-5);
133+
if (!(flags & LOCK_NODEREF))
134+
resolve_symlink(lk->filename, sizeof(lk->filename)-5);
133135
strcat(lk->filename, ".lock");
134136
lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
135137
if (0 <= lk->fd) {
@@ -155,35 +157,35 @@ static int lock_file(struct lock_file *lk, const char *path)
155157
return lk->fd;
156158
}
157159

158-
int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
160+
int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
159161
{
160-
int fd = lock_file(lk, path);
161-
if (fd < 0 && die_on_error)
162+
int fd = lock_file(lk, path, flags);
163+
if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
162164
die("unable to create '%s.lock': %s", path, strerror(errno));
163165
return fd;
164166
}
165167

166-
int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error)
168+
int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
167169
{
168170
int fd, orig_fd;
169171

170-
fd = lock_file(lk, path);
172+
fd = lock_file(lk, path, flags);
171173
if (fd < 0) {
172-
if (die_on_error)
174+
if (flags & LOCK_DIE_ON_ERROR)
173175
die("unable to create '%s.lock': %s", path, strerror(errno));
174176
return fd;
175177
}
176178

177179
orig_fd = open(path, O_RDONLY);
178180
if (orig_fd < 0) {
179181
if (errno != ENOENT) {
180-
if (die_on_error)
182+
if (flags & LOCK_DIE_ON_ERROR)
181183
die("cannot open '%s' for copying", path);
182184
close(fd);
183185
return error("cannot open '%s' for copying", path);
184186
}
185187
} else if (copy_fd(orig_fd, fd)) {
186-
if (die_on_error)
188+
if (flags & LOCK_DIE_ON_ERROR)
187189
exit(128);
188190
close(fd);
189191
return -1;
@@ -215,7 +217,10 @@ int commit_lock_file(struct lock_file *lk)
215217

216218
int hold_locked_index(struct lock_file *lk, int die_on_error)
217219
{
218-
return hold_lock_file_for_update(lk, get_index_file(), die_on_error);
220+
return hold_lock_file_for_update(lk, get_index_file(),
221+
die_on_error
222+
? LOCK_DIE_ON_ERROR
223+
: 0);
219224
}
220225

221226
void set_alternate_index_output(const char *name)

pack-refs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ int pack_refs(unsigned int flags)
8989
memset(&cbdata, 0, sizeof(cbdata));
9090
cbdata.flags = flags;
9191

92-
fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
92+
fd = hold_lock_file_for_update(&packed, git_path("packed-refs"),
93+
LOCK_DIE_ON_ERROR);
9394
cbdata.refs_file = fdopen(fd, "w");
9495
if (!cbdata.refs_file)
9596
die("unable to create ref-pack file structure (%s)",

refs.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
790790
struct ref_lock *lock;
791791
struct stat st;
792792
int last_errno = 0;
793-
int type;
793+
int type, lflags;
794794
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
795795

796796
lock = xcalloc(1, sizeof(struct ref_lock));
@@ -830,8 +830,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
830830

831831
lock->lk = xcalloc(1, sizeof(struct lock_file));
832832

833-
if (flags & REF_NODEREF)
833+
lflags = LOCK_DIE_ON_ERROR;
834+
if (flags & REF_NODEREF) {
834835
ref = orig_ref;
836+
lflags |= LOCK_NODEREF;
837+
}
835838
lock->ref_name = xstrdup(ref);
836839
lock->orig_ref_name = xstrdup(orig_ref);
837840
ref_file = git_path("%s", ref);
@@ -845,8 +848,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
845848
error("unable to create directory for %s", ref_file);
846849
goto error_return;
847850
}
848-
lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
849851

852+
lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags);
850853
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
851854

852855
error_return:

rerere.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ int setup_rerere(struct string_list *merge_rr)
346346
return -1;
347347

348348
merge_rr_path = xstrdup(git_path("MERGE_RR"));
349-
fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
349+
fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
350+
LOCK_DIE_ON_ERROR);
350351
read_rr(merge_rr);
351352
return fd;
352353
}

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static void read_info_alternates(const char * relative_base, int depth)
385385
void add_to_alternates_file(const char *reference)
386386
{
387387
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
388-
int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), 1);
388+
int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR);
389389
char *alt = mkpath("%s/objects\n", reference);
390390
write_or_die(fd, alt, strlen(alt));
391391
if (commit_lock_file(lock))

0 commit comments

Comments
 (0)