[PATCH] http-pull: documentation updates.
[git/dscho.git] / unpack-objects.c
blob7b175c9a49c61fd5ecc482d5f6df1b09928c73b8
1 #include "cache.h"
2 #include "object.h"
4 static int dry_run;
5 static int nr_entries;
6 static const char *base_name;
7 static const char unpack_usage[] = "git-unpack-objects basename";
9 struct pack_entry {
10 unsigned int offset;
11 unsigned char sha1[20];
14 static void *pack_base;
15 static unsigned long pack_size;
17 static struct pack_entry **pack_list;
19 static void *map_file(const char *suffix, unsigned long *sizep)
21 static char pathname[PATH_MAX];
22 unsigned long len;
23 int fd;
24 struct stat st;
25 void *map;
27 len = snprintf(pathname, PATH_MAX, "%s.%s", base_name, suffix);
28 if (len >= PATH_MAX)
29 die("bad pack base-name");
30 fd = open(pathname, O_RDONLY);
31 if (fd < 0 || fstat(fd, &st))
32 die("unable to open '%s'", pathname);
33 len = st.st_size;
34 if (!len)
35 die("bad pack file '%s'", pathname);
36 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
37 if (-1 == (int)(long)map)
38 die("unable to mmap '%s'", pathname);
39 close(fd);
40 *sizep = len;
41 return map;
44 static int sort_by_offset(const void *_a, const void *_b)
46 struct pack_entry *a = *(struct pack_entry **)_a;
47 struct pack_entry *b = *(struct pack_entry **)_b;
48 unsigned int o1, o2;
50 o1 = ntohl(a->offset);
51 o2 = ntohl(b->offset);
52 return o1 < o2 ? -1 : 1;
55 static int check_index(void *index, unsigned long idx_size)
57 unsigned int *array = index;
58 unsigned int nr;
59 int i;
61 if (idx_size < 4*256)
62 return error("index file too small");
63 nr = 0;
64 for (i = 0; i < 256; i++) {
65 unsigned int n = ntohl(array[i]);
66 if (n < nr)
67 return error("non-monotonic index");
68 nr = n;
70 if (idx_size != 4*256 + nr * 24) {
71 printf("idx_size=%lu, expected %u (%u)\n", idx_size, 4*256 + nr * 24, nr);
72 return error("wrong index file size");
75 nr_entries = nr;
76 pack_list = xmalloc(nr * sizeof(struct pack_entry *));
77 for (i = 0; i < nr; i++)
78 pack_list[i] = index + 4*256 + i*24;
80 qsort(pack_list, nr, sizeof(*pack_list), sort_by_offset);
82 printf("%d entries\n", nr);
83 return 0;
86 static void unpack_entry(struct pack_entry *entry)
88 unsigned long size;
89 unsigned long offset;
90 unsigned char *pack;
92 /* Have we done this one already due to deltas based on it? */
93 if (lookup_object(entry->sha1))
94 return;
96 offset = ntohl(entry->offset);
97 if (offset > pack_size - 5)
98 die("object offset outside of pack file");
99 pack = pack_base + offset;
100 offset = pack_size - offset;
101 switch (*pack) {
102 case 'C': case 'T': case 'B':
103 size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
104 printf("%s %c %lu\n", sha1_to_hex(entry->sha1), *pack, size);
105 break;
106 case 'D':
107 printf("%s D", sha1_to_hex(entry->sha1));
108 printf(" %s\n", sha1_to_hex(pack+1));
109 break;
110 default:
111 die("corrupted pack file");
116 * We unpack from the end, older files first. Now, usually
117 * there are deltas etc, so we'll not actually write the
118 * objects in that order, but we might as well try..
120 static void unpack_all(void)
122 int i = nr_entries;
124 while (--i >= 0) {
125 struct pack_entry *entry = pack_list[i];
126 unpack_entry(entry);
130 int main(int argc, char **argv)
132 int i;
133 unsigned long idx_size;
134 void *index;
136 for (i = 1 ; i < argc; i++) {
137 const char *arg = argv[i];
139 if (*arg == '-') {
140 if (!strcmp(arg, "-n")) {
141 dry_run = 1;
142 continue;
144 usage(unpack_usage);
146 if (base_name)
147 usage(unpack_usage);
148 base_name = arg;
150 if (!base_name)
151 usage(unpack_usage);
152 index = map_file("idx", &idx_size);
153 pack_base = map_file("pack", &pack_size);
154 if (check_index(index, idx_size) < 0)
155 die("bad index file");
156 unpack_all();
157 return 0;