From: Michael Haggerty <mhagger@alum.mit.edu>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
"Junio C Hamano" <gitster@pobox.com>
Cc: Eric Sunshine <sunshine@sunshineco.com>,
Ronnie Sahlberg <sahlberg@google.com>,
git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH v2 05/11] refs.c: refactor resolve_ref_unsafe() to use strbuf internally
Date: Wed, 15 Oct 2014 17:06:17 +0200 [thread overview]
Message-ID: <1413385583-4872-6-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1413385583-4872-1-git-send-email-mhagger@alum.mit.edu>
From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
In the beginning, we had resolve_ref() that returns a buffer owned by
this function. Then we started to move away from that direction because
the buffer could be overwritten by the next resolve_ref() call and
introduced two new functions: resolve_ref_unsafe() and resolve_refdup().
The static buffer is still kept internally.
This patch makes the core of resolve_ref use a strbuf instead of static
buffer. Which makes resolve_refdup() more efficient (no need to copy
from the static buffer to a new buffer). It also removes the (random?)
256 char limit. In future, resolve_ref() could be used directly without
going through resolve_refdup() wrapper.
A minor bonus. resolve_ref(dup) are now more thread-friendly (although I'm
not 100% sure if they are thread-safe yet).
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
cache.h | 1 +
refs.c | 115 +++++++++++++++++++++++++++++++++++++---------------------------
2 files changed, 67 insertions(+), 49 deletions(-)
diff --git a/cache.h b/cache.h
index 3e6a914..e36084d 100644
--- a/cache.h
+++ b/cache.h
@@ -1004,6 +1004,7 @@ extern int read_ref(const char *refname, unsigned char *sha1);
*/
extern const char *resolve_ref_unsafe(const char *ref, unsigned char *sha1, int reading, int *flag);
extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag);
+extern int resolve_ref(const char *refname, struct strbuf *result, unsigned char *sha1, int reading, int *flag);
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
diff --git a/refs.c b/refs.c
index 020ee3f..29ea7e0 100644
--- a/refs.c
+++ b/refs.c
@@ -1397,34 +1397,41 @@ static int handle_missing_loose_ref(const char *refname,
}
}
-/* This function needs to return a meaningful errno on failure */
-const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
+/*
+ * 'result' content will be destroyed. Its value may be undefined if
+ * resolve_ref returns -1.
+ *
+ * This function needs to return a meaningful errno on failure
+ */
+int resolve_ref(const char *refname, struct strbuf *result,
+ unsigned char *sha1, int reading, int *flag)
{
+ struct strbuf buffer = STRBUF_INIT;
int depth = MAXDEPTH;
- ssize_t len;
- char buffer[256];
- static char refname_buffer[256];
+ int ret = -1;
if (flag)
*flag = 0;
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
errno = EINVAL;
- return NULL;
+ return -1;
}
+ strbuf_reset(result);
+ strbuf_addstr(result, refname);
+
for (;;) {
char path[PATH_MAX];
struct stat st;
const char *buf;
- int fd;
if (--depth < 0) {
errno = ELOOP;
- return NULL;
+ break;
}
- git_snpath(path, sizeof(path), "%s", refname);
+ git_snpath(path, sizeof(path), "%s", result->buf);
/*
* We might have to loop back here to avoid a race
@@ -1437,30 +1444,26 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
*/
stat_ref:
if (lstat(path, &st) < 0) {
- if (errno == ENOENT) {
- if (handle_missing_loose_ref(refname, sha1,
- reading, flag))
- return NULL;
- return refname;
- } else
- return NULL;
+ if (errno == ENOENT)
+ ret = handle_missing_loose_ref(result->buf, sha1,
+ reading, flag);
+ break;
}
/* Follow "normalized" - ie "refs/.." symlinks by hand */
if (S_ISLNK(st.st_mode)) {
- len = readlink(path, buffer, sizeof(buffer)-1);
- if (len < 0) {
+ /* no need to reset buffer, strbuf_readlink does that */
+ if (strbuf_readlink(&buffer, path, 256) < 0) {
if (errno == ENOENT || errno == EINVAL)
/* inconsistent with lstat; retry */
goto stat_ref;
else
- return NULL;
+ break;
}
- buffer[len] = 0;
- if (starts_with(buffer, "refs/") &&
- !check_refname_format(buffer, 0)) {
- strcpy(refname_buffer, buffer);
- refname = refname_buffer;
+ if (starts_with(buffer.buf, "refs/") &&
+ !check_refname_format(buffer.buf, 0)) {
+ strbuf_reset(result);
+ strbuf_addbuf(result, &buffer);
if (flag)
*flag |= REF_ISSYMREF;
continue;
@@ -1470,34 +1473,24 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
/* Is it a directory? */
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
- return NULL;
+ break;
}
/*
* Anything else, just open it and try to use it as
* a ref
*/
- fd = open(path, O_RDONLY);
- if (fd < 0) {
+ strbuf_reset(&buffer);
+ if (strbuf_read_file(&buffer, path, 256) < 0) {
if (errno == ENOENT)
/* inconsistent with lstat; retry */
goto stat_ref;
else
- return NULL;
- }
- len = read_in_full(fd, buffer, sizeof(buffer)-1);
- if (len < 0) {
- int save_errno = errno;
- close(fd);
- errno = save_errno;
- return NULL;
+ break;
}
- close(fd);
- while (len && isspace(buffer[len-1]))
- len--;
- buffer[len] = '\0';
+ strbuf_rtrim(&buffer);
- if (skip_prefix(buffer, "ref:", &buf)) {
+ if (skip_prefix(buffer.buf, "ref:", &buf)) {
/* It is a symbolic ref */
if (flag)
*flag |= REF_ISSYMREF;
@@ -1507,9 +1500,10 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
if (flag)
*flag |= REF_ISBROKEN;
errno = EINVAL;
- return NULL;
+ break;
}
- refname = strcpy(refname_buffer, buf);
+ strbuf_reset(result);
+ strbuf_add(result, buf, buffer.buf + buffer.len - buf);
continue;
}
@@ -1517,21 +1511,44 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
* It must be a normal ref. Please note that
* FETCH_HEAD has a second line containing other data.
*/
- if (get_sha1_hex(buffer, sha1) ||
- (buffer[40] != '\0' && !isspace(buffer[40]))) {
+ if (get_sha1_hex(buffer.buf, sha1) ||
+ (buffer.buf[40] != '\0' && !isspace(buffer.buf[40]))) {
if (flag)
*flag |= REF_ISBROKEN;
errno = EINVAL;
- return NULL;
- }
- return refname;
+ } else
+ ret = 0;
+ break;
}
+ strbuf_release(&buffer);
+ return ret;
+}
+
+const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
+{
+ static struct strbuf buf = STRBUF_INIT;
+
+ if (!resolve_ref(refname, &buf, sha1, reading, flag))
+ /*
+ * Please note: true to the "unsafe" name of this
+ * function, we return a pointer to our internal
+ * memory here rather than passing ownership to the
+ * caller by calling strbuf_detach():
+ */
+ return buf.buf;
+ else
+ return NULL;
}
char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
{
- const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
- return ret ? xstrdup(ret) : NULL;
+ struct strbuf buf = STRBUF_INIT;
+ if (!resolve_ref(ref, &buf, sha1, reading, flag))
+ return strbuf_detach(&buf, NULL);
+ else {
+ strbuf_release(&buf);
+ return NULL;
+ }
}
/* The argument to filter_refs */
--
2.1.1
next prev parent reply other threads:[~2014-10-15 15:06 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-15 15:06 [PATCH v2 00/11] Consolidate ref parsing code Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 01/11] strbuf_read_file(): preserve errno on failure Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 02/11] handle_missing_loose_ref(): return an int Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 03/11] resolve_ref_unsafe(): reverse the logic of the symref conditional Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 04/11] resolve_ref_unsafe(): use skip_prefix() to skip over "ref:" Michael Haggerty
2014-10-15 15:06 ` Michael Haggerty [this message]
2014-10-15 15:06 ` [PATCH v2 06/11] refs.c: move ref parsing code out of resolve_ref() Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 07/11] handle_missing_loose_ref(): inline function Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 08/11] resolve_gitlink_ref_recursive(): drop arbitrary refname length limit Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 09/11] refs.c: rewrite resolve_gitlink_ref() to use parse_ref() Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 10/11] resove_gitlink_packed_ref(): inline function Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 11/11] resolve_gitlink_ref(): remove redundant test Michael Haggerty
2014-10-16 20:47 ` [PATCH v2 00/11] Consolidate ref parsing code Junio C Hamano
2014-10-16 21:51 ` Junio C Hamano
2014-10-16 23:23 ` Michael Haggerty
2014-10-16 23:53 ` Duy Nguyen
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=1413385583-4872-6-git-send-email-mhagger@alum.mit.edu \
--to=mhagger@alum.mit.edu \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=sahlberg@google.com \
--cc=sunshine@sunshineco.com \
/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).