{"thread":{"id":"36356","subject":"[PATCH v2 00/25] Lockfile correctness and refactoring","startedAt":"2014-04-06T23:33:42Z","lastAt":"2014-04-14T08:21:28Z","messageCount":37,"participants":["Michael Haggerty","Johannes Sixt","Jeff King"],"isPatch":true,"patchVersion":2,"patchTotal":25},"messages":[{"id":"238409","messageId":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":null,"subject":"[PATCH v2 00/25] Lockfile correctness and refactoring","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:42Z","receivedAt":"2014-04-06T23:33:42Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This is a second attempt at renovating the lock file code.  Thanks to\nPeff, Junio, Torsten, and Eric for their helpful reviews of v1.\n\nv1 of this patch series [1] did some refactoring and then added a new\nfeature to the lock_file API: the ability to activate a new version of\na locked file while retaining the lock.\n\nBut the review of v1 turned up even more correctness issues in the\nexisting implementation of lock files.  So this v2 dials back the\nscope of the changes (it omits the new feature) but does more work to\nfix problems with the current lock file implementation.\n\nThe main theme of this patch series is to better define the state\ndiagram for lock_file objects and to fix code that left them in\nincorrect, indeterminate, or unexpected states.  There are also a few\npatches that convert several functions to use strbufs instead of\nlimiting pathnames to a maximum length.\n\nI hope that submitting these patches separately will make it easier\nfor them to be accepted without first having to decide wither the\nactivate-file-while-retaining-lock feature is a good one.\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/245609\n\nMichael Haggerty (25):\n  api-lockfile: expand the documentation\n  unable_to_lock_die(): rename function from unable_to_lock_index_die()\n  rollback_lock_file(): do not clear filename redundantly\n  rollback_lock_file(): set fd to -1\n  lockfile: unlock file if lockfile permissions cannot be adjusted\n  hold_lock_file_for_append(): release lock on errors\n  lock_file(): always add lock_file object to lock_file_list\n  struct lock_file: replace on_list field with flags field\n  lockfile.c: document the various states of lock_file objects\n  lockfile: define a constant LOCK_SUFFIX_LEN\n  delete_ref_loose(): don't muck around in the lock_file's filename\n  prepare_index(): declare return value to be (const char *)\n  write_packed_entry_fn(): convert cb_data into a (const int *)\n  lock_file(): exit early if lockfile cannot be opened\n  remove_lock_file(): call rollback_lock_file()\n  commit_lock_file(): inline temporary variable\n  commit_lock_file(): make committing an unlocked lockfile a NOP\n  lockfile: avoid transitory invalid states\n  try_merge_strategy(): remove redundant lock_file allocation\n  try_merge_strategy(): use a statically-allocated lock_file object\n  commit_lock_file(): use a strbuf to manage temporary space\n  Change lock_file::filename into a strbuf\n  resolve_symlink(): use a strbuf for internal scratch space\n  resolve_symlink(): take a strbuf parameter\n  trim_last_path_elm(): replace last_path_elm()\n\n Documentation/technical/api-lockfile.txt |  40 ++++-\n builtin/commit.c                         |  16 +-\n builtin/merge.c                          |  15 +-\n builtin/reflog.c                         |   2 +-\n builtin/update-index.c                   |   2 +-\n cache.h                                  |   6 +-\n config.c                                 |   6 +-\n lockfile.c                               | 282 +++++++++++++++++++------------\n refs.c                                   |  20 ++-\n shallow.c                                |   6 +-\n 10 files changed, 243 insertions(+), 152 deletions(-)\n\n-- \n1.9.1\n"},{"id":"238410","messageId":"1396827247-28465-2-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 01/25] api-lockfile: expand the documentation","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:43Z","receivedAt":"2014-04-06T23:33:43Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Document a couple more functions and the flags argument as used by\nhold_lock_file_for_update() and hold_lock_file_for_append().\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n Documentation/technical/api-lockfile.txt | 36 +++++++++++++++++++++++++++++---\n 1 file changed, 33 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt\nindex dd89404..b53e300 100644\n--- a/Documentation/technical/api-lockfile.txt\n+++ b/Documentation/technical/api-lockfile.txt\n@@ -28,9 +28,39 @@ hold_lock_file_for_update::\n \tthe final destination (e.g. `$GIT_DIR/index`) and a flag\n \t`die_on_error`.  Attempt to create a lockfile for the\n \tdestination and return the file descriptor for writing\n-\tto the file.  If `die_on_error` flag is true, it dies if\n-\ta lock is already taken for the file; otherwise it\n-\treturns a negative integer to the caller on failure.\n+\tto the file.  The flags parameter is a combination of\n++\n+--\n+LOCK_NODEREF::\n+\n+\tUsually symbolic links in path are resolved in path and the\n+\tlockfile is created by adding \".lock\" to the resolved path;\n+\thowever, if `LOCK_NODEREF` is set, then the lockfile is\n+\tcreated by adding \".lock\" to the path argument itself.\n+\n+LOCK_DIE_ON_ERROR::\n+\n+\tIf a lock is already taken for the file, `die()` with an error\n+\tmessage.  If this option is not specified, return a negative\n+\tinteger to the caller on failure.\n+--\n+\n+hold_lock_file_for_append::\n+\n+\tLike `hold_lock_file_for_update()`, except that additionally\n+\tthe existing contents of the file (if any) are copied to the\n+\tlockfile and its write pointer is positioned at the end of the\n+\tfile before returning.\n+\n+unable_to_lock_error::\n+\n+\tEmit an error describing that there was an error locking the\n+\tspecified path.  The err parameter should be the errno of the\n+\tproblem that caused the failure.\n+\n+unable_to_lock_die::\n+\n+\tLike `unable_to_lock_error()`, but also `die()`.\n \n commit_lock_file::\n \n-- \n1.9.1\n"},{"id":"238411","messageId":"1396827247-28465-3-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 02/25] unable_to_lock_die(): rename function from unable_to_lock_index_die()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:44Z","receivedAt":"2014-04-06T23:33:44Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This function is used for other things besides the index, so rename it\naccordingly.\n\nSuggested-by: Jeff King <peff@peff.net>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/update-index.c | 2 +-\n cache.h                | 2 +-\n lockfile.c             | 6 +++---\n refs.c                 | 2 +-\n 4 files changed, 6 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex d12ad95..3b1721b 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -891,7 +891,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\tif (newfd < 0) {\n \t\t\tif (refresh_args.flags & REFRESH_QUIET)\n \t\t\t\texit(128);\n-\t\t\tunable_to_lock_index_die(get_index_file(), lock_error);\n+\t\t\tunable_to_lock_die(get_index_file(), lock_error);\n \t\t}\n \t\tif (write_cache(newfd, active_cache, active_nr) ||\n \t\t    commit_locked_index(lock_file))\ndiff --git a/cache.h b/cache.h\nindex 107ac61..3a873a4 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -543,7 +543,7 @@ struct lock_file {\n #define LOCK_DIE_ON_ERROR 1\n #define LOCK_NODEREF 2\n extern int unable_to_lock_error(const char *path, int err);\n-extern NORETURN void unable_to_lock_index_die(const char *path, int err);\n+extern NORETURN void unable_to_lock_die(const char *path, int err);\n extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);\n extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);\n extern int commit_lock_file(struct lock_file *);\ndiff --git a/lockfile.c b/lockfile.c\nindex 8fbcb6a..d4bfb3f 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -181,7 +181,7 @@ int unable_to_lock_error(const char *path, int err)\n \treturn -1;\n }\n \n-NORETURN void unable_to_lock_index_die(const char *path, int err)\n+NORETURN void unable_to_lock_die(const char *path, int err)\n {\n \tdie(\"%s\", unable_to_lock_message(path, err));\n }\n@@ -190,7 +190,7 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)\n {\n \tint fd = lock_file(lk, path, flags);\n \tif (fd < 0 && (flags & LOCK_DIE_ON_ERROR))\n-\t\tunable_to_lock_index_die(path, errno);\n+\t\tunable_to_lock_die(path, errno);\n \treturn fd;\n }\n \n@@ -201,7 +201,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)\n \tfd = lock_file(lk, path, flags);\n \tif (fd < 0) {\n \t\tif (flags & LOCK_DIE_ON_ERROR)\n-\t\t\tunable_to_lock_index_die(path, errno);\n+\t\t\tunable_to_lock_die(path, errno);\n \t\treturn fd;\n \t}\n \ndiff --git a/refs.c b/refs.c\nindex 28d5eca..67fe8b7 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2118,7 +2118,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t\t\t */\n \t\t\tgoto retry;\n \t\telse\n-\t\t\tunable_to_lock_index_die(ref_file, errno);\n+\t\t\tunable_to_lock_die(ref_file, errno);\n \t}\n \treturn old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;\n \n-- \n1.9.1\n"},{"id":"238413","messageId":"1396827247-28465-4-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 03/25] rollback_lock_file(): do not clear filename redundantly","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:45Z","receivedAt":"2014-04-06T23:33:45Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It is only necessary to clear the lock_file's filename field if it was\nnot already clear.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex d4bfb3f..7701267 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -277,6 +277,6 @@ void rollback_lock_file(struct lock_file *lk)\n \t\tif (lk->fd >= 0)\n \t\t\tclose(lk->fd);\n \t\tunlink_or_warn(lk->filename);\n+\t\tlk->filename[0] = 0;\n \t}\n-\tlk->filename[0] = 0;\n }\n-- \n1.9.1\n"},{"id":"238412","messageId":"1396827247-28465-5-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 04/25] rollback_lock_file(): set fd to -1","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:46Z","receivedAt":"2014-04-06T23:33:46Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"When rolling back the lockfile, call close_lock_file() so that the\nlock_file's fd field gets set back to -1.  This keeps the lock_file\nobject in a valid state, which is important because these objects are\nallowed to be reused.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 7701267..1122542 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -275,7 +275,7 @@ void rollback_lock_file(struct lock_file *lk)\n {\n \tif (lk->filename[0]) {\n \t\tif (lk->fd >= 0)\n-\t\t\tclose(lk->fd);\n+\t\t\tclose_lock_file(lk);\n \t\tunlink_or_warn(lk->filename);\n \t\tlk->filename[0] = 0;\n \t}\n-- \n1.9.1\n"},{"id":"238416","messageId":"1396827247-28465-6-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 05/25] lockfile: unlock file if lockfile permissions cannot be adjusted","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:47Z","receivedAt":"2014-04-06T23:33:47Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"If the call to adjust_shared_perm() fails, lock_file returns -1, which\nto the caller looks like any other failure to lock the file.  So in\nthis case, roll back the lockfile before returning so that the lock\nfile is deleted immediately and the lockfile object is left in a\npredictable state (namely, unlocked).  Previously, the lockfile was\nretained until process cleanup in this situation.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 1122542..b101f77 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -148,9 +148,11 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\t\tlock_file_list = lk;\n \t\t\tlk->on_list = 1;\n \t\t}\n-\t\tif (adjust_shared_perm(lk->filename))\n-\t\t\treturn error(\"cannot fix permission bits on %s\",\n-\t\t\t\t     lk->filename);\n+\t\tif (adjust_shared_perm(lk->filename)) {\n+\t\t\terror(\"cannot fix permission bits on %s\", lk->filename);\n+\t\t\trollback_lock_file(lk);\n+\t\t\treturn -1;\n+\t\t}\n \t}\n \telse\n \t\tlk->filename[0] = 0;\n-- \n1.9.1\n"},{"id":"238419","messageId":"1396827247-28465-7-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 06/25] hold_lock_file_for_append(): release lock on errors","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:48Z","receivedAt":"2014-04-06T23:33:48Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"If there is an error copying the old contents to the lockfile, roll\nback the lockfile before exiting so that the lockfile is not held\nuntil process cleanup.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex b101f77..e78a35f 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -212,13 +212,13 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)\n \t\tif (errno != ENOENT) {\n \t\t\tif (flags & LOCK_DIE_ON_ERROR)\n \t\t\t\tdie(\"cannot open '%s' for copying\", path);\n-\t\t\tclose(fd);\n+\t\t\trollback_lock_file(lk);\n \t\t\treturn error(\"cannot open '%s' for copying\", path);\n \t\t}\n \t} else if (copy_fd(orig_fd, fd)) {\n \t\tif (flags & LOCK_DIE_ON_ERROR)\n \t\t\texit(128);\n-\t\tclose(fd);\n+\t\trollback_lock_file(lk);\n \t\treturn -1;\n \t}\n \treturn fd;\n-- \n1.9.1\n"},{"id":"238417","messageId":"1396827247-28465-8-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 07/25] lock_file(): always add lock_file object to lock_file_list","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:49Z","receivedAt":"2014-04-06T23:33:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It used to be that if locking failed, lock_file() usually did not\nregister the lock_file object in lock_file_list but sometimes it did.\nThis confusion was compounded if lock_file() was called via\nhold_lock_file_for_append(), which has its own failure modes.\n\nThe ambiguity didn't have any ill effects, because lock_file objects\ncannot be removed from the lock_file_list anyway.  But it is\nunnecessary to leave this behavior inconsistent.\n\nSo change lock_file() to *always* ensure that the lock_file object is\nregistered in lock_file_list regardless of whether an error occurs.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 25 ++++++++++++++++---------\n 1 file changed, 16 insertions(+), 9 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex e78a35f..120a6d6 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -130,6 +130,22 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t */\n \tstatic const size_t max_path_len = sizeof(lk->filename) - 5;\n \n+\tif (!lock_file_list) {\n+\t\t/* One-time initialization */\n+\t\tsigchain_push_common(remove_lock_file_on_signal);\n+\t\tatexit(remove_lock_file);\n+\t}\n+\n+\tif (!lk->on_list) {\n+\t\t/* Initialize *lk and add it to lock_file_list: */\n+\t\tlk->fd = -1;\n+\t\tlk->owner = 0;\n+\t\tlk->on_list = 1;\n+\t\tlk->filename[0] = 0;\n+\t\tlk->next = lock_file_list;\n+\t\tlock_file_list = lk;\n+\t}\n+\n \tif (strlen(path) >= max_path_len)\n \t\treturn -1;\n \tstrcpy(lk->filename, path);\n@@ -138,16 +154,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \tstrcat(lk->filename, \".lock\");\n \tlk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);\n \tif (0 <= lk->fd) {\n-\t\tif (!lock_file_list) {\n-\t\t\tsigchain_push_common(remove_lock_file_on_signal);\n-\t\t\tatexit(remove_lock_file);\n-\t\t}\n \t\tlk->owner = getpid();\n-\t\tif (!lk->on_list) {\n-\t\t\tlk->next = lock_file_list;\n-\t\t\tlock_file_list = lk;\n-\t\t\tlk->on_list = 1;\n-\t\t}\n \t\tif (adjust_shared_perm(lk->filename)) {\n \t\t\terror(\"cannot fix permission bits on %s\", lk->filename);\n \t\t\trollback_lock_file(lk);\n-- \n1.9.1\n"},{"id":"238418","messageId":"1396827247-28465-9-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 08/25] struct lock_file: replace on_list field with flags field","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:50Z","receivedAt":"2014-04-06T23:33:50Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This makes space for other bits, which will arrive soon.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n cache.h    | 2 +-\n lockfile.c | 9 +++++++--\n 2 files changed, 8 insertions(+), 3 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 3a873a4..59ce5cd 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -537,7 +537,7 @@ struct lock_file {\n \tstruct lock_file *next;\n \tint fd;\n \tpid_t owner;\n-\tchar on_list;\n+\tunsigned char flags;\n \tchar filename[PATH_MAX];\n };\n #define LOCK_DIE_ON_ERROR 1\ndiff --git a/lockfile.c b/lockfile.c\nindex 120a6d6..0486c58 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -4,6 +4,11 @@\n #include \"cache.h\"\n #include \"sigchain.h\"\n \n+/* Values for lock_file::flags: */\n+\n+/* This lock_file instance is in the lock_file_list */\n+#define LOCK_FLAGS_ON_LIST 0x01\n+\n static struct lock_file *lock_file_list;\n static const char *alternate_index_output;\n \n@@ -136,11 +141,11 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\tatexit(remove_lock_file);\n \t}\n \n-\tif (!lk->on_list) {\n+\tif (!(lk->flags & LOCK_FLAGS_ON_LIST)) {\n \t\t/* Initialize *lk and add it to lock_file_list: */\n \t\tlk->fd = -1;\n \t\tlk->owner = 0;\n-\t\tlk->on_list = 1;\n+\t\tlk->flags |= LOCK_FLAGS_ON_LIST;\n \t\tlk->filename[0] = 0;\n \t\tlk->next = lock_file_list;\n \t\tlock_file_list = lk;\n-- \n1.9.1\n"},{"id":"238415","messageId":"1396827247-28465-10-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 09/25] lockfile.c: document the various states of lock_file objects","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:51Z","receivedAt":"2014-04-06T23:33:51Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Document the valid states of lock_file objects, how they get into each\nstate, and how the state is encoded in the object's fields.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 52 insertions(+)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 0486c58..2438430 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -4,6 +4,58 @@\n #include \"cache.h\"\n #include \"sigchain.h\"\n \n+/*\n+ * File write-locks as used by Git.\n+ *\n+ * When a file at $FILENAME needs to be written, it is done as\n+ * follows:\n+ *\n+ * 1. Obtain a lock on the file by creating a lockfile at path\n+ *    $FILENAME.lock.  The file is opened for read/write using O_CREAT\n+ *    and O_EXCL mode to ensure that it doesn't already exist.  Such a\n+ *    lock file is respected by writers *but not by readers*.\n+ *\n+ *    Usually, if $FILENAME is a symlink, then it is resolved, and the\n+ *    file ultimately pointed to is the one that is locked and later\n+ *    replaced.  However, if LOCK_NODEREF is used, then $FILENAME\n+ *    itself is locked and later replaced, even if it is a symlink.\n+ *\n+ * 2. Write the new file contents to the lockfile.\n+ *\n+ * 3. Move the lockfile to its final destination using rename(2).\n+ *\n+ * Instead of (3), the change can be rolled back by deleting lockfile.\n+ *\n+ * This module keeps track of all locked files in lock_file_list.\n+ * When the first file is locked, it registers an atexit(3) handler;\n+ * when the program exits, the handler rolls back any files that have\n+ * been locked but were never committed or rolled back.\n+ *\n+ * A lock_file object can be in several states:\n+ *\n+ * - Uninitialized.  In this state the object's flags field must be\n+ *   zero but the rest of its contents need not be initialized.  As\n+ *   soon as the object is used in any way, it is irrevocably\n+ *   registered in the lock_file_list, and flags & LOCK_FLAGS_ON_LIST\n+ *   is set.\n+ *\n+ * - Locked, lockfile open (after hold_lock_file_for_update() or\n+ *   hold_lock_file_for_append()).  In this state, the lockfile\n+ *   exists, filename holds the filename of the lockfile, fd holds a\n+ *   file descriptor open for writing to the lockfile, and owner holds\n+ *   the PID of the process that locked the file.\n+ *\n+ * - Locked, lockfile closed (after close_lock_file()).  Same as the\n+ *   previous state, except that the lockfile is closed and fd is -1.\n+ *\n+ * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a\n+ *   failed attempt to lock).  In this state, filename[0] == '\\0' and\n+ *   fd is -1.  The object is left registered in the lock_file_list,\n+ *   and flags & LOCK_FLAGS_ON_LIST is set.\n+ *\n+ * See Documentation/api-lockfile.txt for more information.\n+ */\n+\n /* Values for lock_file::flags: */\n \n /* This lock_file instance is in the lock_file_list */\n-- \n1.9.1\n"},{"id":"238420","messageId":"1396827247-28465-11-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 10/25] lockfile: define a constant LOCK_SUFFIX_LEN","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:52Z","receivedAt":"2014-04-06T23:33:52Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 11 +++++++----\n 1 file changed, 7 insertions(+), 4 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 2438430..1bd0ae1 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -178,14 +178,17 @@ static char *resolve_symlink(char *p, size_t s)\n \treturn p;\n }\n \n+/* We append \".lock\" to the filename to derive the lockfile name: */\n+#define LOCK_SUFFIX_LEN 5\n \n static int lock_file(struct lock_file *lk, const char *path, int flags)\n {\n \t/*\n-\t * subtract 5 from size to make sure there's room for adding\n-\t * \".lock\" for the lock file name\n+\t * subtract LOCK_SUFFIX_LEN from size to make sure there's\n+\t * room for adding \".lock\" for the lock file name:\n \t */\n-\tstatic const size_t max_path_len = sizeof(lk->filename) - 5;\n+\tstatic const size_t max_path_len = sizeof(lk->filename) -\n+\t\t\t\t\t   LOCK_SUFFIX_LEN;\n \n \tif (!lock_file_list) {\n \t\t/* One-time initialization */\n@@ -302,7 +305,7 @@ int commit_lock_file(struct lock_file *lk)\n \tif (lk->fd >= 0 && close_lock_file(lk))\n \t\treturn -1;\n \tstrcpy(result_file, lk->filename);\n-\ti = strlen(result_file) - 5; /* .lock */\n+\ti = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */\n \tresult_file[i] = 0;\n \tif (rename(lk->filename, result_file))\n \t\treturn -1;\n-- \n1.9.1\n"},{"id":"238414","messageId":"1396827247-28465-12-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 11/25] delete_ref_loose(): don't muck around in the lock_file's filename","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:53Z","receivedAt":"2014-04-06T23:33:53Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It's bad manners.  Especially since, if unlink_or_warn() failed, the\nmemory wasn't restored to its original contents.\n\nSo make our own copy to work with.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 14 ++++++++------\n 1 file changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 67fe8b7..64ea2f0 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2485,12 +2485,14 @@ static int repack_without_ref(const char *refname)\n static int delete_ref_loose(struct ref_lock *lock, int flag)\n {\n \tif (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {\n-\t\t/* loose */\n-\t\tint err, i = strlen(lock->lk->filename) - 5; /* .lock */\n-\n-\t\tlock->lk->filename[i] = 0;\n-\t\terr = unlink_or_warn(lock->lk->filename);\n-\t\tlock->lk->filename[i] = '.';\n+\t\t/*\n+\t\t * loose.  The loose file name is the same as the\n+\t\t * lockfile name, minus \".lock\":\n+\t\t */\n+\t\tchar *loose_filename = xmemdupz(lock->lk->filename,\n+\t\t\t\t\t\tstrlen(lock->lk->filename) - 5);\n+\t\tint err = unlink_or_warn(loose_filename);\n+\t\tfree(loose_filename);\n \t\tif (err && errno != ENOENT)\n \t\t\treturn 1;\n \t}\n-- \n1.9.1\n"},{"id":"238421","messageId":"1396827247-28465-13-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 12/25] prepare_index(): declare return value to be (const char *)","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:54Z","receivedAt":"2014-04-06T23:33:54Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Declare the return value to be const to make it clear that we aren't\ngiving callers permission to write over the string that it points at.\n(The return value is the filename field of a struct lock_file, which\ncan be used by a signal handler at any time and therefore shouldn't be\ntampered with.)\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/commit.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex d9550c5..2e2d8bb 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -302,8 +302,8 @@ static void refresh_cache_or_die(int refresh_flags)\n \t\tdie_resolve_conflict(\"commit\");\n }\n \n-static char *prepare_index(int argc, const char **argv, const char *prefix,\n-\t\t\t   const struct commit *current_head, int is_status)\n+static const char *prepare_index(int argc, const char **argv, const char *prefix,\n+\t\t\t\t const struct commit *current_head, int is_status)\n {\n \tint fd;\n \tstruct string_list partial;\n-- \n1.9.1\n"},{"id":"238432","messageId":"1396827247-28465-14-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 13/25] write_packed_entry_fn(): convert cb_data into a (const int *)","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:55Z","receivedAt":"2014-04-06T23:33:55Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This makes it obvious that we have no plans to change the integer\npointed to, which is actually the fd field from a struct lock_file.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex 64ea2f0..cd471ab 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2176,7 +2176,7 @@ static void write_packed_entry(int fd, char *refname, unsigned char *sha1,\n  */\n static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)\n {\n-\tint *fd = cb_data;\n+\tconst int *fd = cb_data;\n \tenum peel_status peel_status = peel_entry(entry, 0);\n \n \tif (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)\n-- \n1.9.1\n"},{"id":"238430","messageId":"1396827247-28465-15-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 14/25] lock_file(): exit early if lockfile cannot be opened","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:56Z","receivedAt":"2014-04-06T23:33:56Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"This is a bit easier to read than the old version, which nested part\nof the non-error code in an \"if\" block.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 18 +++++++++---------\n 1 file changed, 9 insertions(+), 9 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 1bd0ae1..0879214 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -213,16 +213,16 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\tresolve_symlink(lk->filename, max_path_len);\n \tstrcat(lk->filename, \".lock\");\n \tlk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);\n-\tif (0 <= lk->fd) {\n-\t\tlk->owner = getpid();\n-\t\tif (adjust_shared_perm(lk->filename)) {\n-\t\t\terror(\"cannot fix permission bits on %s\", lk->filename);\n-\t\t\trollback_lock_file(lk);\n-\t\t\treturn -1;\n-\t\t}\n-\t}\n-\telse\n+\tif (lk->fd < 0) {\n \t\tlk->filename[0] = 0;\n+\t\treturn -1;\n+\t}\n+\tlk->owner = getpid();\n+\tif (adjust_shared_perm(lk->filename)) {\n+\t\terror(\"cannot fix permission bits on %s\", lk->filename);\n+\t\trollback_lock_file(lk);\n+\t\treturn -1;\n+\t}\n \treturn lk->fd;\n }\n \n-- \n1.9.1\n"},{"id":"238422","messageId":"1396827247-28465-16-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 15/25] remove_lock_file(): call rollback_lock_file()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:57Z","receivedAt":"2014-04-06T23:33:57Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It does just what we need.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 8 ++------\n 1 file changed, 2 insertions(+), 6 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 0879214..f6bee35 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -69,12 +69,8 @@ static void remove_lock_file(void)\n \tpid_t me = getpid();\n \n \twhile (lock_file_list) {\n-\t\tif (lock_file_list->owner == me &&\n-\t\t    lock_file_list->filename[0]) {\n-\t\t\tif (lock_file_list->fd >= 0)\n-\t\t\t\tclose(lock_file_list->fd);\n-\t\t\tunlink_or_warn(lock_file_list->filename);\n-\t\t}\n+\t\tif (lock_file_list->owner == me)\n+\t\t\trollback_lock_file(lock_file_list);\n \t\tlock_file_list = lock_file_list->next;\n \t}\n }\n-- \n1.9.1\n"},{"id":"238423","messageId":"1396827247-28465-17-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 16/25] commit_lock_file(): inline temporary variable","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:58Z","receivedAt":"2014-04-06T23:33:58Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex f6bee35..47762c6 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -297,12 +297,14 @@ int close_lock_file(struct lock_file *lk)\n int commit_lock_file(struct lock_file *lk)\n {\n \tchar result_file[PATH_MAX];\n-\tsize_t i;\n+\n \tif (lk->fd >= 0 && close_lock_file(lk))\n \t\treturn -1;\n+\n \tstrcpy(result_file, lk->filename);\n-\ti = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */\n-\tresult_file[i] = 0;\n+\t/* remove \".lock\": */\n+\tresult_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;\n+\n \tif (rename(lk->filename, result_file))\n \t\treturn -1;\n \tlk->filename[0] = 0;\n-- \n1.9.1\n"},{"id":"238431","messageId":"1396827247-28465-18-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 17/25] commit_lock_file(): make committing an unlocked lockfile a NOP","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:33:59Z","receivedAt":"2014-04-06T23:33:59Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"It was previously a bug to call commit_lock_file() with a lock_file\nobject that was not active (an illegal access would happen within the\nfunction).  It was presumably never done, but this would be an easy\nprogramming error to overlook.  So guard the file-renaming code with\nan if statement to change committing an unlocked file into a NOP.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\nAlternatively, we could make it a fatal error (e.g., an assert() or\nif...die()) to call commit_lock_file() on an unlocked file, or omit a\nwarning in this case.  But since it is so hard to test code related to\nlocking failures, I have the feeling that such an error is most likely\nto occur in some error-handling path, maybe when some other lockfile\nacquisition has failed, and it would be better to let the code\ncontinue its attempted cleanup instead of dying.  But it would be easy\nto persuade me to change my opinion.\n\n Documentation/technical/api-lockfile.txt | 4 +++-\n lockfile.c                               | 3 +++\n 2 files changed, 6 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt\nindex b53e300..bfb2676 100644\n--- a/Documentation/technical/api-lockfile.txt\n+++ b/Documentation/technical/api-lockfile.txt\n@@ -68,7 +68,9 @@ commit_lock_file::\n \twith an earlier call to `hold_lock_file_for_update()`,\n \tclose the file descriptor and rename the lockfile to its\n \tfinal destination.  Returns 0 upon success, a negative\n-\tvalue on failure to close(2) or rename(2).\n+\tvalue on failure to close(2) or rename(2).  It is a NOP to\n+\tcall `commit_lock_file()` for a `lock_file` object that is not\n+\tcurrently locked.\n \n rollback_lock_file::\n \ndiff --git a/lockfile.c b/lockfile.c\nindex 47762c6..24aaa53 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -301,6 +301,9 @@ int commit_lock_file(struct lock_file *lk)\n \tif (lk->fd >= 0 && close_lock_file(lk))\n \t\treturn -1;\n \n+\tif (!lk->filename[0])\n+\t\treturn 0;\n+\n \tstrcpy(result_file, lk->filename);\n \t/* remove \".lock\": */\n \tresult_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;\n-- \n1.9.1\n"},{"id":"238425","messageId":"1396827247-28465-19-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 18/25] lockfile: avoid transitory invalid states","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:00Z","receivedAt":"2014-04-06T23:34:00Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Because remove_lock_file() can be called any time by the signal\nhandler, it is important that any lock_file objects that are in the\nlock_file_list are always in a valid state.  And since lock_file\nobjects are often reused (but are never removed from lock_file_list),\nthat means we have to be careful whenever mutating a lock_file object\nto always keep it in a well-defined state.\n\nThis was formerly not the case, because part of the state was recorded\nby whether lk->filename was the empty string or a valid filename.  It\nis wrong to assume that this string can be updated atomically; for\nexample, even\n\n    strcpy(lk->filename, value)\n\nis unsafe.  But the old code was even more reckless; for example,\n\n    strcpy(lk->filename, path);\n    if (!(flags & LOCK_NODEREF))\n            resolve_symlink(lk->filename, max_path_len);\n    strcat(lk->filename, \".lock\");\n\nDuring the call to resolve_symlink(), lk->filename contained the name\nof the file that was being locked, not the name of the lockfile.  If a\nsignal would have been raised during that interval, then the signal\nhandler would have deleted the valuable file!\n\nWe could probably continue to use the filename field to encode the\nstate by being careful to write characters 1..N-1 of the filename\nfirst, and then overwrite the NUL at filename[0] with the first\ncharacter of the filename, but that would be awkward and error-prone.\n\nSo, instead of encoding part of the lock_file state in the filename\nfield, add a new bit \"LOCK_FLAGS_LOCKFILE_ACTIVE\" to flags, and use\nthis bit to distinguish between a lock_file object that is active\nvs. one that is inactive.  Be careful to set this bit only when\nfilename really contains the name of a file that should be deleted on\ncleanup.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 55 +++++++++++++++++++++++++++++++++++++++----------------\n 1 file changed, 39 insertions(+), 16 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 24aaa53..b955cca 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -27,11 +27,14 @@\n  * Instead of (3), the change can be rolled back by deleting lockfile.\n  *\n  * This module keeps track of all locked files in lock_file_list.\n- * When the first file is locked, it registers an atexit(3) handler;\n- * when the program exits, the handler rolls back any files that have\n- * been locked but were never committed or rolled back.\n+ * When the first file is locked, it registers an atexit(3) handler\n+ * and a signal handler; when the program exits, the handler rolls\n+ * back any files that have been locked but were never committed or\n+ * rolled back.\n  *\n- * A lock_file object can be in several states:\n+ * Because the signal handler can be called at any time, a lock_file\n+ * object must always be in a well-defined state.  The possible states\n+ * are as follows:\n  *\n  * - Uninitialized.  In this state the object's flags field must be\n  *   zero but the rest of its contents need not be initialized.  As\n@@ -40,18 +43,25 @@\n  *   is set.\n  *\n  * - Locked, lockfile open (after hold_lock_file_for_update() or\n- *   hold_lock_file_for_append()).  In this state, the lockfile\n- *   exists, filename holds the filename of the lockfile, fd holds a\n- *   file descriptor open for writing to the lockfile, and owner holds\n- *   the PID of the process that locked the file.\n+ *   hold_lock_file_for_append()).  In this state:\n+ *   - the lockfile exists\n+ *   - flags & LOCK_FLAGS_LOCKFILE_ACTIVE is set\n+ *   - filename holds the filename of the lockfile\n+ *   - fd holds a file descriptor open for writing to the lockfile\n+ *   - owner holds the PID of the process that locked the file\n  *\n- * - Locked, lockfile closed (after close_lock_file()).  Same as the\n- *   previous state, except that the lockfile is closed and fd is -1.\n+ * - Locked, lockfile closed (after close_lock_file() or an\n+ *   unsuccessful commit_lock_file()).  Same as the previous state,\n+ *   except that the lockfile is closed and fd is -1.\n  *\n- * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a\n- *   failed attempt to lock).  In this state, filename[0] == '\\0' and\n- *   fd is -1.  The object is left registered in the lock_file_list,\n- *   and flags & LOCK_FLAGS_ON_LIST is set.\n+ * - Unlocked (after rollback_lock_file(), a successful\n+ *   commit_lock_file(), or a failed attempt to lock).  In this state:\n+ *   - flags & LOCK_FLAGS_LOCKFILE_ACTIVE is unset\n+ *   - filename[0] == '\\0' (usually, though there are transitory states\n+ *     in which this condition doesn't hold)\n+ *   - fd is -1\n+ *   - the object is left registered in the lock_file_list,\n+ *     and flags & LOCK_FLAGS_ON_LIST is set.\n  *\n  * See Documentation/api-lockfile.txt for more information.\n  */\n@@ -61,6 +71,13 @@\n /* This lock_file instance is in the lock_file_list */\n #define LOCK_FLAGS_ON_LIST 0x01\n \n+/*\n+ * The filename field points at an active lockfile (i.e., a file that\n+ * needs to be deleted if the process dies)\n+ */\n+#define LOCK_FLAGS_LOCKFILE_ACTIVE 0x02\n+\n+\n static struct lock_file *lock_file_list;\n static const char *alternate_index_output;\n \n@@ -192,6 +209,8 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\tatexit(remove_lock_file);\n \t}\n \n+\tassert(!(lk->flags & LOCK_FLAGS_LOCKFILE_ACTIVE));\n+\n \tif (!(lk->flags & LOCK_FLAGS_ON_LIST)) {\n \t\t/* Initialize *lk and add it to lock_file_list: */\n \t\tlk->fd = -1;\n@@ -214,6 +233,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\treturn -1;\n \t}\n \tlk->owner = getpid();\n+\tlk->flags |= LOCK_FLAGS_LOCKFILE_ACTIVE;\n \tif (adjust_shared_perm(lk->filename)) {\n \t\terror(\"cannot fix permission bits on %s\", lk->filename);\n \t\trollback_lock_file(lk);\n@@ -301,7 +321,7 @@ int commit_lock_file(struct lock_file *lk)\n \tif (lk->fd >= 0 && close_lock_file(lk))\n \t\treturn -1;\n \n-\tif (!lk->filename[0])\n+\tif (!(lk->flags & LOCK_FLAGS_LOCKFILE_ACTIVE))\n \t\treturn 0;\n \n \tstrcpy(result_file, lk->filename);\n@@ -310,6 +330,7 @@ int commit_lock_file(struct lock_file *lk)\n \n \tif (rename(lk->filename, result_file))\n \t\treturn -1;\n+\tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n \tlk->filename[0] = 0;\n \treturn 0;\n }\n@@ -334,6 +355,7 @@ int commit_locked_index(struct lock_file *lk)\n \t\t\treturn -1;\n \t\tif (rename(lk->filename, alternate_index_output))\n \t\t\treturn -1;\n+\t\tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n \t\tlk->filename[0] = 0;\n \t\treturn 0;\n \t}\n@@ -343,10 +365,11 @@ int commit_locked_index(struct lock_file *lk)\n \n void rollback_lock_file(struct lock_file *lk)\n {\n-\tif (lk->filename[0]) {\n+\tif (lk->flags & LOCK_FLAGS_LOCKFILE_ACTIVE) {\n \t\tif (lk->fd >= 0)\n \t\t\tclose_lock_file(lk);\n \t\tunlink_or_warn(lk->filename);\n+\t\tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n \t\tlk->filename[0] = 0;\n \t}\n }\n-- \n1.9.1\n"},{"id":"238434","messageId":"1396827247-28465-20-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 19/25] try_merge_strategy(): remove redundant lock_file allocation","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:01Z","receivedAt":"2014-04-06T23:34:01Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"By the time the \"if\" block is entered, the lock_file instance from the\nmain function block is no longer in use, so re-use that one instead of\nallocating a second one.\n\nNote that the \"lock\" variable in the \"if\" block shadowed the \"lock\"\nvariable at function scope, so the only change needed is to remove the\ninner definition.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/merge.c | 1 -\n 1 file changed, 1 deletion(-)\n\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex e15d0e1..f714961 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -671,7 +671,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,\n \tif (!strcmp(strategy, \"recursive\") || !strcmp(strategy, \"subtree\")) {\n \t\tint clean, x;\n \t\tstruct commit *result;\n-\t\tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \t\tint index_fd;\n \t\tstruct commit_list *reversed = NULL;\n \t\tstruct merge_options o;\n-- \n1.9.1\n"},{"id":"238427","messageId":"1396827247-28465-21-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 20/25] try_merge_strategy(): use a statically-allocated lock_file object","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:02Z","receivedAt":"2014-04-06T23:34:02Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Even the one lockfile object needn't be allocated each time the\nfunction is called.  Instead, define one statically-allocated\nlock_file object and reuse it for every call.\n\nSuggested-by: Jeff King <peff@peff.net>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/merge.c | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex f714961..0cde893 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -657,16 +657,16 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,\n \t\t\t      struct commit_list *remoteheads,\n \t\t\t      struct commit *head, const char *head_arg)\n {\n+\tstatic struct lock_file lock;\n \tint index_fd;\n-\tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \n-\tindex_fd = hold_locked_index(lock, 1);\n+\tindex_fd = hold_locked_index(&lock, 1);\n \trefresh_cache(REFRESH_QUIET);\n \tif (active_cache_changed &&\n \t\t\t(write_cache(index_fd, active_cache, active_nr) ||\n-\t\t\t commit_locked_index(lock)))\n+\t\t\t commit_locked_index(&lock)))\n \t\treturn error(_(\"Unable to write index.\"));\n-\trollback_lock_file(lock);\n+\trollback_lock_file(&lock);\n \n \tif (!strcmp(strategy, \"recursive\") || !strcmp(strategy, \"subtree\")) {\n \t\tint clean, x;\n@@ -699,14 +699,14 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,\n \t\tfor (j = common; j; j = j->next)\n \t\t\tcommit_list_insert(j->item, &reversed);\n \n-\t\tindex_fd = hold_locked_index(lock, 1);\n+\t\tindex_fd = hold_locked_index(&lock, 1);\n \t\tclean = merge_recursive(&o, head,\n \t\t\t\tremoteheads->item, reversed, &result);\n \t\tif (active_cache_changed &&\n \t\t\t\t(write_cache(index_fd, active_cache, active_nr) ||\n-\t\t\t\t commit_locked_index(lock)))\n+\t\t\t\t commit_locked_index(&lock)))\n \t\t\tdie (_(\"unable to write %s\"), get_index_file());\n-\t\trollback_lock_file(lock);\n+\t\trollback_lock_file(&lock);\n \t\treturn clean ? 0 : 1;\n \t} else {\n \t\treturn try_merge_command(strategy, xopts_nr, xopts,\n-- \n1.9.1\n"},{"id":"238433","messageId":"1396827247-28465-22-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 21/25] commit_lock_file(): use a strbuf to manage temporary space","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:03Z","receivedAt":"2014-04-06T23:34:03Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Avoid relying on the filename length restrictions that are currently\nchecked by lock_file().\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 12 +++++++-----\n 1 file changed, 7 insertions(+), 5 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex b955cca..bf018b5 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -316,7 +316,8 @@ int close_lock_file(struct lock_file *lk)\n \n int commit_lock_file(struct lock_file *lk)\n {\n-\tchar result_file[PATH_MAX];\n+\tstatic struct strbuf result_file = STRBUF_INIT;\n+\tint err;\n \n \tif (lk->fd >= 0 && close_lock_file(lk))\n \t\treturn -1;\n@@ -324,11 +325,12 @@ int commit_lock_file(struct lock_file *lk)\n \tif (!(lk->flags & LOCK_FLAGS_LOCKFILE_ACTIVE))\n \t\treturn 0;\n \n-\tstrcpy(result_file, lk->filename);\n \t/* remove \".lock\": */\n-\tresult_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;\n-\n-\tif (rename(lk->filename, result_file))\n+\tstrbuf_add(&result_file, lk->filename,\n+\t\t   strlen(lk->filename) - LOCK_SUFFIX_LEN);\n+\terr = rename(lk->filename, result_file.buf);\n+\tstrbuf_reset(&result_file);\n+\tif (err)\n \t\treturn -1;\n \tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n \tlk->filename[0] = 0;\n-- \n1.9.1\n"},{"id":"238429","messageId":"1396827247-28465-23-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 22/25] Change lock_file::filename into a strbuf","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:04Z","receivedAt":"2014-04-06T23:34:04Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"For now, we still make sure to allocate at least PATH_MAX characters\nfor the strbuf because resolve_symlink() doesn't know how to expand\nthe space for its return value.  (That will be fixed in a moment.)\n\nAnother alternative would be to just use a strbuf as scratch space in\nlock_file() but then store a pointer to the naked string in struct\nlock_file.  But lock_file objects are often reused.  By reusing the\nsame strbuf, we can avoid having to reallocate the string most times\nwhen a lock_file object is reused.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/commit.c | 12 ++++++------\n builtin/reflog.c |  2 +-\n cache.h          |  2 +-\n config.c         |  6 +++---\n lockfile.c       | 45 +++++++++++++++++++--------------------------\n refs.c           |  6 +++---\n shallow.c        |  6 +++---\n 7 files changed, 36 insertions(+), 43 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 2e2d8bb..1833595 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -330,7 +330,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix\n \t\t\tdie(_(\"unable to create temporary index\"));\n \n \t\told_index_env = getenv(INDEX_ENVIRONMENT);\n-\t\tsetenv(INDEX_ENVIRONMENT, index_lock.filename, 1);\n+\t\tsetenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);\n \n \t\tif (interactive_add(argc, argv, prefix, patch_interactive) != 0)\n \t\t\tdie(_(\"interactive add failed\"));\n@@ -341,10 +341,10 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix\n \t\t\tunsetenv(INDEX_ENVIRONMENT);\n \n \t\tdiscard_cache();\n-\t\tread_cache_from(index_lock.filename);\n+\t\tread_cache_from(index_lock.filename.buf);\n \n \t\tcommit_style = COMMIT_NORMAL;\n-\t\treturn index_lock.filename;\n+\t\treturn index_lock.filename.buf;\n \t}\n \n \t/*\n@@ -368,7 +368,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix\n \t\t    close_lock_file(&index_lock))\n \t\t\tdie(_(\"unable to write new_index file\"));\n \t\tcommit_style = COMMIT_NORMAL;\n-\t\treturn index_lock.filename;\n+\t\treturn index_lock.filename.buf;\n \t}\n \n \t/*\n@@ -453,9 +453,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix\n \t\tdie(_(\"unable to write temporary index file\"));\n \n \tdiscard_cache();\n-\tread_cache_from(false_lock.filename);\n+\tread_cache_from(false_lock.filename.buf);\n \n-\treturn false_lock.filename;\n+\treturn false_lock.filename.buf;\n }\n \n static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,\ndiff --git a/builtin/reflog.c b/builtin/reflog.c\nindex c12a9784..d7df34e 100644\n--- a/builtin/reflog.c\n+++ b/builtin/reflog.c\n@@ -431,7 +431,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,\n \t\t\t write_str_in_full(lock->lock_fd, \"\\n\") != 1 ||\n \t\t\t close_ref(lock) < 0)) {\n \t\t\tstatus |= error(\"Couldn't write %s\",\n-\t\t\t\tlock->lk->filename);\n+\t\t\t\t\tlock->lk->filename.buf);\n \t\t\tunlink(newlog_path);\n \t\t} else if (rename(newlog_path, log_file)) {\n \t\t\tstatus |= error(\"cannot rename %s to %s\",\ndiff --git a/cache.h b/cache.h\nindex 59ce5cd..6ab2483 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -538,7 +538,7 @@ struct lock_file {\n \tint fd;\n \tpid_t owner;\n \tunsigned char flags;\n-\tchar filename[PATH_MAX];\n+\tstruct strbuf filename;\n };\n #define LOCK_DIE_ON_ERROR 1\n #define LOCK_NODEREF 2\ndiff --git a/config.c b/config.c\nindex 6821cef..78c3dad 100644\n--- a/config.c\n+++ b/config.c\n@@ -1706,7 +1706,7 @@ out_free:\n \treturn ret;\n \n write_err_out:\n-\tret = write_error(lock->filename);\n+\tret = write_error(lock->filename.buf);\n \tgoto out_free;\n \n }\n@@ -1821,7 +1821,7 @@ int git_config_rename_section_in_file(const char *config_filename,\n \t\t\t\t}\n \t\t\t\tstore.baselen = strlen(new_name);\n \t\t\t\tif (!store_write_section(out_fd, new_name)) {\n-\t\t\t\t\tret = write_error(lock->filename);\n+\t\t\t\t\tret = write_error(lock->filename.buf);\n \t\t\t\t\tgoto out;\n \t\t\t\t}\n \t\t\t\t/*\n@@ -1847,7 +1847,7 @@ int git_config_rename_section_in_file(const char *config_filename,\n \t\t\tcontinue;\n \t\tlength = strlen(output);\n \t\tif (write_in_full(out_fd, output, length) != length) {\n-\t\t\tret = write_error(lock->filename);\n+\t\t\tret = write_error(lock->filename.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\ndiff --git a/lockfile.c b/lockfile.c\nindex bf018b5..3befd7e 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -196,13 +196,6 @@ static char *resolve_symlink(char *p, size_t s)\n \n static int lock_file(struct lock_file *lk, const char *path, int flags)\n {\n-\t/*\n-\t * subtract LOCK_SUFFIX_LEN from size to make sure there's\n-\t * room for adding \".lock\" for the lock file name:\n-\t */\n-\tstatic const size_t max_path_len = sizeof(lk->filename) -\n-\t\t\t\t\t   LOCK_SUFFIX_LEN;\n-\n \tif (!lock_file_list) {\n \t\t/* One-time initialization */\n \t\tsigchain_push_common(remove_lock_file_on_signal);\n@@ -216,26 +209,26 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\tlk->fd = -1;\n \t\tlk->owner = 0;\n \t\tlk->flags |= LOCK_FLAGS_ON_LIST;\n-\t\tlk->filename[0] = 0;\n+\t\tstrbuf_init(&lk->filename, PATH_MAX);\n \t\tlk->next = lock_file_list;\n \t\tlock_file_list = lk;\n \t}\n \n-\tif (strlen(path) >= max_path_len)\n-\t\treturn -1;\n-\tstrcpy(lk->filename, path);\n-\tif (!(flags & LOCK_NODEREF))\n-\t\tresolve_symlink(lk->filename, max_path_len);\n-\tstrcat(lk->filename, \".lock\");\n-\tlk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);\n+\tstrbuf_addstr(&lk->filename, path);\n+\tif (!(flags & LOCK_NODEREF)) {\n+\t\tresolve_symlink(lk->filename.buf, lk->filename.alloc);\n+\t\tstrbuf_setlen(&lk->filename, strlen(lk->filename.buf));\n+\t}\n+\tstrbuf_addstr(&lk->filename, \".lock\");\n+\tlk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);\n \tif (lk->fd < 0) {\n-\t\tlk->filename[0] = 0;\n+\t\tstrbuf_reset(&lk->filename);\n \t\treturn -1;\n \t}\n \tlk->owner = getpid();\n \tlk->flags |= LOCK_FLAGS_LOCKFILE_ACTIVE;\n-\tif (adjust_shared_perm(lk->filename)) {\n-\t\terror(\"cannot fix permission bits on %s\", lk->filename);\n+\tif (adjust_shared_perm(lk->filename.buf)) {\n+\t\terror(\"cannot fix permission bits on %s\", lk->filename.buf);\n \t\trollback_lock_file(lk);\n \t\treturn -1;\n \t}\n@@ -326,14 +319,14 @@ int commit_lock_file(struct lock_file *lk)\n \t\treturn 0;\n \n \t/* remove \".lock\": */\n-\tstrbuf_add(&result_file, lk->filename,\n-\t\t   strlen(lk->filename) - LOCK_SUFFIX_LEN);\n-\terr = rename(lk->filename, result_file.buf);\n+\tstrbuf_add(&result_file, lk->filename.buf,\n+\t\t   lk->filename.len - LOCK_SUFFIX_LEN);\n+\terr = rename(lk->filename.buf, result_file.buf);\n \tstrbuf_reset(&result_file);\n \tif (err)\n \t\treturn -1;\n \tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n-\tlk->filename[0] = 0;\n+\tstrbuf_reset(&lk->filename);\n \treturn 0;\n }\n \n@@ -355,10 +348,10 @@ int commit_locked_index(struct lock_file *lk)\n \tif (alternate_index_output) {\n \t\tif (lk->fd >= 0 && close_lock_file(lk))\n \t\t\treturn -1;\n-\t\tif (rename(lk->filename, alternate_index_output))\n+\t\tif (rename(lk->filename.buf, alternate_index_output))\n \t\t\treturn -1;\n \t\tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n-\t\tlk->filename[0] = 0;\n+\t\tstrbuf_reset(&lk->filename);\n \t\treturn 0;\n \t}\n \telse\n@@ -370,8 +363,8 @@ void rollback_lock_file(struct lock_file *lk)\n \tif (lk->flags & LOCK_FLAGS_LOCKFILE_ACTIVE) {\n \t\tif (lk->fd >= 0)\n \t\t\tclose_lock_file(lk);\n-\t\tunlink_or_warn(lk->filename);\n+\t\tunlink_or_warn(lk->filename.buf);\n \t\tlk->flags &= ~LOCK_FLAGS_LOCKFILE_ACTIVE;\n-\t\tlk->filename[0] = 0;\n+\t\tstrbuf_reset(&lk->filename);\n \t}\n }\ndiff --git a/refs.c b/refs.c\nindex cd471ab..15d9e04 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2489,8 +2489,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)\n \t\t * loose.  The loose file name is the same as the\n \t\t * lockfile name, minus \".lock\":\n \t\t */\n-\t\tchar *loose_filename = xmemdupz(lock->lk->filename,\n-\t\t\t\t\t\tstrlen(lock->lk->filename) - 5);\n+\t\tchar *loose_filename = xmemdupz(lock->lk->filename.buf,\n+\t\t\t\t\t\tlock->lk->filename.len - 5);\n \t\tint err = unlink_or_warn(loose_filename);\n \t\tfree(loose_filename);\n \t\tif (err && errno != ENOENT)\n@@ -2828,7 +2828,7 @@ int write_ref_sha1(struct ref_lock *lock,\n \tif (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||\n \t    write_in_full(lock->lock_fd, &term, 1) != 1\n \t\t|| close_ref(lock) < 0) {\n-\t\terror(\"Couldn't write %s\", lock->lk->filename);\n+\t\terror(\"Couldn't write %s\", lock->lk->filename.buf);\n \t\tunlock_ref(lock);\n \t\treturn -1;\n \t}\ndiff --git a/shallow.c b/shallow.c\nindex 0b267b6..cb29a66 100644\n--- a/shallow.c\n+++ b/shallow.c\n@@ -269,8 +269,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,\n \tif (write_shallow_commits(&sb, 0, extra)) {\n \t\tif (write_in_full(fd, sb.buf, sb.len) != sb.len)\n \t\t\tdie_errno(\"failed to write to %s\",\n-\t\t\t\t  shallow_lock->filename);\n-\t\t*alternate_shallow_file = shallow_lock->filename;\n+\t\t\t\t  shallow_lock->filename.buf);\n+\t\t*alternate_shallow_file = shallow_lock->filename.buf;\n \t} else\n \t\t/*\n \t\t * is_repository_shallow() sees empty string as \"no\n@@ -316,7 +316,7 @@ void prune_shallow(int show_only)\n \tif (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {\n \t\tif (write_in_full(fd, sb.buf, sb.len) != sb.len)\n \t\t\tdie_errno(\"failed to write to %s\",\n-\t\t\t\t  shallow_lock.filename);\n+\t\t\t\t  shallow_lock.filename.buf);\n \t\tcommit_lock_file(&shallow_lock);\n \t} else {\n \t\tunlink(git_path(\"shallow\"));\n-- \n1.9.1\n"},{"id":"238428","messageId":"1396827247-28465-24-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 23/25] resolve_symlink(): use a strbuf for internal scratch space","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:05Z","receivedAt":"2014-04-06T23:34:05Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Aside from shortening and simplifying the code, this removes another\nplace where the path name length is arbitrarily limited.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 33 ++++++++++++---------------------\n 1 file changed, 12 insertions(+), 21 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 3befd7e..c4e32a9 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -150,44 +150,35 @@ static char *last_path_elm(char *p)\n static char *resolve_symlink(char *p, size_t s)\n {\n \tint depth = MAXDEPTH;\n+\tstatic struct strbuf link = STRBUF_INIT;\n \n \twhile (depth--) {\n-\t\tchar link[PATH_MAX];\n-\t\tint link_len = readlink(p, link, sizeof(link));\n-\t\tif (link_len < 0) {\n-\t\t\t/* not a symlink anymore */\n-\t\t\treturn p;\n-\t\t}\n-\t\telse if (link_len < sizeof(link))\n-\t\t\t/* readlink() never null-terminates */\n-\t\t\tlink[link_len] = '\\0';\n-\t\telse {\n-\t\t\twarning(\"%s: symlink too long\", p);\n-\t\t\treturn p;\n-\t\t}\n+\t\tif (strbuf_readlink(&link, p, strlen(p)) < 0)\n+\t\t\tbreak;\n \n-\t\tif (is_absolute_path(link)) {\n+\t\tif (is_absolute_path(link.buf)) {\n \t\t\t/* absolute path simply replaces p */\n-\t\t\tif (link_len < s)\n-\t\t\t\tstrcpy(p, link);\n+\t\t\tif (link.len < s)\n+\t\t\t\tstrcpy(p, link.buf);\n \t\t\telse {\n \t\t\t\twarning(\"%s: symlink too long\", p);\n-\t\t\t\treturn p;\n+\t\t\t\tbreak;\n \t\t\t}\n \t\t} else {\n \t\t\t/*\n-\t\t\t * link is a relative path, so I must replace the\n+\t\t\t * link is a relative path, so replace the\n \t\t\t * last element of p with it.\n \t\t\t */\n \t\t\tchar *r = (char *)last_path_elm(p);\n-\t\t\tif (r - p + link_len < s)\n-\t\t\t\tstrcpy(r, link);\n+\t\t\tif (r - p + link.len < s)\n+\t\t\t\tstrcpy(r, link.buf);\n \t\t\telse {\n \t\t\t\twarning(\"%s: symlink too long\", p);\n-\t\t\t\treturn p;\n+\t\t\t\tbreak;\n \t\t\t}\n \t\t}\n \t}\n+\tstrbuf_reset(&link);\n \treturn p;\n }\n \n-- \n1.9.1\n"},{"id":"238424","messageId":"1396827247-28465-25-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 24/25] resolve_symlink(): take a strbuf parameter","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:06Z","receivedAt":"2014-04-06T23:34:06Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Change resolve_symlink() to take a strbuf rather than a string as\nparameter.  This simplifies the code and removes an arbitrary pathname\nlength restriction.  It also means that lock_file's filename field no\nlonger needs to be initialized to a large size.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 53 +++++++++++++++++++----------------------------------\n 1 file changed, 19 insertions(+), 34 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex c4e32a9..8e7bcda 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -133,53 +133,40 @@ static char *last_path_elm(char *p)\n #define MAXDEPTH 5\n \n /*\n- * p = path that may be a symlink\n- * s = full size of p\n+ * path contains a path that might be a symlink.\n  *\n- * If p is a symlink, attempt to overwrite p with a path to the real\n- * file or directory (which may or may not exist), following a chain of\n- * symlinks if necessary.  Otherwise, leave p unmodified.\n+ * If path is a symlink, attempt to overwrite it with a path to the\n+ * real file or directory (which may or may not exist), following a\n+ * chain of symlinks if necessary.  Otherwise, leave path unmodified.\n  *\n- * This is a best-effort routine.  If an error occurs, p will either be\n- * left unmodified or will name a different symlink in a symlink chain\n- * that started with p's initial contents.\n- *\n- * Always returns p.\n+ * This is a best-effort routine.  If an error occurs, path will\n+ * either be left unmodified or will name a different symlink in a\n+ * symlink chain that started with the original path.\n  */\n-\n-static char *resolve_symlink(char *p, size_t s)\n+static void resolve_symlink(struct strbuf *path)\n {\n \tint depth = MAXDEPTH;\n \tstatic struct strbuf link = STRBUF_INIT;\n \n \twhile (depth--) {\n-\t\tif (strbuf_readlink(&link, p, strlen(p)) < 0)\n+\t\tif (strbuf_readlink(&link, path->buf, path->len) < 0)\n \t\t\tbreak;\n \n-\t\tif (is_absolute_path(link.buf)) {\n+\t\tif (is_absolute_path(link.buf))\n \t\t\t/* absolute path simply replaces p */\n-\t\t\tif (link.len < s)\n-\t\t\t\tstrcpy(p, link.buf);\n-\t\t\telse {\n-\t\t\t\twarning(\"%s: symlink too long\", p);\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t} else {\n+\t\t\tstrbuf_reset(path);\n+\t\telse {\n \t\t\t/*\n \t\t\t * link is a relative path, so replace the\n \t\t\t * last element of p with it.\n \t\t\t */\n-\t\t\tchar *r = (char *)last_path_elm(p);\n-\t\t\tif (r - p + link.len < s)\n-\t\t\t\tstrcpy(r, link.buf);\n-\t\t\telse {\n-\t\t\t\twarning(\"%s: symlink too long\", p);\n-\t\t\t\tbreak;\n-\t\t\t}\n+\t\t\tchar *r = last_path_elm(path->buf);\n+\t\t\tstrbuf_setlen(path, r - path->buf);\n \t\t}\n+\n+\t\tstrbuf_addbuf(path, &link);\n \t}\n \tstrbuf_reset(&link);\n-\treturn p;\n }\n \n /* We append \".lock\" to the filename to derive the lockfile name: */\n@@ -200,16 +187,14 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)\n \t\tlk->fd = -1;\n \t\tlk->owner = 0;\n \t\tlk->flags |= LOCK_FLAGS_ON_LIST;\n-\t\tstrbuf_init(&lk->filename, PATH_MAX);\n+\t\tstrbuf_init(&lk->filename, 0);\n \t\tlk->next = lock_file_list;\n \t\tlock_file_list = lk;\n \t}\n \n \tstrbuf_addstr(&lk->filename, path);\n-\tif (!(flags & LOCK_NODEREF)) {\n-\t\tresolve_symlink(lk->filename.buf, lk->filename.alloc);\n-\t\tstrbuf_setlen(&lk->filename, strlen(lk->filename.buf));\n-\t}\n+\tif (!(flags & LOCK_NODEREF))\n+\t\tresolve_symlink(&lk->filename);\n \tstrbuf_addstr(&lk->filename, \".lock\");\n \tlk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);\n \tif (lk->fd < 0) {\n-- \n1.9.1\n"},{"id":"238426","messageId":"1396827247-28465-26-git-send-email-mhagger@alum.mit.edu","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"[PATCH v2 25/25] trim_last_path_elm(): replace last_path_elm()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-06T23:34:07Z","receivedAt":"2014-04-06T23:34:07Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Rewrite last_path_elm() to take a strbuf parameter and to trim off the\nlast path name element in place rather than returning a pointer to the\nbeginning of the last path name element.  This simplifies the function\na bit and makes it integrate better with its caller, which is also\nstrbuf-based.  Rename the function accordingly.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n lockfile.c | 38 ++++++++++++++++----------------------\n 1 file changed, 16 insertions(+), 22 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex 8e7bcda..f9c8515 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -100,32 +100,28 @@ static void remove_lock_file_on_signal(int signo)\n }\n \n /*\n- * p = absolute or relative path name\n+ * path = absolute or relative path name\n  *\n- * Return a pointer into p showing the beginning of the last path name\n- * element.  If p is empty or the root directory (\"/\"), just return p.\n+ * Remove the last path name element from path (leaving the preceding\n+ * \"/\", if any).  If path is empty or the root directory (\"/\"), set\n+ * path to the empty string.\n  */\n-static char *last_path_elm(char *p)\n+static void trim_last_path_elm(struct strbuf *path)\n {\n-\t/* r starts pointing to null at the end of the string */\n-\tchar *r = strchr(p, '\\0');\n-\n-\tif (r == p)\n-\t\treturn p; /* just return empty string */\n-\n-\tr--; /* back up to last non-null character */\n+\tint i = path->len;\n \n \t/* back up past trailing slashes, if any */\n-\twhile (r > p && *r == '/')\n-\t\tr--;\n+\twhile (i && path->buf[i - 1] == '/')\n+\t\ti--;\n \n \t/*\n-\t * then go backwards until I hit a slash, or the beginning of\n-\t * the string\n+\t * then go backwards until a slash, or the beginning of the\n+\t * string\n \t */\n-\twhile (r > p && *(r-1) != '/')\n-\t\tr--;\n-\treturn r;\n+\twhile (i && path->buf[i - 1] != '/')\n+\t\ti--;\n+\n+\tstrbuf_setlen(path, i);\n }\n \n \n@@ -155,14 +151,12 @@ static void resolve_symlink(struct strbuf *path)\n \t\tif (is_absolute_path(link.buf))\n \t\t\t/* absolute path simply replaces p */\n \t\t\tstrbuf_reset(path);\n-\t\telse {\n+\t\telse\n \t\t\t/*\n \t\t\t * link is a relative path, so replace the\n \t\t\t * last element of p with it.\n \t\t\t */\n-\t\t\tchar *r = last_path_elm(path->buf);\n-\t\t\tstrbuf_setlen(path, r - path->buf);\n-\t\t}\n+\t\t\ttrim_last_path_elm(path);\n \n \t\tstrbuf_addbuf(path, &link);\n \t}\n-- \n1.9.1\n"},{"id":"238435","messageId":"534242AC.7030908@viscovery.net","threadId":"36356","inReplyTo":"1396827247-28465-19-git-send-email-mhagger@alum.mit.edu","subject":"Re: [PATCH v2 18/25] lockfile: avoid transitory invalid states","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2014-04-07T06:16:12Z","receivedAt":"2014-04-07T06:16:12Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 4/7/2014 1:34, schrieb Michael Haggerty:\n> Because remove_lock_file() can be called any time by the signal\n> handler, it is important that any lock_file objects that are in the\n> lock_file_list are always in a valid state.  And since lock_file\n> objects are often reused (but are never removed from lock_file_list),\n> that means we have to be careful whenever mutating a lock_file object\n> to always keep it in a well-defined state.\n> ...\n> So, instead of encoding part of the lock_file state in the filename\n> field, add a new bit \"LOCK_FLAGS_LOCKFILE_ACTIVE\" to flags, and use\n> this bit to distinguish between a lock_file object that is active\n> vs. one that is inactive.  Be careful to set this bit only when\n> filename really contains the name of a file that should be deleted on\n> cleanup.\n\nSince this flag is primarily for communication between the main code and a\nsignal handler, the only safe way is to define the flag as volatile\nsig_atomic_t, not to make it a bit of a larger type!\n\n-- Hannes\n"},{"id":"238438","messageId":"53428846.7060104@alum.mit.edu","threadId":"36356","inReplyTo":"534242AC.7030908@viscovery.net","subject":"Re: [PATCH v2 18/25] lockfile: avoid transitory invalid states","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-07T11:13:10Z","receivedAt":"2014-04-07T11:13:10Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 04/07/2014 08:16 AM, Johannes Sixt wrote:\n> Am 4/7/2014 1:34, schrieb Michael Haggerty:\n>> Because remove_lock_file() can be called any time by the signal\n>> handler, it is important that any lock_file objects that are in the\n>> lock_file_list are always in a valid state.  And since lock_file\n>> objects are often reused (but are never removed from lock_file_list),\n>> that means we have to be careful whenever mutating a lock_file object\n>> to always keep it in a well-defined state.\n>> ...\n>> So, instead of encoding part of the lock_file state in the filename\n>> field, add a new bit \"LOCK_FLAGS_LOCKFILE_ACTIVE\" to flags, and use\n>> this bit to distinguish between a lock_file object that is active\n>> vs. one that is inactive.  Be careful to set this bit only when\n>> filename really contains the name of a file that should be deleted on\n>> cleanup.\n> \n> Since this flag is primarily for communication between the main code and a\n> signal handler, the only safe way is to define the flag as volatile\n> sig_atomic_t, not to make it a bit of a larger type!\n\nThanks for the feedback.  You are obviously right, and I will fix it.\n\nBut I have a feeling that this line of thought is going to lead to the\nsignal handler's not being able to do anything.  How far can we afford\nto pursue strict correctness?  We have\n\n\tstruct lock_file {\n\t\tstruct lock_file *next;\n\t\tint fd;\n\t\tpid_t owner;\n\t\tchar on_list;\n\t\tchar filename[PATH_MAX];\n\t};\n\n\tstatic struct lock_file *lock_file_list;\n\nThe signal handler currently reads\n\n    lock_file_list\n    lock_file::next\n    lock_file::fd\n    lock_file::owner\n    lock_file::filename\n    *lock_file::filename\n\nand writes lock_file_list.  Among other things it calls close(),\nunlink(), vsnprintf(), and fprintf() (the last two via warning()).\n\nBut most of these actions are undefined under the C99 standard:\n\n> If the signal occurs other than as the result of calling the abort\n> or raise function, the behavior is undefined if the signal handler\n> refers to any object with static storage duration other than by\n> assigning a value to an object declared as volatile sig_atomic_t, or\n> the signal handler calls any function in the standard library other\n> than the abort function, the _Exit function, or the signal function\n> with the first argument equal to the signal number corresponding to\n> the signal that caused the invocation of the handler.\n\nI don't have time to rewrite *all* of Git right now, so how can we get\nreasonable safety and portability within a feasible amount of work?\n\nMichael\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"},{"id":"238439","messageId":"5342962A.8020608@viscovery.net","threadId":"36356","inReplyTo":"53428846.7060104@alum.mit.edu","subject":"Re: [PATCH v2 18/25] lockfile: avoid transitory invalid states","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2014-04-07T12:12:26Z","receivedAt":"2014-04-07T12:12:26Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 4/7/2014 13:13, schrieb Michael Haggerty:\n> On 04/07/2014 08:16 AM, Johannes Sixt wrote:\n>> Am 4/7/2014 1:34, schrieb Michael Haggerty:\n>>> So, instead of encoding part of the lock_file state in the filename\n>>> field, add a new bit \"LOCK_FLAGS_LOCKFILE_ACTIVE\" to flags, and use\n>>> this bit to distinguish between a lock_file object that is active\n>>> vs. one that is inactive.  Be careful to set this bit only when\n>>> filename really contains the name of a file that should be deleted on\n>>> cleanup.\n>>\n>> Since this flag is primarily for communication between the main code and a\n>> signal handler, the only safe way is to define the flag as volatile\n>> sig_atomic_t, not to make it a bit of a larger type!\n> \n> Thanks for the feedback.  You are obviously right, and I will fix it.\n> \n> But I have a feeling that this line of thought is going to lead to the\n> signal handler's not being able to do anything.  How far can we afford\n> to pursue strict correctness?  ...\n> \n> The signal handler currently reads\n> \n>     lock_file_list\n>     lock_file::next\n>     lock_file::fd\n>     lock_file::owner\n>     lock_file::filename\n>     *lock_file::filename\n> \n> and writes lock_file_list.  Among other things it calls close(),\n> unlink(), vsnprintf(), and fprintf() (the last two via warning()).\n> \n> But most of these actions are undefined under the C99 standard:\n\nGood point. But not all is lost because some of the functions are\nwell-defined under POSIX, particularly close() and unlink(). (*printf are\nnot, though.)\n\n> I don't have time to rewrite *all* of Git right now, so how can we get\n> reasonable safety and portability within a feasible amount of work?\n\nIt shouldn't be *that* bad. We can make all members volatile, except\nfilename (because we wouldn't be able to strcpy(lk->filename, ...) without\na type cast).\n\nHow far *do* you want to go? I'm certainly not opposed to field-test your\ncurrent changeset (plus and adjustment to use sig_atomic_t) -- overall it\nis an improvement. And then we will see how it works.\n\nJust as food for thought: A compiler barrier should be sufficient to\ninhibit that the compiler reorders code across accesses of the volatile\nflag. Like in the main code:\n\n\tstrcpy(lk->filename, ...);\n\tBARRIER();\n\tlk->is_active = 1;\t/* volatile sig_atomic_t */\n\nand in the signal handler:\n\n\tif (!lk->is_active)\n\t\treturn;\n\tBARRIER();\n\tunlink(lk->filename);\n\nwith some suitable definition of BARRIER(). I don't think that we need an\nexplicit memory barrier (in practice) because that should be implied by\nthe context switch leading to the signal handler.\n\n-- Hannes\n"},{"id":"238440","messageId":"5342A451.5020905@alum.mit.edu","threadId":"36356","inReplyTo":"5342962A.8020608@viscovery.net","subject":"Re: [PATCH v2 18/25] lockfile: avoid transitory invalid states","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-07T13:12:49Z","receivedAt":"2014-04-07T13:12:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 04/07/2014 02:12 PM, Johannes Sixt wrote:\n> Am 4/7/2014 13:13, schrieb Michael Haggerty:\n>> On 04/07/2014 08:16 AM, Johannes Sixt wrote:\n>>> Am 4/7/2014 1:34, schrieb Michael Haggerty:\n>>>> So, instead of encoding part of the lock_file state in the filename\n>>>> field, add a new bit \"LOCK_FLAGS_LOCKFILE_ACTIVE\" to flags, and use\n>>>> this bit to distinguish between a lock_file object that is active\n>>>> vs. one that is inactive.  Be careful to set this bit only when\n>>>> filename really contains the name of a file that should be deleted on\n>>>> cleanup.\n>>>\n>>> Since this flag is primarily for communication between the main code and a\n>>> signal handler, the only safe way is to define the flag as volatile\n>>> sig_atomic_t, not to make it a bit of a larger type!\n>>\n>> Thanks for the feedback.  You are obviously right, and I will fix it.\n>>\n>> But I have a feeling that this line of thought is going to lead to the\n>> signal handler's not being able to do anything.  How far can we afford\n>> to pursue strict correctness?  ...\n>>\n>> The signal handler currently reads\n>>\n>>     lock_file_list\n>>     lock_file::next\n>>     lock_file::fd\n>>     lock_file::owner\n>>     lock_file::filename\n>>     *lock_file::filename\n>>\n>> and writes lock_file_list.  Among other things it calls close(),\n>> unlink(), vsnprintf(), and fprintf() (the last two via warning()).\n>>\n>> But most of these actions are undefined under the C99 standard:\n> \n> Good point. But not all is lost because some of the functions are\n> well-defined under POSIX, particularly close() and unlink(). (*printf are\n> not, though.)\n> \n>> I don't have time to rewrite *all* of Git right now, so how can we get\n>> reasonable safety and portability within a feasible amount of work?\n> \n> It shouldn't be *that* bad. We can make all members volatile, except\n> filename (because we wouldn't be able to strcpy(lk->filename, ...) without\n> a type cast).\n> \n> How far *do* you want to go? I'm certainly not opposed to field-test your\n> current changeset (plus and adjustment to use sig_atomic_t) -- overall it\n> is an improvement. And then we will see how it works.\n\nFor now I think I'd just like to get the biggest problems fixed without\nmaking anything worse.  Given that there might be a GSoC student working\nin this neighborhood, he/she might be able to take up the baton.\n\nI changed the patch series to use a new \"volatile sig_atomic_t active\"\nfield rather than a bit in a \"flags\" field.  I'll wait a short time to\nsee if there is more feedback before pushing it to the list; meanwhile\nyou can find it here if you have time to look at it and/or test it:\n\n    http://github.com/mhagger/git, branch \"lock-correctness\"\n\nMichael\n\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"},{"id":"238494","messageId":"20140407184627.GA19342@sigill.intra.peff.net","threadId":"36356","inReplyTo":"1396827247-28465-2-git-send-email-mhagger@alum.mit.edu","subject":"Re: [PATCH v2 01/25] api-lockfile: expand the documentation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-04-07T18:46:27Z","receivedAt":"2014-04-07T18:46:27Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Apr 07, 2014 at 01:33:43AM +0200, Michael Haggerty wrote:\n\n> +unable_to_lock_error::\n> +\n> +\tEmit an error describing that there was an error locking the\n> +\tspecified path.  The err parameter should be the errno of the\n> +\tproblem that caused the failure.\n> +\n> +unable_to_lock_die::\n> +\n> +\tLike `unable_to_lock_error()`, but also `die()`.\n\nThe die() function is still called unable_to_lock_index_die() at this\npoint in the series.  Presumably you change it later. I don't know if it\nis worth caring about the order or not; it's a doc change, so it's not\nlike it breaks bisectability.\n\n-Peff\n"},{"id":"238495","messageId":"20140407184728.GB19342@sigill.intra.peff.net","threadId":"36356","inReplyTo":"1396827247-28465-3-git-send-email-mhagger@alum.mit.edu","subject":"Re: [PATCH v2 02/25] unable_to_lock_die(): rename function from unable_to_lock_index_die()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-04-07T18:47:29Z","receivedAt":"2014-04-07T18:47:29Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Apr 07, 2014 at 01:33:44AM +0200, Michael Haggerty wrote:\n\n> This function is used for other things besides the index, so rename it\n> accordingly.\n\nOh, and here it is. I should really have just read ahead before\nresponding to patch 1.\n\nWe can either re-order these first two, or just not worry about it.\n\n-Peff\n"},{"id":"238500","messageId":"20140407193131.GC19342@sigill.intra.peff.net","threadId":"36356","inReplyTo":"1396827247-28465-18-git-send-email-mhagger@alum.mit.edu","subject":"Re: [PATCH v2 17/25] commit_lock_file(): make committing an unlocked lockfile a NOP","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-04-07T19:31:31Z","receivedAt":"2014-04-07T19:31:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Apr 07, 2014 at 01:33:59AM +0200, Michael Haggerty wrote:\n\n> It was previously a bug to call commit_lock_file() with a lock_file\n> object that was not active (an illegal access would happen within the\n> function).  It was presumably never done, but this would be an easy\n> programming error to overlook.  So guard the file-renaming code with\n> an if statement to change committing an unlocked file into a NOP.\n> \n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n> Alternatively, we could make it a fatal error (e.g., an assert() or\n> if...die()) to call commit_lock_file() on an unlocked file, or omit a\n> warning in this case.  But since it is so hard to test code related to\n> locking failures, I have the feeling that such an error is most likely\n> to occur in some error-handling path, maybe when some other lockfile\n> acquisition has failed, and it would be better to let the code\n> continue its attempted cleanup instead of dying.  But it would be easy\n> to persuade me to change my opinion.\n\nYeah, I would have expected a die(\"BUG\") here.\n\nI think it is worth making it a fatal mistake and catching it. Rolling\nback an uninitialized lockfile is probably OK; we are canceling an\noperation that never started. But committing a lockfile that we didn't\nactually fill out could be a sign of a serious error, and we may be\npropagating a bogus success code. E.g., imagine that receive-pack claims\nto have written your ref, but actually commit_lock_file was a silent\nNOP. I'd much rather have it die loudly so we can track down the case.\n\n-Peff\n"},{"id":"238502","messageId":"20140407193834.GD19342@sigill.intra.peff.net","threadId":"36356","inReplyTo":"5342A451.5020905@alum.mit.edu","subject":"Re: [PATCH v2 18/25] lockfile: avoid transitory invalid states","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-04-07T19:38:34Z","receivedAt":"2014-04-07T19:38:34Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Apr 07, 2014 at 03:12:49PM +0200, Michael Haggerty wrote:\n\n> > How far *do* you want to go? I'm certainly not opposed to field-test your\n> > current changeset (plus and adjustment to use sig_atomic_t) -- overall it\n> > is an improvement. And then we will see how it works.\n> \n> For now I think I'd just like to get the biggest problems fixed without\n> making anything worse.  Given that there might be a GSoC student working\n> in this neighborhood, he/she might be able to take up the baton.\n> \n> I changed the patch series to use a new \"volatile sig_atomic_t active\"\n> field rather than a bit in a \"flags\" field.\n\nThat seems like a good place to stop for now.\n\nIf any code touches the fields, it can unset the \"active\" flag (even\ntemporarily), and restore it when the structure is in a known state.\n\nI'm not sure if we can ever reach full safety. If you have an event\nloop, the sane thing to do is set an atomic flag in your signal handler\nand then handle it on the next iteration of the loop. But all of our\nsignal handlers are jumped to from arbitrary code, and are just going to\ndie(). There's nothing to return to, so any useful work we do has to be\ndone from the handler.\n\n-Peff\n"},{"id":"238506","messageId":"20140407194043.GE19342@sigill.intra.peff.net","threadId":"36356","inReplyTo":"1396827247-28465-1-git-send-email-mhagger@alum.mit.edu","subject":"Re: [PATCH v2 00/25] Lockfile correctness and refactoring","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-04-07T19:40:44Z","receivedAt":"2014-04-07T19:40:44Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Apr 07, 2014 at 01:33:42AM +0200, Michael Haggerty wrote:\n\n> This is a second attempt at renovating the lock file code.  Thanks to\n> Peff, Junio, Torsten, and Eric for their helpful reviews of v1.\n> \n> v1 of this patch series [1] did some refactoring and then added a new\n> feature to the lock_file API: the ability to activate a new version of\n> a locked file while retaining the lock.\n> \n> But the review of v1 turned up even more correctness issues in the\n> existing implementation of lock files.  So this v2 dials back the\n> scope of the changes (it omits the new feature) but does more work to\n> fix problems with the current lock file implementation.\n> \n> The main theme of this patch series is to better define the state\n> diagram for lock_file objects and to fix code that left them in\n> incorrect, indeterminate, or unexpected states.  There are also a few\n> patches that convert several functions to use strbufs instead of\n> limiting pathnames to a maximum length.\n\nLooks OK to me, modulo the few comments I sent.\n\nI still think resolve_symref should probably not be \"best-effort\", but\nthat can come later on top.\n\n-Peff\n"},{"id":"238533","messageId":"53440201.5040107@alum.mit.edu","threadId":"36356","inReplyTo":"20140407184728.GB19342@sigill.intra.peff.net","subject":"Re: [PATCH v2 02/25] unable_to_lock_die(): rename function from unable_to_lock_index_die()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-08T14:04:49Z","receivedAt":"2014-04-08T14:04:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 04/07/2014 08:47 PM, Jeff King wrote:\n> On Mon, Apr 07, 2014 at 01:33:44AM +0200, Michael Haggerty wrote:\n> \n>> This function is used for other things besides the index, so rename it\n>> accordingly.\n> \n> Oh, and here it is. I should really have just read ahead before\n> responding to patch 1.\n> \n> We can either re-order these first two, or just not worry about it.\n\nThanks for catching this.  Yes, swapping the first two patches solves\nthe problem and doesn't conflict.\n\nMichael\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"},{"id":"238818","messageId":"534B9A88.1000101@alum.mit.edu","threadId":"36356","inReplyTo":"20140407193131.GC19342@sigill.intra.peff.net","subject":"Re: [PATCH v2 17/25] commit_lock_file(): make committing an unlocked lockfile a NOP","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2014-04-14T08:21:28Z","receivedAt":"2014-04-14T08:21:28Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 04/07/2014 09:31 PM, Jeff King wrote:\n> On Mon, Apr 07, 2014 at 01:33:59AM +0200, Michael Haggerty wrote:\n> \n>> It was previously a bug to call commit_lock_file() with a lock_file\n>> object that was not active (an illegal access would happen within the\n>> function).  It was presumably never done, but this would be an easy\n>> programming error to overlook.  So guard the file-renaming code with\n>> an if statement to change committing an unlocked file into a NOP.\n>> [...]\n> \n> Yeah, I would have expected a die(\"BUG\") here.\n> \n> I think it is worth making it a fatal mistake and catching it. Rolling\n> back an uninitialized lockfile is probably OK; we are canceling an\n> operation that never started. But committing a lockfile that we didn't\n> actually fill out could be a sign of a serious error, and we may be\n> propagating a bogus success code. E.g., imagine that receive-pack claims\n> to have written your ref, but actually commit_lock_file was a silent\n> NOP. I'd much rather have it die loudly so we can track down the case.\n\nOK, I will change this to a fatal error in v3.\n\nMichael\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"}]}