From: Jeff King <peff@peff.net>
To: Johannes Schindelin <johannes.schindelin@gmx.de>
Cc: git@vger.kernel.org, Jeff Hostetler <jeffhost@microsoft.com>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] read-cache: close index.lock in do_write_index
Date: Wed, 26 Apr 2017 23:13:11 -0400 [thread overview]
Message-ID: <20170427031311.hfbit6glkx2tw3ru@sigill.intra.peff.net> (raw)
In-Reply-To: <e1b4f9c377ceee296112fa07bd06492a1de1be67.1493237111.git.johannes.schindelin@gmx.de>
On Wed, Apr 26, 2017 at 10:05:23PM +0200, Johannes Schindelin wrote:
> From: Jeff Hostetler <jeffhost@microsoft.com>
>
> Teach do_write_index() to close the index.lock file
> before getting the mtime and updating the istate.timestamp
> fields.
>
> On Windows, a file's mtime is not updated until the file is
> closed. On Linux, the mtime is set after the last flush.
I wondered at first what this would mean for atomicity. The original
code does an fstat, so we're sure to get the timestamp of what we just
wrote.
I think we should be OK after your change, though. We're stat()ing the
lockfile itself, so nobody else should be touching it (because they'd be
violating the lock to do so).
> -static int do_write_index(struct index_state *istate, int newfd,
> +static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
> int strip_extensions)
> [...]
> - if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
> + if (ce_flush(&c, newfd, istate->sha1))
> + return -1;
> + if (close_tempfile(tempfile))
> + return error(_("could not close '%s'"), tempfile->filename.buf);
> + if (lstat(tempfile->filename.buf, &st))
> return -1;
So now we unconditionally close in do_write_index(), but I don't see any
close_tempfile() calls going away. For the call in write_shared_index(),
that's because we either call delete_tempfile() or rename_tempfile(),
either of which would close as needed, but can handle an already-closed
file.
The other caller is do_write_locked_index(), which accepts either a
flag: either COMMIT_LOCK, CLOSE_LOCK, or neither. COMMIT_LOCK is OK; it
can handle the already-closed file. CLOSE_LOCK is obviously fine. It
just becomes a noop. But when neither flag is set, now we close the
lock. Are there any callers that will be affected?
There are three callers, but I think they all eventually trace up to
write_locked_index(). And grepping for callers of that function, it
looks like each one uses either COMMIT_LOCK or CLOSE_LOCK.
So perhaps we'd want to squash in (or perhaps do as a preparatory
patch) something like:
diff --git a/read-cache.c b/read-cache.c
index b0276fd55..db7a812af 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2193,14 +2193,16 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
int ret = do_write_index(istate, &lock->tempfile, 0);
if (ret)
return ret;
+
+ /* Callers must specify exactly one of COMMIT/CLOSE */
assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
(COMMIT_LOCK | CLOSE_LOCK));
+ assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) != 0);
+
if (flags & COMMIT_LOCK)
return commit_locked_index(lock);
- else if (flags & CLOSE_LOCK)
- return close_lock_file(lock);
else
- return ret;
+ return close_lock_file(lock);
}
static int write_split_index(struct index_state *istate,
We could also get rid of CLOSE_LOCK entirely at this point. Or since
these are the only two flags, just turn the flags field into a boolean
"int commit_lock". But doing it as above is perhaps more readable
(callers say CLOSE_LOCK instead of an unannotated "0"), and the extra
assert will catch any topics in flight that add calls using "0" for
flags.
-Peff
next prev parent reply other threads:[~2017-04-27 3:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-26 20:05 [PATCH] read-cache: close index.lock in do_write_index Johannes Schindelin
2017-04-27 3:13 ` Jeff King [this message]
2017-04-27 16:51 ` Jeff Hostetler
2017-04-27 3:21 ` Junio C Hamano
2017-04-27 16:45 ` Jeff Hostetler
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170427031311.hfbit6glkx2tw3ru@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jeffhost@microsoft.com \
--cc=johannes.schindelin@gmx.de \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).