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>
This commit is contained in:
Pierre Habouzit
2007-09-10 12:35:09 +02:00
committed by Junio C Hamano
parent 635d043f30
commit fd17f5b5f7
5 changed files with 56 additions and 120 deletions

View File

@ -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;
}