@matheusbsbs / matheus-bagliano / commits / 32637cdf4a
Fork 0

show-index.c: learn about index v2

When index v2 is encountered, the CRC32 of each object is also displayed in parenthesis at the end of the line. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 9, 2007 at 01:06 UTC 32637cdf4a6a8d806feaa6b018bc4e4c7e2eb366
1 file changed +59 -9
show-index.c
+59 -9
@@ -1,14 +1,26 @@
1 #include "cache.h"
2 +#include "pack.h"
3
4 int main(int argc, char **argv)
5 {
6 int i;
7 unsigned nr;
7 - unsigned int entry[6];
8 + unsigned int version;
9 static unsigned int top_index[256];
10
10 - if (fread(top_index, sizeof(top_index), 1, stdin) != 1)
11 - die("unable to read index");
11 + if (fread(top_index, 2 * 4, 1, stdin) != 1)
12 + die("unable to read header");
13 + if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
14 + version = ntohl(top_index[1]);
15 + if (version < 2 || version > 2)
16 + die("unknown index version");
17 + if (fread(top_index, 256 * 4, 1, stdin) != 1)
18 + die("unable to read index");
19 + } else {
20 + version = 1;
21 + if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
22 + die("unable to read index");
23 + }
24 nr = 0;
25 for (i = 0; i < 256; i++) {
26 unsigned n = ntohl(top_index[i]);
@@ -16,13 +28,51 @@ int main(int argc, char **argv)
28 die("corrupt index file");
29 nr = n;
30 }
19 - for (i = 0; i < nr; i++) {
20 - unsigned offset;
31 + if (version == 1) {
32 + for (i = 0; i < nr; i++) {
33 + unsigned int offset, entry[6];
34
22 - if (fread(entry, 24, 1, stdin) != 1)
23 - die("unable to read entry %u/%u", i, nr);
24 - offset = ntohl(entry[0]);
25 - printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1)));
35 + if (fread(entry, 4 + 20, 1, stdin) != 1)
36 + die("unable to read entry %u/%u", i, nr);
37 + offset = ntohl(entry[0]);
38 + printf("%u %s\n", offset, sha1_to_hex((void *)(entry+1)));
39 + }
40 + } else {
41 + unsigned off64_nr = 0;
42 + struct {
43 + unsigned char sha1[20];
44 + uint32_t crc;
45 + uint32_t off;
46 + } *entries = xmalloc(nr * sizeof(entries[0]));
47 + for (i = 0; i < nr; i++)
48 + if (fread(entries[i].sha1, 20, 1, stdin) != 1)
49 + die("unable to read sha1 %u/%u", i, nr);
50 + for (i = 0; i < nr; i++)
51 + if (fread(&entries[i].crc, 4, 1, stdin) != 1)
52 + die("unable to read crc %u/%u", i, nr);
53 + for (i = 0; i < nr; i++)
54 + if (fread(&entries[i].off, 4, 1, stdin) != 1)
55 + die("unable to read 32b offset %u/%u", i, nr);
56 + for (i = 0; i < nr; i++) {
57 + uint64_t offset;
58 + uint32_t off = ntohl(entries[i].off);
59 + if (!(off & 0x80000000)) {
60 + offset = off;
61 + } else {
62 + uint32_t off64[2];
63 + if ((off & 0x7fffffff) != off64_nr)
64 + die("inconsistent 64b offset index");
65 + if (fread(off64, 8, 1, stdin) != 1)
66 + die("unable to read 64b offset %u", off64_nr);
67 + offset = (((uint64_t)ntohl(off64[0])) << 32) |
68 + ntohl(off64[1]);
69 + off64_nr++;
70 + }
71 + printf("%llu %s (%08x)\n", (unsigned long long) offset,
72 + sha1_to_hex(entries[i].sha1),
73 + ntohl(entries[i].crc));
74 + }
75 + free(entries);
76 }
77 return 0;
78 }