Subject: [PATCH v2] lockfile: allow reopening a closed but still locked file

From: Junio C Hamano <[email-masked]>
Date: Mon, 14 Jul 2014 13:19:42 -0700

In some code paths (e.g. giving "add -i" to prepare the contents to
be committed interactively inside "commit -p") where a caller takes
a lock, writes the new content, give chance for others to use it
while still holding the lock, and then releases the lock when all is
done.  As an extension, allow the caller to re-update an already
closed file while still holding the lock (i.e. not yet committed) by
re-opening the file, to be followed by updating the contents and
then by the usual close_lock_file() or commit_lock_file().

This is necessary if we want to add code to rebuild the cache-tree
and write the resulting index out after "add -i" returns the control
to "commit -p", for example.

Signed-off-by: Junio C Hamano <[email-masked]>
---

 * I was being silly in the first attemt as usual ;-)  There is not
   much to be shared between the reopen and initial open code path
   in that we do not want to give O_EXCL to open(), we know the file
   exists so O_CREAT is not necessary, we know we have added it to
   the unlocked-atexit list and we already know that we have futzed
   with its permission bits.

   With this added on top of the nd/split-index series, the conflict
   resolution for the "interactive" codepath in builtin/commit.c
   with dt/cache-tree-repair topic would become like this:

  @@ -340,6 +340,13 @@ static char *prepare_index(int argc, const char

                  discard_cache();
                  read_cache_from(index_lock.filename);
  +               if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
  +                       if (reopen_lock_file(&index_lock) < 0)
  +                               die(_("unable to write index file"));
  +                       if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
  +                               die(_("unable to update temporary index"));
  +               } else
  +                       warning(_("Failed to update main cache tree"));

                  commit_style = COMMIT_NORMAL;
                  return index_lock.filename;

 cache.h    |  1 +
 lockfile.c | 10 ++++++++++
 2 files changed, 11 insertions(+)

diff --git a/cache.h b/cache.h
index c6b7770..b780794 100644
--- a/cache.h
+++ b/cache.h
@@ -567,6 +567,7 @@ extern NORETURN void unable_to_lock_index_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 int commit_lock_file(struct lock_file *);
+extern int reopen_lock_file(struct lock_file *);
 extern void update_index_if_able(struct index_state *, struct lock_file *);
 
 extern int hold_locked_index(struct lock_file *, int);
diff --git a/lockfile.c b/lockfile.c
index b706614..9c12ec5 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -228,6 +228,16 @@ int close_lock_file(struct lock_file *lk)
 	return close(fd);
 }
 
+int reopen_lock_file(struct lock_file *lk)
+{
+	if (0 <= lk->fd)
+		die(_("BUG: reopen a lockfile that is still open"));
+	if (!lk->filename[0])
+		die(_("BUG: reopen a lockfile that has been committed"));
+	lk->fd = open(lk->filename, O_WRONLY);
+	return lk->fd;
+}
+
 int commit_lock_file(struct lock_file *lk)
 {
 	char result_file[PATH_MAX];
-- 
2.0.1-814-g49d294e