summaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-10 10:35:09 (GMT)
committerJunio C Hamano <gitster@pobox.com>2007-09-10 19:50:58 (GMT)
commitfd17f5b5f77716bf90098c6e49e3cf7fd9f56306 (patch)
tree7b6ed050e8283c9fe80d0c8229281de4bed4dccf /sha1_file.c
parent635d043f30c60f5ec8121bd94304e529f90407c7 (diff)
downloadgit-fd17f5b5f77716bf90098c6e49e3cf7fd9f56306.zip
git-fd17f5b5f77716bf90098c6e49e3cf7fd9f56306.tar.gz
git-fd17f5b5f77716bf90098c6e49e3cf7fd9f56306.tar.bz2
Replace all read_fd use with strbuf_read, and get rid of it.
This brings builtin-stripspace, builtin-tag and mktag to use strbufs. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c60
1 files changed, 9 insertions, 51 deletions
diff --git a/sha1_file.c b/sha1_file.c
index 9978a58..aea1096 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -14,6 +14,7 @@
#include "tag.h"
#include "tree.h"
#include "refs.h"
+#include "strbuf.h"
#ifndef O_NOATIME
#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
@@ -2302,68 +2303,25 @@ int has_sha1_file(const unsigned char *sha1)
return find_sha1_file(sha1, &st) ? 1 : 0;
}
-/*
- * reads from fd as long as possible into a supplied buffer of size bytes.
- * If necessary the buffer's size is increased using realloc()
- *
- * returns 0 if anything went fine and -1 otherwise
- *
- * The buffer is always NUL-terminated, not including it in returned size.
- *
- * NOTE: both buf and size may change, but even when -1 is returned
- * you still have to free() it yourself.
- */
-int read_fd(int fd, char **return_buf, unsigned long *return_size)
-{
- char *buf = *return_buf;
- unsigned long size = *return_size;
- ssize_t iret;
- unsigned long off = 0;
-
- if (!buf || size <= 1) {
- size = 1024;
- buf = xrealloc(buf, size);
- }
-
- do {
- iret = xread(fd, buf + off, (size - 1) - off);
- if (iret > 0) {
- off += iret;
- if (off == size - 1) {
- size = alloc_nr(size);
- buf = xrealloc(buf, size);
- }
- }
- } while (iret > 0);
-
- buf[off] = '\0';
-
- *return_buf = buf;
- *return_size = off;
-
- if (iret < 0)
- return -1;
- return 0;
-}
-
int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
{
- unsigned long size = 4096;
- char *buf = xmalloc(size);
+ struct strbuf buf;
int ret;
- if (read_fd(fd, &buf, &size)) {
- free(buf);
+ strbuf_init(&buf, 0);
+ if (strbuf_read(&buf, fd, 4096) < 0) {
+ strbuf_release(&buf);
return -1;
}
if (!type)
type = blob_type;
if (write_object)
- ret = write_sha1_file(buf, size, type, sha1);
+ ret = write_sha1_file(buf.buf, buf.len, type, sha1);
else
- ret = hash_sha1_file(buf, size, type, sha1);
- free(buf);
+ ret = hash_sha1_file(buf.buf, buf.len, type, sha1);
+ strbuf_release(&buf);
+
return ret;
}