From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v0 2/3] index_fd(): split into two helper functions
Date: Sun, 8 May 2011 01:47:34 -0700 [thread overview]
Message-ID: <1304844455-23570-3-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1304844455-23570-1-git-send-email-gitster@pobox.com>
Split out the case where we do not know the size of the input (hence we
read everything into a strbuf before doing anything) to index_pipe(), and
the other case where we mmap or read the whole data to index_bulk().
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
sha1_file.c | 42 +++++++++++++++++++++++++++++++-----------
1 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 17c179c..49416b0 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2619,22 +2619,29 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
return ret;
}
+static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
+ const char *path, unsigned flags)
+{
+ struct strbuf sbuf = STRBUF_INIT;
+ int ret;
+
+ if (strbuf_read(&sbuf, fd, 4096) >= 0)
+ ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
+ else
+ ret = -1;
+ strbuf_release(&sbuf);
+ return ret;
+}
+
#define SMALL_FILE_SIZE (32*1024)
-int index_fd(unsigned char *sha1, int fd, struct stat *st,
- enum object_type type, const char *path, unsigned flags)
+static int index_core(unsigned char *sha1, int fd, size_t size,
+ enum object_type type, const char *path,
+ unsigned flags)
{
int ret;
- size_t size = xsize_t(st->st_size);
- if (!S_ISREG(st->st_mode)) {
- struct strbuf sbuf = STRBUF_INIT;
- if (strbuf_read(&sbuf, fd, 4096) >= 0)
- ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
- else
- ret = -1;
- strbuf_release(&sbuf);
- } else if (!size) {
+ if (!size) {
ret = index_mem(sha1, NULL, size, type, path, flags);
} else if (size <= SMALL_FILE_SIZE) {
char *buf = xmalloc(size);
@@ -2648,6 +2655,19 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
ret = index_mem(sha1, buf, size, type, path, flags);
munmap(buf, size);
}
+ return ret;
+}
+
+int index_fd(unsigned char *sha1, int fd, struct stat *st,
+ enum object_type type, const char *path, unsigned flags)
+{
+ int ret;
+ size_t size = xsize_t(st->st_size);
+
+ if (!S_ISREG(st->st_mode))
+ ret = index_pipe(sha1, fd, type, path, flags);
+ else
+ ret = index_core(sha1, fd, size, type, path, flags);
close(fd);
return ret;
}
--
1.7.5.1.268.gce5bd
next prev parent reply other threads:[~2011-05-08 8:47 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-08 8:47 [PATCH v0 0/3] git add a-Big-file Junio C Hamano
2011-05-08 8:47 ` [PATCH v0 1/3] index_fd(): turn write_object and format_check arguments into one flag Junio C Hamano
2011-05-08 8:47 ` Junio C Hamano [this message]
2011-05-08 8:47 ` [PATCH v0 3/3] Bigfile: teach "git add" to send a large file straight to a pack Junio C Hamano
2011-05-08 10:19 ` Nguyen Thai Ngoc Duy
2011-05-08 17:37 ` Junio C Hamano
2011-05-09 22:04 ` [PATCH 0/4] convert.c clean-up Junio C Hamano
2011-05-09 22:04 ` [PATCH 1/4] convert: rename the "eol" global variable to "core_eol" Junio C Hamano
2011-05-09 22:04 ` [PATCH 2/4] convert: give saner names to crlf/eol variables, types and functions Junio C Hamano
2011-05-09 22:05 ` [PATCH 3/4] convert: make it safer to add conversion attributes Junio C Hamano
2011-05-09 22:05 ` [PATCH 4/4] convert: make it harder to screw up adding a conversion attribute Junio C Hamano
2011-05-10 12:59 ` [PATCH 0/4] convert.c clean-up Nguyen Thai Ngoc Duy
2011-05-10 14:09 ` Junio C Hamano
2011-05-09 14:05 ` [PATCH v0 3/3] Bigfile: teach "git add" to send a large file straight to a pack Shawn Pearce
2011-05-09 15:58 ` Junio C Hamano
2011-05-09 16:14 ` Junio C Hamano
2011-05-29 18:20 ` Ævar Arnfjörð Bjarmason
2011-05-29 20:29 ` Junio C Hamano
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=1304844455-23570-3-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
/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).