get_locked_file_path(): new function
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 1 Oct 2014 10:28:39 +0000 (12:28 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Oct 2014 20:53:54 +0000 (13:53 -0700)
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>
Documentation/technical/api-lockfile.txt
cache.h
lockfile.c
refs.c
index a3cb69b968209cdab002753af151a0e990f74c7f..d4484d154d553b55f3f92811e9737c8d7079cf19 100644 (file)
@@ -143,6 +143,11 @@ hold_lock_file_for_append::
        the existing contents of the file (if any) to the lockfile and
        position its write pointer at the end of the file.
 
        the existing contents of the file (if any) to the lockfile and
        position its write pointer at the end of the file.
 
+get_locked_file_path::
+
+       Return the path of the file that is locked by the specified
+       lock_file object. The caller must free the memory.
+
 commit_lock_file::
 
        Take a pointer to the `struct lock_file` initialized with an
 commit_lock_file::
 
        Take a pointer to the `struct lock_file` initialized with an
diff --git a/cache.h b/cache.h
index 7ea4e81257f02c929f967f92e9098ee487c121f0..d19e57ff64d2b4ef1601f2486877c4f4043c9fdd 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -590,6 +590,7 @@ extern void unable_to_lock_message(const char *path, int err,
 extern NORETURN void unable_to_lock_die(const char *path, int err);
 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
 extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
 extern NORETURN void unable_to_lock_die(const char *path, int err);
 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
 extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
+extern char *get_locked_file_path(struct lock_file *);
 extern int commit_lock_file_to(struct lock_file *, const char *path);
 extern int commit_lock_file(struct lock_file *);
 extern int reopen_lock_file(struct lock_file *);
 extern int commit_lock_file_to(struct lock_file *, const char *path);
 extern int commit_lock_file(struct lock_file *);
 extern int reopen_lock_file(struct lock_file *);
index 0a8c3c881ee00a184b8bee8c30c3640507e3dfbd..c51c6ec69d0352378c2d31bea45bb7029c8f5657 100644 (file)
@@ -257,6 +257,15 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
        return fd;
 }
 
        return fd;
 }
 
+char *get_locked_file_path(struct lock_file *lk)
+{
+       if (!lk->active)
+               die("BUG: get_locked_file_path() called for unlocked object");
+       if (lk->filename.len <= LOCK_SUFFIX_LEN)
+               die("BUG: get_locked_file_path() called for malformed lock object");
+       return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN);
+}
+
 int close_lock_file(struct lock_file *lk)
 {
        int fd = lk->fd;
 int close_lock_file(struct lock_file *lk)
 {
        int fd = lk->fd;
diff --git a/refs.c b/refs.c
index c10eaff57f212b336958bca5d468b69cd30c8415..e40c47edce9cf3b2b93b801b7a64ebe7d170441d 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2606,9 +2606,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
                 * loose.  The loose file name is the same as the
                 * lockfile name, minus ".lock":
                 */
                 * loose.  The loose file name is the same as the
                 * lockfile name, minus ".lock":
                 */
-               char *loose_filename = xmemdupz(
-                               lock->lk->filename.buf,
-                               lock->lk->filename.len - LOCK_SUFFIX_LEN);
+               char *loose_filename = get_locked_file_path(lock->lk);
                int err = unlink_or_warn(loose_filename);
                free(loose_filename);
                if (err && errno != ENOENT)
                int err = unlink_or_warn(loose_filename);
                free(loose_filename);
                if (err && errno != ENOENT)