Skip to content

Commit ec38b4e

Browse files
mhaggergitster
authored andcommitted
get_locked_file_path(): new function
Add a function to return the path of the file that is locked by a lock_file object. This reduces the knowledge that callers have to have about the lock_file layout. Suggested-by: Ronnie Sahlberg <[email protected]> Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 316683b commit ec38b4e

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

Documentation/technical/api-lockfile.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ hold_lock_file_for_append::
143143
the existing contents of the file (if any) to the lockfile and
144144
position its write pointer at the end of the file.
145145

146+
get_locked_file_path::
147+
148+
Return the path of the file that is locked by the specified
149+
lock_file object. The caller must free the memory.
150+
146151
commit_lock_file::
147152

148153
Take a pointer to the `struct lock_file` initialized with an

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ extern void unable_to_lock_message(const char *path, int err,
590590
extern NORETURN void unable_to_lock_die(const char *path, int err);
591591
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
592592
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
593+
extern char *get_locked_file_path(struct lock_file *);
593594
extern int commit_lock_file_to(struct lock_file *, const char *path);
594595
extern int commit_lock_file(struct lock_file *);
595596
extern int reopen_lock_file(struct lock_file *);

lockfile.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
257257
return fd;
258258
}
259259

260+
char *get_locked_file_path(struct lock_file *lk)
261+
{
262+
if (!lk->active)
263+
die("BUG: get_locked_file_path() called for unlocked object");
264+
if (lk->filename.len <= LOCK_SUFFIX_LEN)
265+
die("BUG: get_locked_file_path() called for malformed lock object");
266+
return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN);
267+
}
268+
260269
int close_lock_file(struct lock_file *lk)
261270
{
262271
int fd = lk->fd;

refs.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,9 +2606,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
26062606
* loose. The loose file name is the same as the
26072607
* lockfile name, minus ".lock":
26082608
*/
2609-
char *loose_filename = xmemdupz(
2610-
lock->lk->filename.buf,
2611-
lock->lk->filename.len - LOCK_SUFFIX_LEN);
2609+
char *loose_filename = get_locked_file_path(lock->lk);
26122610
int err = unlink_or_warn(loose_filename);
26132611
free(loose_filename);
26142612
if (err && errno != ENOENT)

0 commit comments

Comments
 (0)