Remove some unneeded 'exit 0' from bash scripts
[lcapit-junk-code.git] / git / dump-pack-idx-v1.c
blobef00bc212d9e8a1fb42a4bd00efb0950269b3f45
1 /*
2 * dump-pack-idx-v1: Dump the 'SHA1 offset' list from a .idx v1 file
3 */
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/mman.h>
12 #include <arpa/inet.h>
14 #define FAN_OUT_SIZE (4 * 256)
15 #define PACK_IDX_SIGNATURE 0xff744f63
17 struct pack_idx_header {
18 uint32_t idx_signature;
19 uint32_t idx_version;
22 static inline void hashcpy(unsigned char *sha_dst,
23 const unsigned char *sha_src)
25 memcpy(sha_dst, sha_src, 20);
28 static char *sha1_to_hex(const unsigned char *sha1)
30 static int bufno;
31 static char hexbuffer[4][50];
32 static const char hex[] = "0123456789abcdef";
33 char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
34 int i;
36 for (i = 0; i < 20; i++) {
37 unsigned int val = *sha1++;
38 *buf++ = hex[val >> 4];
39 *buf++ = hex[val & 0xf];
41 *buf = '\0';
43 return buffer;
46 static void dump_index_v1(void *base, off_t size)
48 unsigned char *list;
49 unsigned long nr_sha1, i;
51 list = base + FAN_OUT_SIZE;
52 nr_sha1 = (size - (FAN_OUT_SIZE + 40)) / 24;
53 for (i = 0; i < nr_sha1; i++) {
54 unsigned char sha1[20];
55 unsigned int offset;
57 memcpy(&offset, list, sizeof(offset));
58 hashcpy(sha1, list + 4);
59 printf("%s %u\n", sha1_to_hex(sha1), ntohl(offset));
60 list += 24;
64 static void usage(void)
66 printf("dump-pack-idx-v1 [ -f -v ] < pack-idx >\n");
69 int main(int argc, char *argv[])
71 void *idx;
72 char *idx_file;
73 struct stat buf;
74 struct pack_idx_header *hdr;
75 int version, force, show_version, opt, err, fd;
77 version = 1;
78 show_version = force = 0;
80 while ((opt = getopt(argc, argv, "vf")) != -1) {
81 switch (opt) {
82 case 'v':
83 show_version = 1;
84 break;
85 case 'f':
86 force = 1;
87 break;
88 default:
89 usage();
90 exit(1);
94 if (optind >= argc) {
95 usage();
96 exit(1);
98 idx_file = argv[optind];
100 if (!strstr(idx_file, ".idx") && !force) {
101 fprintf(stderr,
102 "ERROR: '%s' doesn't seem to be an .idx file, try '-f'"
103 " if you know what you're doing\n", idx_file);
104 exit(1);
107 fd = open(idx_file, O_RDONLY);
108 if (fd < 0) {
109 perror("open()");
110 exit(1);
113 err = fstat(fd, &buf);
114 if (err) {
115 perror("fstat()");
116 exit(1);
119 idx = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
120 close(fd);
121 if (idx == MAP_FAILED) {
122 perror("mmap()");
123 exit(1);
126 hdr = idx;
127 if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE))
128 version = ntohl(hdr->idx_version);
130 if (show_version) {
131 printf("version: %d\n", version);
132 exit(0);
135 if (version != 1) {
136 printf("Only dumps index v1, this is index v%d\n", version);
137 exit(1);
140 dump_index_v1(idx, buf.st_size);
142 munmap(idx, buf.st_size);
143 return 0;