{"thread":{"id":"11648","subject":"[PATCH] refs.c: rework ref_locks by abstracting from underlying struct lock_file","startedAt":"2008-01-16T19:13:38Z","lastAt":"2008-01-16T19:14:30Z","messageCount":2,"participants":["Brandon Casey"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"65585","messageId":"478E5762.8000007@nrlssc.navy.mil","threadId":"11648","inReplyTo":"1200510486-11438-1-git-send-email-casey@nrlssc.navy.mil","subject":"[PATCH] lockfile.c: modify handling of rename failures and alternate_index_output","fromName":"Brandon Casey","fromEmail":"casey@nrlssc.navy.mil","sentAt":"2008-01-16T19:13:38Z","receivedAt":"2008-01-16T19:13:38Z","isPatch":true,"sender":{"key":"drafnel@gmail.com","avatar":"https://avatars.githubusercontent.com/u/921167?v=4"},"body":"commit_lock_file(): if rename() fails, we should not\n  truncate the name of the lockfile. This file should be deleted\n  by rollback_lock_file() or remove_lock_file().\n\ncommit_locked_index(): call close_lock_file() when alternate_index_output\n  is set and handle rename() failures like commit_lock_file().\n---\n lockfile.c |   14 +++++++++-----\n 1 files changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/lockfile.c b/lockfile.c\nindex bcc4786..e8e5dbb 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -171,15 +171,16 @@ 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-\tint i;\n+\tsize_t i;\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 \tresult_file[i] = 0;\n-\ti = rename(lk->filename, result_file);\n+\tif (rename(lk->filename, result_file))\n+\t\treturn -1;\n \tlk->filename[0] = 0;\n-\treturn i;\n+\treturn 0;\n }\n \n int hold_locked_index(struct lock_file *lk, int die_on_error)\n@@ -195,9 +196,12 @@ void set_alternate_index_output(const char *name)\n int commit_locked_index(struct lock_file *lk)\n {\n \tif (alternate_index_output) {\n-\t\tint result = rename(lk->filename, 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\t\treturn -1;\n \t\tlk->filename[0] = 0;\n-\t\treturn result;\n+\t\treturn 0;\n \t}\n \telse\n \t\treturn commit_lock_file(lk);\n-- \n1.5.4.rc3.14.g44397-dirty\n"},{"id":"65584","messageId":"478E5796.7040001@nrlssc.navy.mil","threadId":"11648","inReplyTo":"1200510486-11438-2-git-send-email-casey@nrlssc.navy.mil","subject":"[PATCH] refs.c: rework ref_locks by abstracting from underlying struct lock_file","fromName":"Brandon Casey","fromEmail":"casey@nrlssc.navy.mil","sentAt":"2008-01-16T19:14:30Z","receivedAt":"2008-01-16T19:14:30Z","isPatch":true,"sender":{"key":"drafnel@gmail.com","avatar":"https://avatars.githubusercontent.com/u/921167?v=4"},"body":"Instead of calling close_lock_file() and commit_lock_file() directly,\nwhich take a struct lock_file argument, add two new functions:\nclose_ref() and commit_ref(), which handle calling the previous\nlock_file functions and modifying the ref_lock structure.\n---\n refs.c |   21 ++++++++++++++++++---\n 1 files changed, 18 insertions(+), 3 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 2c78956..e705ed3 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1018,6 +1018,22 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)\n \treturn 1;\n }\n \n+static int close_ref(struct ref_lock *lock)\n+{\n+\tif (close_lock_file(lock->lk))\n+\t\treturn -1;\n+\tlock->lock_fd = -1;\n+\treturn 0;\n+}\n+\n+static int commit_ref(struct ref_lock *lock)\n+{\n+\tif (commit_lock_file(lock->lk))\n+\t\treturn -1;\n+\tlock->lock_fd = -1;\n+\treturn 0;\n+}\n+\n void unlock_ref(struct ref_lock *lock)\n {\n         /* Do not free lock->lk -- atexit() still looks at them */\n@@ -1128,7 +1144,7 @@ int write_ref_sha1(struct ref_lock *lock,\n \t}\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_lock_file(lock->lk) < 0) {\n+\t    \t|| close_ref(lock) < 0) {\n \t\terror(\"Couldn't write %s\", lock->lk->filename);\n \t\tunlock_ref(lock);\n \t\treturn -1;\n@@ -1161,12 +1177,11 @@ int write_ref_sha1(struct ref_lock *lock,\n \t\t    !strcmp(head_ref, lock->ref_name))\n \t\t\tlog_ref_write(\"HEAD\", lock->old_sha1, sha1, logmsg);\n \t}\n-\tif (commit_lock_file(lock->lk)) {\n+\tif (commit_ref(lock)) {\n \t\terror(\"Couldn't set %s\", lock->ref_name);\n \t\tunlock_ref(lock);\n \t\treturn -1;\n \t}\n-\tlock->lock_fd = -1;\n \tunlock_ref(lock);\n \treturn 0;\n }\n-- \n1.5.4.rc3.14.g44397-dirty\n"}]}