From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH 18/22] lockfile: also keep track of the filename of the file being locked
Date: Tue, 1 Apr 2014 17:58:26 +0200 [thread overview]
Message-ID: <1396367910-7299-19-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1396367910-7299-1-git-send-email-mhagger@alum.mit.edu>
This reduces the amount of string editing gyrations and makes it
unnecessary for callers to know how to derive the filename from the
lock_filename.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
cache.h | 1 +
lockfile.c | 57 ++++++++++++++++++++++++++++++++-------------------------
refs.c | 10 ++--------
3 files changed, 35 insertions(+), 33 deletions(-)
diff --git a/cache.h b/cache.h
index 3d0a835..0fefc66 100644
--- a/cache.h
+++ b/cache.h
@@ -538,6 +538,7 @@ struct lock_file {
int fd;
pid_t owner;
unsigned char flags;
+ struct strbuf filename;
struct strbuf lock_filename;
};
#define LOCK_DIE_ON_ERROR 1
diff --git a/lockfile.c b/lockfile.c
index 87b40c4..07b5c21 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -35,24 +35,26 @@
*
* - Uninitialized. In this state the object's flags field must be
* zero but the rest of the contents need not be initialized. In
- * particular, the lock_filename strbuf should *not* be initialized
- * externally. The first time the object is used in any way, it is
- * initialized, permanently registered in the lock_file_list, and
- * flags & LOCK_FLAGS_ON_LIST is set.
+ * particular, the filename and lock_filename strbufs should *not*
+ * be initialized externally. The first time the object is used in
+ * any way, it is initialized, permanently registered in the
+ * lock_file_list, and flags & LOCK_FLAGS_ON_LIST is set.
*
* - Locked, lockfile open (after hold_lock_file_for_update() or
* hold_lock_file_for_append()). In this state, the lockfile
- * exists, lock_filename holds the filename of the lockfile, fd
- * holds a file descriptor open for writing to the lockfile, and
- * owner holds the PID of the process that locked the file.
+ * exists, filename holds the filename of the locked file,
+ * lock_filename holds the filename of the lockfile, fd holds a file
+ * descriptor open for writing to the lockfile, and owner holds the
+ * PID of the process that locked the file.
*
* - Locked, lockfile closed (after close_lock_file()). Same as the
* previous state, except that the lockfile is closed and fd is -1.
*
* - Unlocked (after commit_lock_file(), rollback_lock_file(), or a
- * failed attempt to lock). In this state, lock_filename is the
- * empty string and fd is -1. The object is left registered in the
- * lock_file_list, and flags & LOCK_FLAGS_ON_LIST is set.
+ * failed attempt to lock). In this state, filename and
+ * lock_filename are the empty string and fd is -1. The object is
+ * left registered in the lock_file_list, and flags &
+ * LOCK_FLAGS_ON_LIST is set.
*
* See Documentation/api-lockfile.txt for more information.
*/
@@ -164,25 +166,30 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
lk->owner = getpid();
if (lk->flags & LOCK_FLAGS_ON_LIST) {
- assert(!lk->lock_filename.len); /* object not already in use */
- if (strbuf_avail(&lk->lock_filename) < path_len + LOCK_SUFFIX_LEN)
- strbuf_grow(&lk->lock_filename, path_len + LOCK_SUFFIX_LEN);
+ /* Sanity check that object is not already in use: */
+ assert(!lk->filename.len);
+ assert(!lk->lock_filename.len);
} else {
/* Initialize *lk and add it to lock_file_list: */
lk->fd = -1;
lk->flags |= LOCK_FLAGS_ON_LIST;
- strbuf_init(&lk->lock_filename, path_len + LOCK_SUFFIX_LEN);
+ strbuf_init(&lk->filename, path_len);
+ strbuf_init(&lk->lock_filename, 0);
lk->next = lock_file_list;
lock_file_list = lk;
}
- strbuf_add(&lk->lock_filename, path, path_len);
+ strbuf_add(&lk->filename, path, path_len);
if (!(flags & LOCK_NODEREF))
- resolve_symlink(&lk->lock_filename);
+ resolve_symlink(&lk->filename);
+
+ strbuf_grow(&lk->lock_filename, lk->filename.len + LOCK_SUFFIX_LEN);
+ strbuf_addbuf(&lk->lock_filename, &lk->filename);
strbuf_addstr(&lk->lock_filename, ".lock");
lk->fd = open(lk->lock_filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
if (lk->fd < 0) {
+ strbuf_setlen(&lk->filename, 0);
strbuf_setlen(&lk->lock_filename, 0);
return -1;
}
@@ -269,18 +276,16 @@ int close_lock_file(struct lock_file *lk)
int commit_lock_file(struct lock_file *lk)
{
- char *result_file;
- size_t path_len = lk->lock_filename.len - LOCK_SUFFIX_LEN;
int err = 0;
if (lk->fd >= 0 && close_lock_file(lk))
return -1;
- result_file = xmemdupz(lk->lock_filename.buf, path_len);
- if (rename(lk->lock_filename.buf, result_file))
+ if (rename(lk->lock_filename.buf, lk->filename.buf)) {
err = -1;
- else
+ } else {
+ strbuf_setlen(&lk->filename, 0);
strbuf_setlen(&lk->lock_filename, 0);
- free(result_file);
+ }
return err;
}
@@ -304,19 +309,21 @@ int commit_locked_index(struct lock_file *lk)
return -1;
if (rename(lk->lock_filename.buf, alternate_index_output))
return -1;
+ strbuf_setlen(&lk->filename, 0);
strbuf_setlen(&lk->lock_filename, 0);
return 0;
- }
- else
+ } else {
return commit_lock_file(lk);
+ }
}
void rollback_lock_file(struct lock_file *lk)
{
- if (lk->lock_filename.len) {
+ if (lk->filename.len) {
if (lk->fd >= 0)
close_lock_file(lk);
unlink_or_warn(lk->lock_filename.buf);
+ strbuf_setlen(&lk->filename, 0);
strbuf_setlen(&lk->lock_filename, 0);
}
}
diff --git a/refs.c b/refs.c
index 93abc94..85967e7 100644
--- a/refs.c
+++ b/refs.c
@@ -2485,14 +2485,8 @@ static int repack_without_ref(const char *refname)
static int delete_ref_loose(struct ref_lock *lock, int flag)
{
if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
- /*
- * loose. The loose file name is the same as the
- * lockfile name, minus ".lock":
- */
- char *loose_filename = xmemdupz(lock->lk->lock_filename.buf,
- lock->lk->lock_filename.len - 5);
- int err = unlink_or_warn(loose_filename);
- free(loose_filename);
+ /* loose */
+ int err = unlink_or_warn(lock->lk->filename.buf);
if (err && errno != ENOENT)
return 1;
}
--
1.9.0
next prev parent reply other threads:[~2014-04-01 15:59 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-01 15:58 [PATCH 00/22] Lockfile refactoring and pre-activation Michael Haggerty
2014-04-01 15:58 ` [PATCH 01/22] t3204: test deleting references when lock files already exist Michael Haggerty
2014-04-01 19:53 ` Jeff King
2014-04-02 10:28 ` Michael Haggerty
2014-04-01 15:58 ` [PATCH 02/22] try_merge_strategy(): remove redundant lock_file allocation Michael Haggerty
2014-04-01 19:56 ` Jeff King
2014-04-02 10:53 ` Michael Haggerty
2014-04-02 16:53 ` Junio C Hamano
2014-04-03 12:43 ` Michael Haggerty
2014-04-01 15:58 ` [PATCH 03/22] rollback_lock_file(): do not clear filename redundantly Michael Haggerty
2014-04-01 15:58 ` [PATCH 04/22] rollback_lock_file(): set fd to -1 Michael Haggerty
2014-04-01 19:59 ` Jeff King
2014-04-02 16:58 ` Junio C Hamano
2014-04-06 21:45 ` Michael Haggerty
2014-04-07 16:37 ` Junio C Hamano
2014-04-01 15:58 ` [PATCH 05/22] lockfile: unlock file if lockfile permissions cannot be adjusted Michael Haggerty
2014-04-01 20:02 ` Jeff King
2014-04-01 20:05 ` Jeff King
2014-04-02 6:47 ` Torsten Bögershausen
2014-04-06 22:02 ` Michael Haggerty
2014-04-01 15:58 ` [PATCH 06/22] hold_lock_file_for_append(): release lock on errors Michael Haggerty
2014-04-01 15:58 ` [PATCH 07/22] lock_file(): always add lock_file object to lock_file_list Michael Haggerty
2014-04-01 20:16 ` Jeff King
2014-04-02 17:01 ` Junio C Hamano
2014-04-06 21:54 ` Michael Haggerty
2014-04-07 9:36 ` Jeff King
2014-04-01 15:58 ` [PATCH 08/22] struct lock_file: replace on_list field with flags field Michael Haggerty
2014-04-01 15:58 ` [PATCH 09/22] api-lockfile: expand the documentation Michael Haggerty
2014-04-01 20:19 ` Jeff King
2014-04-02 11:36 ` Michael Haggerty
2014-04-01 15:58 ` [PATCH 10/22] lockfile.c: document the various states of lock_file objects Michael Haggerty
2014-04-01 15:58 ` [PATCH 11/22] lockfile: define a constant LOCK_SUFFIX_LEN Michael Haggerty
2014-04-02 17:27 ` Junio C Hamano
2014-04-01 15:58 ` [PATCH 12/22] delete_ref_loose(): don't muck around in the lock_file's filename Michael Haggerty
2014-04-01 20:21 ` Jeff King
2014-04-02 11:50 ` Michael Haggerty
2014-04-02 6:52 ` Torsten Bögershausen
2014-04-02 6:55 ` Jeff King
2014-04-01 15:58 ` [PATCH 13/22] config: change write_error() to take a (struct lock_file *) argument Michael Haggerty
2014-04-02 6:58 ` Torsten Bögershausen
2014-04-06 22:04 ` Michael Haggerty
2014-04-02 17:29 ` Junio C Hamano
2014-04-01 15:58 ` [PATCH 14/22] lockfile: use strbufs when handling (most) paths Michael Haggerty
2014-04-01 20:28 ` Jeff King
2014-04-02 17:16 ` Junio C Hamano
2014-04-01 15:58 ` [PATCH 15/22] resolve_symlink(): use a strbuf internally Michael Haggerty
2014-04-01 15:58 ` [PATCH 16/22] commit_lock_file(): don't work with a fixed-length buffer Michael Haggerty
2014-04-01 15:58 ` [PATCH 17/22] lock_file(): exit early if lockfile cannot be opened Michael Haggerty
2014-04-01 15:58 ` Michael Haggerty [this message]
2014-04-02 17:19 ` [PATCH 18/22] lockfile: also keep track of the filename of the file being locked Junio C Hamano
2014-04-06 22:05 ` Michael Haggerty
2014-04-01 15:58 ` [PATCH 19/22] struct lock_file: rename lock_filename field to staging_filename Michael Haggerty
2014-04-01 15:58 ` [PATCH 20/22] remove_lock_file(): call rollback_lock_file() Michael Haggerty
2014-04-01 15:58 ` [PATCH 21/22] lockfile: extract a function reset_lock_file() Michael Haggerty
2014-04-02 7:06 ` Eric Sunshine
2014-04-02 13:37 ` Michael Haggerty
2014-04-01 15:58 ` [PATCH 22/22] lockfile: allow new file contents to be written while retaining lock Michael Haggerty
2014-04-01 20:39 ` Jeff King
2014-04-02 7:20 ` Eric Sunshine
2014-04-02 17:26 ` Junio C Hamano
2014-04-01 20:44 ` [PATCH 00/22] Lockfile refactoring and pre-activation Jeff King
2014-04-03 11:42 ` Michael Haggerty
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1396367910-7299-19-git-send-email-mhagger@alum.mit.edu \
--to=mhagger@alum.mit.edu \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).