From 6a0b0b6de996e2ac7eeb951e3d08f577c11c7e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 30 Nov 2014 16:05:00 +0700 Subject: tree.c: update read_tree_recursive callback to pass strbuf as base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows the callback to use 'base' as a temporary buffer to quickly assemble full path "without" extra allocation. The callback has to restore it afterwards of course. Helped-by: Eric Sunshine Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- archive.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'archive.c') diff --git a/archive.c b/archive.c index 94a9981adf7..9e30246b640 100644 --- a/archive.c +++ b/archive.c @@ -157,18 +157,26 @@ static int write_archive_entry(const unsigned char *sha1, const char *base, return write_entry(args, sha1, path.buf, path.len, mode); } +static int write_archive_entry_buf(const unsigned char *sha1, struct strbuf *base, + const char *filename, unsigned mode, int stage, + void *context) +{ + return write_archive_entry(sha1, base->buf, base->len, + filename, mode, stage, context); +} + static void queue_directory(const unsigned char *sha1, - const char *base, int baselen, const char *filename, + struct strbuf *base, const char *filename, unsigned mode, int stage, struct archiver_context *c) { struct directory *d; - d = xmallocz(sizeof(*d) + baselen + 1 + strlen(filename)); + d = xmallocz(sizeof(*d) + base->len + 1 + strlen(filename)); d->up = c->bottom; - d->baselen = baselen; + d->baselen = base->len; d->mode = mode; d->stage = stage; c->bottom = d; - d->len = sprintf(d->path, "%.*s%s/", baselen, base, filename); + d->len = sprintf(d->path, "%.*s%s/", (int)base->len, base->buf, filename); hashcpy(d->sha1, sha1); } @@ -191,28 +199,28 @@ static int write_directory(struct archiver_context *c) } static int queue_or_write_archive_entry(const unsigned char *sha1, - const char *base, int baselen, const char *filename, + struct strbuf *base, const char *filename, unsigned mode, int stage, void *context) { struct archiver_context *c = context; while (c->bottom && - !(baselen >= c->bottom->len && - !strncmp(base, c->bottom->path, c->bottom->len))) { + !(base->len >= c->bottom->len && + !strncmp(base->buf, c->bottom->path, c->bottom->len))) { struct directory *next = c->bottom->up; free(c->bottom); c->bottom = next; } if (S_ISDIR(mode)) { - queue_directory(sha1, base, baselen, filename, + queue_directory(sha1, base, filename, mode, stage, c); return READ_TREE_RECURSIVE; } if (write_directory(c)) return -1; - return write_archive_entry(sha1, base, baselen, filename, mode, + return write_archive_entry(sha1, base->buf, base->len, filename, mode, stage, context); } @@ -260,7 +268,7 @@ int write_archive_entries(struct archiver_args *args, err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec, args->pathspec.has_wildcard ? queue_or_write_archive_entry : - write_archive_entry, + write_archive_entry_buf, &context); if (err == READ_TREE_RECURSIVE) err = 0; @@ -286,14 +294,14 @@ static const struct archiver *lookup_archiver(const char *name) return NULL; } -static int reject_entry(const unsigned char *sha1, const char *base, - int baselen, const char *filename, unsigned mode, +static int reject_entry(const unsigned char *sha1, struct strbuf *base, + const char *filename, unsigned mode, int stage, void *context) { int ret = -1; if (S_ISDIR(mode)) { struct strbuf sb = STRBUF_INIT; - strbuf_addstr(&sb, base); + strbuf_addbuf(&sb, base); strbuf_addstr(&sb, filename); if (!match_pathspec(context, sb.buf, sb.len, 0, NULL, 1)) ret = READ_TREE_RECURSIVE; -- cgit v1.2.3