return 0;
}
-static int get_sha1_file(const char *path, unsigned char *result)
-{
- char buffer[60];
- int fd = open(path, O_RDONLY);
- int len;
-
- if (fd < 0)
- return -1;
- len = read(fd, buffer, sizeof(buffer));
- close(fd);
- if (len < 40)
- return -1;
- return get_sha1_hex(buffer, result);
-}
-
static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
*git_graft_file;
static void setup_git_env(void)
return 0;
}
-int get_sha1(const char *str, unsigned char *sha1)
-{
- static const char *prefix[] = {
- "",
- "refs",
- "refs/tags",
- "refs/heads",
- "refs/snap",
- NULL
- };
- const char **p;
-
- if (!get_sha1_hex(str, sha1))
- return 0;
-
- for (p = prefix; *p; p++) {
- char * pathname = git_path("%s/%s", *p, str);
- if (!get_sha1_file(pathname, sha1))
- return 0;
- }
-
- return -1;
-}
-
char * sha1_to_hex(const unsigned char *sha1)
{
static char buffer[50];
free(buffer);
return NULL;
}
+ free(buffer);
/* Now we have the ID of the referred-to object in
* actual_sha1. Check again. */
}
ssize_t size;
unsigned long objsize;
int posn = 0;
- void *buf = map_sha1_file_internal(sha1, &objsize);
+ void *map = map_sha1_file_internal(sha1, &objsize);
+ void *buf = map;
+ void *temp_obj = NULL;
z_stream stream;
+
if (!buf) {
unsigned char *unpacked;
unsigned long len;
memset(&stream, 0, sizeof(stream));
deflateInit(&stream, Z_BEST_COMPRESSION);
size = deflateBound(&stream, len + hdrlen);
- buf = xmalloc(size);
+ temp_obj = buf = xmalloc(size);
/* Compress it */
stream.next_out = buf;
while (deflate(&stream, Z_FINISH) == Z_OK)
/* nothing */;
deflateEnd(&stream);
+ free(unpacked);
objsize = stream.total_out;
}
}
posn += size;
} while (posn < objsize);
+
+ if (map)
+ munmap(map, objsize);
+ if (temp_obj)
+ free(temp_obj);
+
return 0;
}
-int write_sha1_from_fd(const unsigned char *sha1, int fd)
+int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
+ size_t bufsize, size_t *bufposn)
{
char *filename = sha1_file_name(sha1);
int local;
z_stream stream;
unsigned char real_sha1[20];
- unsigned char buf[4096];
unsigned char discard[4096];
int ret;
SHA_CTX c;
do {
ssize_t size;
- size = read(fd, buf, 4096);
+ if (*bufposn) {
+ stream.avail_in = *bufposn;
+ stream.next_in = (unsigned char *) buffer;
+ do {
+ stream.next_out = discard;
+ stream.avail_out = sizeof(discard);
+ ret = inflate(&stream, Z_SYNC_FLUSH);
+ SHA1_Update(&c, discard, sizeof(discard) -
+ stream.avail_out);
+ } while (stream.avail_in && ret == Z_OK);
+ write(local, buffer, *bufposn - stream.avail_in);
+ memmove(buffer, buffer + *bufposn - stream.avail_in,
+ stream.avail_in);
+ *bufposn = stream.avail_in;
+ if (ret != Z_OK)
+ break;
+ }
+ size = read(fd, buffer + *bufposn, bufsize - *bufposn);
if (size <= 0) {
close(local);
unlink(filename);
perror("Reading from connection");
return -1;
}
- write(local, buf, size);
- stream.avail_in = size;
- stream.next_in = buf;
- do {
- stream.next_out = discard;
- stream.avail_out = sizeof(discard);
- ret = inflate(&stream, Z_SYNC_FLUSH);
- SHA1_Update(&c, discard, sizeof(discard) -
- stream.avail_out);
- } while (stream.avail_in && ret == Z_OK);
-
- } while (ret == Z_OK);
+ *bufposn += size;
+ } while (1);
inflateEnd(&stream);
close(local);