7 static const char *base_name;
8 static const char unpack_usage[] = "git-unpack-objects basename";
11 unsigned int offset; /* network byte order */
12 unsigned char sha1[20];
15 static void *pack_base;
16 static unsigned long pack_size;
17 static void *index_base;
18 static unsigned long index_size;
20 static struct pack_entry **pack_list;
22 static void *map_file(const char *suffix, unsigned long *sizep)
24 static char pathname[PATH_MAX];
30 len = snprintf(pathname, PATH_MAX, "%s.%s", base_name, suffix);
32 die("bad pack base-name");
33 fd = open(pathname, O_RDONLY);
34 if (fd < 0 || fstat(fd, &st))
35 die("unable to open '%s'", pathname);
38 die("bad pack file '%s'", pathname);
39 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
40 if (-1 == (int)(long)map)
41 die("unable to mmap '%s'", pathname);
47 static int sort_by_offset(const void *_a, const void *_b)
49 struct pack_entry *a = *(struct pack_entry **)_a;
50 struct pack_entry *b = *(struct pack_entry **)_b;
53 o1 = ntohl(a->offset);
54 o2 = ntohl(b->offset);
55 return o1 < o2 ? -1 : 1;
58 static int check_index(void)
60 unsigned int *array = index_base;
64 if (index_size < 4*256)
65 return error("index file too small");
67 for (i = 0; i < 256; i++) {
68 unsigned int n = ntohl(array[i]);
70 return error("non-monotonic index");
73 if (index_size != 4*256 + nr * 24) {
74 printf("index_size=%lu, expected %u (%u)\n",
75 index_size, 4*256 + nr * 24, nr);
76 return error("wrong index file size");
80 pack_list = xmalloc(nr * sizeof(struct pack_entry *));
81 for (i = 0; i < nr; i++)
82 pack_list[i] = index_base + 4*256 + i*24;
84 qsort(pack_list, nr, sizeof(*pack_list), sort_by_offset);
86 printf("%d entries\n", nr);
90 static int unpack_non_delta_entry(struct pack_entry *entry,
99 unsigned char sha1[20];
102 printf("%s %c %lu\n", sha1_to_hex(entry->sha1), kind, size);
106 buffer = xmalloc(size + 1);
108 memset(&stream, 0, sizeof(stream));
109 stream.next_in = data;
110 stream.avail_in = left;
111 stream.next_out = buffer;
112 stream.avail_out = size;
114 inflateInit(&stream);
115 st = inflate(&stream, Z_FINISH);
117 if ((st != Z_STREAM_END) || stream.total_out != size)
120 case 'C': type_s = "commit"; break;
121 case 'T': type_s = "tree"; break;
122 case 'B': type_s = "blob"; break;
123 default: goto err_finish;
125 if (write_sha1_file(buffer, size, type_s, sha1) < 0)
126 die("failed to write %s (%s)",
127 sha1_to_hex(entry->sha1), type_s);
128 printf("%s %s\n", sha1_to_hex(sha1), type_s);
129 if (memcmp(sha1, entry->sha1, 20))
130 die("resulting %s have wrong SHA1", type_s);
141 static int find_pack_entry(unsigned char *sha1, struct pack_entry **ent)
143 int *level1_ofs = index_base;
144 int hi = ntohl(level1_ofs[*sha1]);
145 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
146 void *index = index_base + 4*256;
149 int mi = (lo + hi) / 2;
150 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
151 printf("lo=%d mi=%d hi=%d cmp=%d\n", lo, mi, hi, cmp);
153 *ent = index + 24 * mi;
164 /* forward declaration for a mutually recursive function */
165 static void unpack_entry(struct pack_entry *);
167 static int unpack_delta_entry(struct pack_entry *entry,
168 unsigned char *base_sha1,
169 unsigned long delta_size,
172 void *data, *delta_data, *result, *base;
173 unsigned long data_size, result_size, base_size;
177 unsigned char sha1[20];
180 die("truncated pack file");
181 data = base_sha1 + 20;
182 data_size = left - 20;
183 printf("%s D %lu", sha1_to_hex(entry->sha1), delta_size);
184 printf(" %s\n", sha1_to_hex(base_sha1));
189 /* pack+5 is the base sha1, unless we have it, we need to
192 if (!has_sha1_file(base_sha1)) {
193 struct pack_entry *base;
194 if (!find_pack_entry(base_sha1, &base))
195 die("cannot find delta-pack base object");
198 delta_data = xmalloc(delta_size);
200 memset(&stream, 0, sizeof(stream));
202 stream.next_in = data;
203 stream.avail_in = data_size;
204 stream.next_out = delta_data;
205 stream.avail_out = delta_size;
207 inflateInit(&stream);
208 st = inflate(&stream, Z_FINISH);
210 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
211 die("delta data unpack failed");
213 base = read_sha1_file(base_sha1, type, &base_size);
215 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
216 result = patch_delta(base, base_size,
217 delta_data, delta_size,
220 die("failed to apply delta");
223 if (write_sha1_file(result, result_size, type, sha1) < 0)
224 die("failed to write %s (%s)",
225 sha1_to_hex(entry->sha1), type);
227 printf("%s %s\n", sha1_to_hex(sha1), type);
228 if (memcmp(sha1, entry->sha1, 20))
229 die("resulting %s have wrong SHA1", type);
233 static void unpack_entry(struct pack_entry *entry)
235 unsigned long offset, size, left;
238 /* Have we done this one already due to deltas based on it? */
239 if (lookup_object(entry->sha1))
242 offset = ntohl(entry->offset);
243 if (offset > pack_size - 5)
244 die("object offset outside of pack file");
245 pack = pack_base + offset;
246 size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
247 left = pack_size - offset - 5;
249 case 'C': case 'T': case 'B':
250 unpack_non_delta_entry(entry, *pack, pack+5, size, left);
253 unpack_delta_entry(entry, pack+5, size, left);
256 die("corrupted pack file");
261 * We unpack from the end, older files first. Now, usually
262 * there are deltas etc, so we'll not actually write the
263 * objects in that order, but we might as well try..
265 static void unpack_all(void)
270 struct pack_entry *entry = pack_list[i];
275 int main(int argc, char **argv)
279 for (i = 1 ; i < argc; i++) {
280 const char *arg = argv[i];
283 if (!strcmp(arg, "-n")) {
295 index_base = map_file("idx", &index_size);
296 pack_base = map_file("pack", &pack_size);
297 if (check_index() < 0)
298 die("bad index file");