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 <sahlberg@google.com> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC ec38b4e482e96e62762452cab5714e55abdb48c3
4 files changed +16 -3
Documentation/technical/api-lockfile.txt
+5
@@ -143,6 +143,11 @@ hold_lock_file_for_append::
143 the existing contents of the file (if any) to the lockfile and
144 position its write pointer at the end of the file.
145
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 +
151 commit_lock_file::
152
153 Take a pointer to the `struct lock_file` initialized with an
cache.h
+1
@@ -590,6 +590,7 @@ extern void unable_to_lock_message(const char *path, int err,
590 extern NORETURN void unable_to_lock_die(const char *path, int err);
591 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
592 extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
593 +extern char *get_locked_file_path(struct lock_file *);
594 extern int commit_lock_file_to(struct lock_file *, const char *path);
595 extern int commit_lock_file(struct lock_file *);
596 extern int reopen_lock_file(struct lock_file *);
lockfile.c
+9
@@ -257,6 +257,15 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
257 return fd;
258 }
259
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 +
269 int close_lock_file(struct lock_file *lk)
270 {
271 int fd = lk->fd;
refs.c
+1 -3
@@ -2606,9 +2606,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
2606 * loose. The loose file name is the same as the
2607 * lockfile name, minus ".lock":
2608 */
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);
2610 int err = unlink_or_warn(loose_filename);
2611 free(loose_filename);
2612 if (err && errno != ENOENT)