10 #include <arpa/inet.h>
12 #define FAN_OUT_SIZE (4 * 256)
13 #define PACK_IDX_SIGNATURE 0xff744f63
14 #define PACK_SIGNATURE 0x5041434b /* "PACK" */
16 struct pack_idx_header
{
17 uint32_t idx_signature
;
22 uint32_t hdr_signature
;
27 static char *sha1_to_hex(const unsigned char *sha1
)
30 static char hexbuffer
[4][50];
31 static const char hex
[] = "0123456789abcdef";
32 char *buffer
= hexbuffer
[3 & ++bufno
], *buf
= buffer
;
35 for (i
= 0; i
< 20; i
++) {
36 unsigned int val
= *sha1
++;
37 *buf
++ = hex
[val
>> 4];
38 *buf
++ = hex
[val
& 0xf];
45 static int xread(int fd
, void *buf
, ssize_t size
)
49 bytes
= read(fd
, buf
, size
);
55 static void idx_open(const char *file
, int *fd
)
58 struct pack_idx_header hdr
;
60 *fd
= open(file
, O_RDONLY
);
66 err
= xread(*fd
, &hdr
, sizeof(hdr
));
68 perror("read_stdin()");
72 if (hdr
.idx_signature
!= htonl(PACK_IDX_SIGNATURE
)) {
74 "ERROR: '%s' is not an index file or is index v1\n",
79 version
= ntohl(hdr
.idx_version
);
82 "ERROR: must be index v3, but it's index v%d\n",
88 static void pck_open(const char *file
, int *fd
, size_t *nr_obj
)
91 struct pack_header hdr
;
93 *fd
= open(file
, O_RDONLY
);
99 err
= xread(*fd
, &hdr
, sizeof(hdr
));
101 perror("read_stdin()");
105 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
)) {
106 fprintf(stderr
, "file '%s' is not a GIT packfile\n", file
);
110 if (ntohl(hdr
.hdr_version
) != 4) {
111 fprintf(stderr
, "pack file is not v4 (it's %d)\n",
112 htonl(hdr
.hdr_version
));
116 *nr_obj
= ntohl(hdr
.hdr_entries
);
119 static void usage(void)
121 printf("dump-sha1-list < pack-idx > < pack-file >\n");
124 int main(int argc
, char *argv
[])
128 char *idx_file
, *pck_file
;
135 if (strstr(argv
[1], ".idx")) {
143 idx_open(idx_file
, &idx_fd
);
144 pck_open(pck_file
, &pck_fd
, &nr_obj
);
146 /* skip the fan out and crc32 tables */
147 if (lseek(idx_fd
, FAN_OUT_SIZE
+ nr_obj
* sizeof(uint32_t),
153 for (i
= 0; i
< nr_obj
; i
++) {
154 unsigned char sha1
[20];
157 xread(pck_fd
, sha1
, 20);
158 xread(idx_fd
, &offset
, sizeof(offset
));
159 printf("%s %u\n", sha1_to_hex(sha1
), ntohl(offset
));