aboutsummaryrefslogtreecommitdiffstats
path: root/sha1_file.c
diff options
context:
space:
mode:
authorDaniel Barkalow <barkalow@iabervon.org>2005-07-10 18:25:38 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-07-10 15:39:08 -0700
commita5eda52bfe8f0afe3abc0c7eaceb487c2ffb7ebb (patch)
tree4a34e5e63ba2ff742113e27077b03318d9747dc4 /sha1_file.c
parent454fbbcde3beb5eb36a4554e5b7074f8d501a6f1 (diff)
downloadgit-a5eda52bfe8f0afe3abc0c7eaceb487c2ffb7ebb.tar.gz
[PATCH] write_sha1_to_fd()
Add write_sha1_to_fd(), which writes an object to a file descriptor. This includes support for unpacking it and recompressing it. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/sha1_file.c b/sha1_file.c
index b2914dd2ea..b13578bdf5 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1221,6 +1221,65 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
return 0;
}
+int write_sha1_to_fd(int fd, const unsigned char *sha1)
+{
+ ssize_t size;
+ unsigned long objsize;
+ int posn = 0;
+ char *buf = map_sha1_file_internal(sha1, &objsize, 0);
+ z_stream stream;
+ if (!buf) {
+ unsigned char *unpacked;
+ unsigned long len;
+ char type[20];
+ char hdr[50];
+ int hdrlen;
+ // need to unpack and recompress it by itself
+ unpacked = read_packed_sha1(sha1, type, &len);
+
+ hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
+
+ /* Set it up */
+ memset(&stream, 0, sizeof(stream));
+ deflateInit(&stream, Z_BEST_COMPRESSION);
+ size = deflateBound(&stream, len + hdrlen);
+ buf = xmalloc(size);
+
+ /* Compress it */
+ stream.next_out = buf;
+ stream.avail_out = size;
+
+ /* First header.. */
+ stream.next_in = hdr;
+ stream.avail_in = hdrlen;
+ while (deflate(&stream, 0) == Z_OK)
+ /* nothing */;
+
+ /* Then the data itself.. */
+ stream.next_in = unpacked;
+ stream.avail_in = len;
+ while (deflate(&stream, Z_FINISH) == Z_OK)
+ /* nothing */;
+ deflateEnd(&stream);
+
+ objsize = stream.total_out;
+ }
+
+ do {
+ size = write(fd, buf + posn, objsize - posn);
+ if (size <= 0) {
+ if (!size) {
+ fprintf(stderr, "write closed");
+ } else {
+ perror("write ");
+ }
+ return -1;
+ }
+ posn += size;
+ } while (posn < objsize);
+ return 0;
+}
+
int write_sha1_from_fd(const unsigned char *sha1, int fd)
{
char *filename = sha1_file_name(sha1);