return unpack_sha1_rest(&stream, hdr, *size);
}
-/* Returns 0 on fast-path success, returns 1 on deltified
- * and need to unpack to see info.
- */
+static int packed_delta_info(unsigned char *base_sha1,
+ unsigned long delta_size,
+ unsigned long left,
+ char *type,
+ unsigned long *sizep)
+{
+ unsigned char *data;
+ unsigned char delta_head[64];
+ int i;
+ unsigned char cmd;
+ unsigned long data_size, result_size, base_size, verify_base_size;
+ z_stream stream;
+ int st;
+
+ if (left < 20)
+ die("truncated pack file");
+ if (sha1_object_info(base_sha1, type, &base_size))
+ die("cannot get info for delta-pack base");
+
+ data = base_sha1 + 20;
+ data_size = left - 20;
+
+ memset(&stream, 0, sizeof(stream));
+
+ stream.next_in = data;
+ stream.avail_in = data_size;
+ stream.next_out = delta_head;
+ stream.avail_out = sizeof(delta_head);
+
+ inflateInit(&stream);
+ st = inflate(&stream, Z_FINISH);
+ inflateEnd(&stream);
+ if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head))
+ die("delta data unpack-initial failed");
+
+ /* Examine the initial part of the delta to figure out
+ * the result size. Verify the base size while we are at it.
+ */
+ data = delta_head;
+ verify_base_size = i = 0;
+ cmd = *data++;
+ while (cmd) {
+ if (cmd & 1)
+ verify_base_size |= *data++ << i;
+ i += 8;
+ cmd >>= 1;
+ }
+
+ /* Read the result size */
+ result_size = i = 0;
+ cmd = *data++;
+ while (cmd) {
+ if (cmd & 1)
+ result_size |= *data++ << i;
+ i += 8;
+ cmd >>= 1;
+ }
+ if (verify_base_size != base_size)
+ die("delta base size mismatch");
+
+ *sizep = result_size;
+ return 0;
+}
+
static int packed_object_info(struct pack_entry *entry,
char *type, unsigned long *sizep)
{
offset = entry->offset;
if (p->pack_size - 5 < offset)
die("object offset outside of pack file");
+
+ if (use_packed_git(p))
+ die("cannot map packed file");
+
pack = p->pack_base + offset;
size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
left = p->pack_size - offset - 5;
switch (*pack) {
case 'D':
- return 1;
+ return packed_delta_info(pack+5, size, left, type, sizep);
break;
case 'C':
strcpy(type, "commit");
case 'B':
strcpy(type, "blob");
break;
+ case 'G':
+ strcpy(type, "tag");
+ break;
default:
die("corrupted pack file");
}
case 'B':
strcpy(type, "blob");
break;
+ case 'G':
+ strcpy(type, "tag");
+ break;
default:
die("corrupted pack file");
}
}
}
+static char *write_sha1_file_prepare(void *buf,
+ unsigned long len,
+ const char *type,
+ unsigned char *sha1,
+ unsigned char *hdr,
+ int *hdrlen)
+{
+ SHA_CTX c;
+
+ /* Generate the header */
+ *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
+
+ /* Sha1.. */
+ SHA1_Init(&c);
+ SHA1_Update(&c, hdr, *hdrlen);
+ SHA1_Update(&c, buf, len);
+ SHA1_Final(sha1, &c);
+
+ return sha1_file_name(sha1);
+}
+
int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
{
int size;
unsigned char *compressed;
z_stream stream;
unsigned char sha1[20];
- SHA_CTX c;
char *filename;
static char tmpfile[PATH_MAX];
unsigned char hdr[50];
int fd, hdrlen, ret;
- /* Generate the header */
- hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
-
- /* Sha1.. */
- SHA1_Init(&c);
- SHA1_Update(&c, hdr, hdrlen);
- SHA1_Update(&c, buf, len);
- SHA1_Final(sha1, &c);
-
+ /* Normally if we have it in the pack then we do not bother writing
+ * it out into .git/objects/??/?{38} file.
+ */
+ filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
if (returnsha1)
memcpy(returnsha1, sha1, 20);
-
- filename = sha1_file_name(sha1);
+ if (has_sha1_file(sha1))
+ return 0;
fd = open(filename, O_RDONLY);
if (fd >= 0) {
/*