From: Linus Torvalds <torvalds@osdl.org>
To: Junio C Hamano <junkio@cox.net>
Cc: Git Mailing List <git@vger.kernel.org>,
Davide Libenzi <davidel@xmailserver.org>
Subject: Re: Add "git-update-ref" to update the HEAD (or other) ref
Date: Sun, 25 Sep 2005 18:07:37 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.58.0509251753460.3308@g5.osdl.org> (raw)
In-Reply-To: <7vhdc8n2xb.fsf@assigned-by-dhcp.cox.net>
On Sun, 25 Sep 2005, Junio C Hamano wrote:
>
> Is something like the one at the end acceptable?
Looking at the patch closer, no, that's incorrect.
"oldsha" doesn't necessarily exist, since there has to be some way to
force the new one. So if "oldval" is NULL, we shouldn't re-verify
anything.
Also, independently of that your patch is buggy because calling
"resolve_ref()" again will overwrite the lockpath, since it's re-used by
the static buffer in git_path(). That's why the "strdup()" is there.
Yeah, yeah, static buffers are evil, but they are also simple and
efficient.
But something like this (on top of my original one) might work.
Linus
----
diff --git a/update-ref.c b/update-ref.c
--- a/update-ref.c
+++ b/update-ref.c
@@ -63,6 +63,19 @@ const char *resolve_ref(const char *path
return path;
}
+static int re_verify(const char *path, unsigned char *oldsha1, unsigned char *currsha1)
+{
+ char buf[40];
+ int fd = open(path, O_RDONLY), nr;
+ if (fd < 0)
+ return -1;
+ nr = read(fd, buf, 40);
+ close(fd);
+ if (nr != 40 || get_sha1_hex(buf, currsha1) < 0)
+ return -1;
+ return memcmp(oldsha1, currsha1, 20) ? -1 : 0;
+}
+
int main(int argc, char **argv)
{
char *hex;
@@ -108,14 +121,18 @@ int main(int argc, char **argv)
unlink(lockpath);
die("Unable to write to %s", lockpath);
}
-
+
/*
- * FIXME!
- *
- * We should re-read the old ref here, and re-verify that it
- * matches "oldsha1". Otherwise there's a small race.
+ * Re-read the ref after getting the lock to verify
*/
+ if (oldval && re_verify(path, oldsha1, currsha1) < 0) {
+ unlink(lockpath);
+ die("Ref lock failed");
+ }
+ /*
+ * Finally, replace the old ref with the new one
+ */
if (rename(lockpath, path) < 0) {
unlink(lockpath);
die("Unable to create %s", path);
prev parent reply other threads:[~2005-09-26 1:07 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-25 18:43 Add "git-update-ref" to update the HEAD (or other) ref Linus Torvalds
2005-09-25 19:05 ` Linus Torvalds
2005-09-25 22:37 ` Junio C Hamano
2005-09-28 2:45 ` [PATCH] Use git-update-ref in scripts Junio C Hamano
2005-09-28 15:21 ` Linus Torvalds
2005-09-28 16:56 ` Junio C Hamano
2005-09-28 17:13 ` Linus Torvalds
2005-09-28 17:29 ` Junio C Hamano
2005-09-28 18:14 ` Linus Torvalds
2005-09-28 18:28 ` Junio C Hamano
2005-09-29 16:07 ` Linus Torvalds
2005-09-28 18:17 ` Junio C Hamano
2005-09-28 19:47 ` Junio C Hamano
2005-09-28 21:19 ` Fredrik Kuivinen
2005-09-29 15:16 ` Linus Torvalds
2005-09-25 23:27 ` Add "git-update-ref" to update the HEAD (or other) ref Junio C Hamano
2005-09-26 0:50 ` Linus Torvalds
2005-09-26 4:25 ` Junio C Hamano
2005-09-26 1:07 ` Linus Torvalds [this message]
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=Pine.LNX.4.58.0509251753460.3308@g5.osdl.org \
--to=torvalds@osdl.org \
--cc=davidel@xmailserver.org \
--cc=git@vger.kernel.org \
--cc=junkio@cox.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